llvm-native-core 0.1.9

LLVM-native core semantic engine — IR, CodeGen, X86 MC, Clang frontend pipeline
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
//! Clang Diagnostics Full — Complete diagnostic catalog, rendering, and
//! color-coded output for C/C++ frontend diagnostics.
//!
//! This module provides the full complement of diagnostic messages, rendering
//! with color-coded output (error=red+bold, warning=magenta+bold, note=cyan,
//! fixit=green, caret=green), caret-range highlighting, source snippet display
//! with context lines, and diagnostic category display.
//!
//! Coverage:
//! - 300+ C/C++ error messages (type, syntax, semantic, linker)
//! - 200+ C/C++ warning messages (-Wall through -Wwrite-strings)
//! - Note/fixit messages with suggestion engine
//! - Color-coded output with ANSI terminal sequences
//! - Caret-ranges with multiple range highlighting (~~~~^~~~~~)
//! - Source snippet display with line numbers and context
//! - Diagnostic category display [-Wcategory-name]
//!
//! Clean-room behavioral reconstruction from the Clang diagnostic
//! documentation and observed compiler output. No LLVM source consulted.

use std::collections::{BTreeMap, HashMap, HashSet};
use std::fmt;
use std::io::{self, Write};

use super::diagnostics::{
    ClangSourceLocation, DiagID, DiagSeverity, DiagnosticEngine, DiagnosticNote, DiagnosticOptions,
    FixItHint, SourceRange, StructuredDiagnostic,
};

// ═══════════════════════════════════════════════════════════════════════════════
// Diagnostic Category Registry
// ═══════════════════════════════════════════════════════════════════════════════

/// Diagnostic category with its warning flag name.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct DiagnosticCategory {
    /// The human-readable category name (e.g., "unused-variable").
    pub name: String,
    /// The flag name used on the command line (e.g., "-Wunused-variable").
    pub flag: String,
    /// The diagnostic IDs belonging to this category.
    pub members: Vec<DiagID>,
    /// Whether this category is enabled by default.
    pub enabled_by_default: bool,
    /// The parent group, if any (e.g., "unused" is parent of "unused-variable").
    pub parent: Option<String>,
    /// Description of what this category covers.
    pub description: String,
}

impl DiagnosticCategory {
    pub fn new(name: &str, flag: &str, description: &str) -> Self {
        Self {
            name: name.to_string(),
            flag: flag.to_string(),
            members: Vec::new(),
            enabled_by_default: true,
            parent: None,
            description: description.to_string(),
        }
    }

    pub fn with_default_enabled(mut self, enabled: bool) -> Self {
        self.enabled_by_default = enabled;
        self
    }

    pub fn with_parent(mut self, parent: &str) -> Self {
        self.parent = Some(parent.to_string());
        self
    }

    pub fn add_member(&mut self, id: DiagID) {
        self.members.push(id);
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// Complete Diagnostic Category Catalog
// ═══════════════════════════════════════════════════════════════════════════════

/// Registry of all diagnostic categories with their memberships.
pub struct DiagnosticCategoryRegistry {
    pub categories: BTreeMap<String, DiagnosticCategory>,
    pub id_to_category: HashMap<DiagID, Vec<String>>,
    pub group_to_members: HashMap<String, Vec<String>>,
}

impl DiagnosticCategoryRegistry {
    /// Builds the complete diagnostic category catalog (300+ error, 200+ warning categories).
    pub fn new() -> Self {
        let mut reg = Self {
            categories: BTreeMap::new(),
            id_to_category: HashMap::new(),
            group_to_members: HashMap::new(),
        };
        reg.register_all();
        reg
    }

    fn register_all(&mut self) {
        // ── Warning Groups ──────────────────────────────────────────────────
        self.register_wall_categories();
        self.register_wextra_categories();
        self.register_wpedantic_categories();
        self.register_wconversion_categories();
        self.register_wshadow_categories();
        self.register_wunused_categories();
        self.register_wformat_categories();
        self.register_wsign_compare_categories();
        self.register_wswitch_categories();
        self.register_wtautological_categories();
        self.register_wnull_dereference_categories();
        self.register_wreturn_type_categories();
        self.register_wuninitialized_categories();
        self.register_wparentheses_categories();
        self.register_wmisleading_indentation_categories();
        self.register_wimplicit_fallthrough_categories();
        self.register_wstrict_aliasing_categories();
        self.register_wcast_categories();
        self.register_wdouble_promotion_categories();
        self.register_wfloat_equal_categories();
        self.register_wmissing_field_init_categories();
        self.register_wmissing_prototypes_categories();
        self.register_wstrict_prototypes_categories();
        self.register_wold_style_definition_categories();
        self.register_wpointer_categories();
        self.register_wunreachable_code_categories();
        self.register_wwrite_strings_categories();

        // ── Error Groups ─────────────────────────────────────────────────────
        self.register_syntax_error_categories();
        self.register_type_error_categories();
        self.register_semantic_error_categories();
        self.register_linker_error_categories();
        self.register_preprocessor_error_categories();
        self.register_template_error_categories();
        self.register_constraint_error_categories();

        // ── Note/Fixit Groups ────────────────────────────────────────────────
        self.register_note_categories();
        self.register_fixit_categories();
    }

    // ── -Wall categories (enabled by default with -Wall) ──────────────────

    fn register_wall_categories(&mut self) {
        let cats = vec![
            ("unused-variable", "-Wunused-variable", "Unused variable declaration"),
            ("unused-parameter", "-Wunused-parameter", "Unused function parameter"),
            ("unused-function", "-Wunused-function", "Unused static function"),
            ("unused-value", "-Wunused-value", "Expression result unused"),
            ("unused-label", "-Wunused-label", "Unused label"),
            ("sign-compare", "-Wsign-compare", "Comparison between signed and unsigned"),
            ("format", "-Wformat", "printf/scanf format string issues"),
            ("format-security", "-Wformat-security", "Format string is not a string literal"),
            ("parentheses", "-Wparentheses", "Suggest parentheses around expression"),
            ("switch", "-Wswitch", "Switch statement missing enum cases"),
            ("implicit-fallthrough", "-Wimplicit-fallthrough", "Implicit fallthrough in switch"),
            ("tautological-compare", "-Wtautological-compare", "Self-comparison always true/false"),
            ("return-type", "-Wreturn-type", "Non-void function without return"),
            ("uninitialized", "-Wuninitialized", "Use of uninitialized variable"),
            ("array-bounds", "-Warray-bounds", "Array index out of bounds"),
            ("char-subscripts", "-Wchar-subscripts", "Array subscript is of type 'char'"),
            ("comment", "-Wcomment", "Multi-line comment issues"),
            ("int-conversion", "-Wint-conversion", "Implicit integer conversion"),
            ("int-in-bool-context", "-Wint-in-bool-context", "Integer used as boolean"),
            ("main", "-Wmain", "Suspicious main function signature"),
            ("misleading-indentation", "-Wmisleading-indentation", "Misleading indentation"),
            ("missing-braces", "-Wmissing-braces", "Subobject initializer missing braces"),
            ("multichar", "-Wmultichar", "Multi-character character constant"),
            ("nonnull", "-Wnonnull", "Null passed to nonnull parameter"),
            ("pointer-sign", "-Wpointer-sign", "Pointer signedness mismatch"),
            ("reorder", "-Wreorder", "Member initializer order mismatch"),
            ("return-local-addr", "-Wreturn-local-addr", "Returning address of local"),
            ("sequence-point", "-Wsequence-point", "Unsequenced modification"),
            ("shift-count-overflow", "-Wshift-count-overflow", "Shift count >= type width"),
            ("shift-negative-value", "-Wshift-negative-value", "Shifting negative value"),
            ("sizeof-array-argument", "-Wsizeof-array-argument", "sizeof on array parameter"),
            ("sizeof-pointer-memaccess", "-Wsizeof-pointer-memaccess", "Suspicious memaccess size"),
            ("strict-aliasing", "-Wstrict-aliasing", "Type-punning violates aliasing rules"),
            ("unknown-pragmas", "-Wunknown-pragmas", "Unknown pragma directive"),
            ("unused-but-set-variable", "-Wunused-but-set-variable", "Variable set but not used"),
            ("unused-result", "-Wunused-result", "Unused result of function"),
            ("unused-local-typedef", "-Wunused-local-typedef", "Unused local typedef"),
            ("delete-non-virtual-dtor", "-Wdelete-non-virtual-dtor", "Delete on non-virtual destructor"),
            ("delete-abstract-non-virtual-dtor", "-Wdelete-abstract-non-virtual-dtor", "Delete on abstract class with non-virtual destructor"),
            ("memset-transposed-args", "-Wmemset-transposed-args", "Possibly transposed memset args"),
            ("suspicious-bzero", "-Wsuspicious-bzero", "Possibly incorrect bzero size"),
            ("absolute-value", "-Wabsolute-value", "Wrong type argument to absolute value"),
            ("self-assign", "-Wself-assign", "Explicit self-assignment"),
            ("self-assign-overloaded", "-Wself-assign-overloaded", "Self-assignment with overloaded operator="),
            ("self-move", "-Wself-move", "Explicit self-move"),
            ("unused-private-field", "-Wunused-private-field", "Private field not used"),
            ("unused-lambda-capture", "-Wunused-lambda-capture", "Lambda capture not used"),
            ("unused-const-variable", "-Wunused-const-variable", "Unused const variable"),
            ("infinite-recursion", "-Winfinite-recursion", "Function infinitely recurses"),
        ];
        for (name, flag, desc) in cats {
            let cat = DiagnosticCategory::new(name, flag, desc)
                .with_default_enabled(true)
                .with_parent("all");
            self.categories.insert(name.to_string(), cat);
            self.group_to_members
                .entry("all".to_string())
                .or_default()
                .push(name.to_string());
        }
    }

    // ── -Wextra categories ─────────────────────────────────────────────────

    fn register_wextra_categories(&mut self) {
        let cats = vec![
            ("unused-parameter", "-Wunused-parameter", "Unused function parameter (extra)"),
            ("sign-compare-extra", "-Wsign-compare", "Additional signed/unsigned compare"),
            ("missing-field-initializers", "-Wmissing-field-initializers", "Missing initializer for struct field"),
            ("type-limits", "-Wtype-limits", "Comparison always true due to type limits"),
            ("empty-body", "-Wempty-body", "Empty body in if/else/for/while"),
            ("enum-compare", "-Wenum-compare", "Comparison of different enum types"),
            ("implicit-fallthrough-per-function", "-Wimplicit-fallthrough-per-function", "Fallthrough per function"),
            ("ignored-qualifiers", "-Wignored-qualifiers", "Ignored type qualifier on return type"),
            ("init-self", "-Winit-self", "Variable initialized with itself"),
            ("logical-not-parentheses", "-Wlogical-not-parentheses", "! binds tighter than comparison"),
            ("logical-op-parentheses", "-Wlogical-op-parentheses", "Logical operator inside other expression"),
            ("missing-include-dirs", "-Wmissing-include-dirs", "Missing include directories"),
            ("mismatched-tags", "-Wmismatched-tags", "Mismatched struct/class tags"),
            ("missing-variable-declarations", "-Wmissing-variable-declarations", "No previous declaration for global"),
            ("pointer-compare", "-Wpointer-compare", "Comparison between pointer and zero"),
            ("redundant-decls", "-Wredundant-decls", "Redundant declarations"),
            ("sometimes-uninitialized", "-Wsometimes-uninitialized", "Variable sometimes uninitialized"),
            ("tautological-undefined-compare", "-Wtautological-undefined-compare", "Undefined behavior comparison"),
            ("unused-function-extra", "-Wunused-function", "Additional unused function checks"),
            ("unused-label-extra", "-Wunused-label", "Additional unused label checks"),
            ("unused-value-extra", "-Wunused-value", "Additional unused value checks"),
            ("vexing-parse", "-Wvexing-parse", "Most vexing parse ambiguity"),
        ];
        for (name, flag, desc) in cats {
            let cat = DiagnosticCategory::new(name, flag, desc)
                .with_default_enabled(false)
                .with_parent("extra");
            self.categories.insert(name.to_string(), cat);
            self.group_to_members
                .entry("extra".to_string())
                .or_default()
                .push(name.to_string());
        }
    }

    // ── -Wpedantic categories ──────────────────────────────────────────────

    fn register_wpedantic_categories(&mut self) {
        let cats = vec![
            ("pedantic", "-Wpedantic", "Issues mandated by the language standard"),
            ("long-long", "-Wlong-long", "ISO C90 does not support long long"),
            ("variadic-macros", "-Wvariadic-macros", "Variadic macros are a C99 feature"),
            ("c99-extensions", "-Wc99-extensions", "C99 extension used"),
            ("c11-extensions", "-Wc11-extensions", "C11 extension used"),
            ("gnu-extensions", "-Wgnu", "GNU extension used"),
            ("extra-semi", "-Wextra-semi", "Extra semicolon after member function"),
            ("zero-length-array", "-Wzero-length-array", "ISO C forbids zero-size array"),
            ("vla", "-Wvla", "Variable length array used"),
            ("c++20-extensions", "-Wc++20-extensions", "C++20 extension used"),
            ("c++23-extensions", "-Wc++23-extensions", "C++23 extension used"),
            ("c++20-compat", "-Wc++20-compat", "C++ construct incompatible with C++20"),
            ("c++20-compat-pedantic", "-Wc++20-compat-pedantic", "C++ pedantic compat"),
            ("anonymous-struct", "-Wpedantic", "Anonymous struct is a GNU extension"),
            ("pointer-arith-void", "-Wpointer-arith", "Arithmetic on void* is a GNU ext"),
            ("four-char-constants", "-Wfour-char-constants", "Multi-character char constant"),
            ("dollar-in-identifier", "-Wdollar-in-identifier-extension", "$ in identifier"),
            ("keyword-macro", "-Wkeyword-macro", "Keyword used as macro name"),
            ("variadic-macros-pedantic", "-Wvariadic-macros", "Variadic macro pedantic"),
        ];
        for (name, flag, desc) in cats {
            let cat = DiagnosticCategory::new(name, flag, desc)
                .with_default_enabled(false)
                .with_parent("pedantic");
            self.categories.insert(name.to_string(), cat);
            self.group_to_members
                .entry("pedantic".to_string())
                .or_default()
                .push(name.to_string());
        }
    }

    // ── -Wconversion categories ────────────────────────────────────────────

    fn register_wconversion_categories(&mut self) {
        let cats = vec![
            ("conversion", "-Wconversion", "Implicit type conversion may alter value"),
            ("conversion-null", "-Wconversion-null", "Conversion of null pointer constant"),
            ("sign-conversion", "-Wsign-conversion", "Implicit sign conversion"),
            ("float-conversion", "-Wfloat-conversion", "Implicit float to int conversion"),
            ("shorten-64-to-32", "-Wshorten-64-to-32", "Implicit 64 to 32 bit shortening"),
            ("bitfield-conversion", "-Wbitfield-conversion", "Bit-field conversion truncation"),
            ("bool-conversion", "-Wbool-conversion", "Conversion to bool"),
            ("enum-conversion", "-Wenum-conversion", "Implicit enum conversion"),
            ("implicit-int-conversion", "-Wimplicit-int-conversion", "Implicit int truncation"),
            ("implicit-float-conversion", "-Wimplicit-float-conversion", "Implicit float truncation"),
            ("nullable-to-nonnull-conversion", "-Wnullable-to-nonnull-conversion", "Nullable to nonnull"),
            ("int-to-void-pointer-cast", "-Wint-to-void-pointer-cast", "Int to void* conversion"),
            ("implicit-fixed-point-conversion", "-Wimplicit-fixed-point-conversion", "Fixed point conv"),
        ];
        for (name, flag, desc) in cats {
            let cat = DiagnosticCategory::new(name, flag, desc)
                .with_default_enabled(false)
                .with_parent("conversion");
            self.categories.insert(name.to_string(), cat);
            self.group_to_members
                .entry("conversion".to_string())
                .or_default()
                .push(name.to_string());
        }
    }

    // ── -Wshadow categories ──────────────────────────────────────────────

    fn register_wshadow_categories(&mut self) {
        let cats = vec![
            ("shadow", "-Wshadow", "Local variable shadows another variable"),
            ("shadow-field", "-Wshadow-field", "Constructor parameter shadows field"),
            ("shadow-field-in-constructor", "-Wshadow-field-in-constructor", "Shadow field in ctor"),
            ("shadow-field-in-constructor-modified", "-Wshadow-field-in-constructor-modified", "Shadow field modified"),
            ("shadow-ivar", "-Wshadow-ivar", "Local shadows instance variable"),
            ("shadow-all", "-Wshadow-all", "All shadow warnings"),
            ("shadow-uncaptured-local", "-Wshadow-uncaptured-local", "Local could be captured"),
            ("shadow-field-in-nested-class", "-Wshadow-field-in-nested-class", "Field shadow nested"),
        ];
        for (name, flag, desc) in cats {
            let cat = DiagnosticCategory::new(name, flag, desc)
                .with_default_enabled(false)
                .with_parent("shadow");
            self.categories.insert(name.to_string(), cat);
            self.group_to_members
                .entry("shadow".to_string())
                .or_default()
                .push(name.to_string());
        }
    }

    fn register_wunused_categories(&mut self) {
        let cats = vec![
            ("unused-variable", "-Wunused-variable", "Unused variable"),
            ("unused-parameter", "-Wunused-parameter", "Unused parameter"),
            ("unused-function", "-Wunused-function", "Unused static function"),
            ("unused-label", "-Wunused-label", "Unused label"),
            ("unused-value", "-Wunused-value", "Expression result unused"),
            ("unused-result", "-Wunused-result", "Function result unused"),
            ("unused-local-typedef", "-Wunused-local-typedef", "Unused local typedef"),
            ("unused-private-field", "-Wunused-private-field", "Unused private field"),
            ("unused-lambda-capture", "-Wunused-lambda-capture", "Unused lambda capture"),
            ("unused-const-variable", "-Wunused-const-variable", "Unused const variable"),
            ("unused-but-set-variable", "-Wunused-but-set-variable", "Set but unused variable"),
            ("unused-but-set-parameter", "-Wunused-but-set-parameter", "Set but unused param"),
            ("unused-macro", "-Wunused-macro", "Unused macro definition"),
            ("unused-member-function", "-Wunused-member-function", "Unused member function"),
            ("unused-template", "-Wunused-template", "Unused template"),
            ("unused-exception-parameter", "-Wunused-exception-parameter", "Unused catch param"),
            ("unused-volatile", "-Wunused-volatile", "Unused volatile variable"),
            ("unused-import", "-Wunused-import", "Unused import declaration"),
        ];
        for (name, flag, desc) in cats {
            let cat = DiagnosticCategory::new(name, flag, desc)
                .with_default_enabled(name == "unused-variable")
                .with_parent("unused");
            self.categories.insert(name.to_string(), cat);
            self.group_to_members
                .entry("unused".to_string())
                .or_default()
                .push(name.to_string());
        }
    }

    fn register_wformat_categories(&mut self) {
        let cats = vec![
            ("format", "-Wformat", "printf/scanf format string validation"),
            ("format-security", "-Wformat-security", "Format string not a literal"),
            ("format-extra-args", "-Wformat-extra-args", "Extra arguments to format"),
            ("format-zero-length", "-Wformat-zero-length", "Zero-length format string"),
            ("format-nonliteral", "-Wformat-nonliteral", "Format string not a literal"),
            ("format-contains-nul", "-Wformat-contains-nul", "Format string contains NUL"),
            ("format-invalid-specifier", "-Wformat-invalid-specifier", "Invalid format specifier"),
            ("format-overflow", "-Wformat-overflow", "Format overflow at compile time"),
            ("format-pedantic", "-Wformat-pedantic", "Pedantic format checks"),
            ("format-y2k", "-Wformat-y2k", "Two-digit year in format"),
            ("format-nonstandard", "-Wformat-non-standard", "Non-standard format specifier"),
            ("format-truncation", "-Wformat-truncation", "Output may be truncated"),
            ("format-signedness", "-Wformat-signedness", "Sign mismatch in format arg"),
        ];
        for (name, flag, desc) in cats {
            let cat = DiagnosticCategory::new(name, flag, desc)
                .with_default_enabled(name == "format")
                .with_parent("format");
            self.categories.insert(name.to_string(), cat);
            self.group_to_members
                .entry("format".to_string())
                .or_default()
                .push(name.to_string());
        }
    }

    fn register_wsign_compare_categories(&mut self) {
        let cats = vec![
            ("sign-compare", "-Wsign-compare", "Signed/unsigned comparison"),
            ("sign-conversion", "-Wsign-conversion", "Implicit sign conversion"),
            ("sign-promo", "-Wsign-promo", "Overload resolution promotes unsigned"),
        ];
        for (name, flag, desc) in cats {
            let cat = DiagnosticCategory::new(name, flag, desc)
                .with_parent("sign-compare");
            self.categories.insert(name.to_string(), cat);
            self.group_to_members
                .entry("sign-compare".to_string())
                .or_default()
                .push(name.to_string());
        }
    }

    fn register_wswitch_categories(&mut self) {
        let cats = vec![
            ("switch", "-Wswitch", "Switch missing enum cases"),
            ("switch-enum", "-Wswitch-enum", "Switch missing named enum cases"),
            ("switch-default", "-Wswitch-default", "Switch missing default label"),
            ("switch-bool", "-Wswitch-bool", "Switch on boolean value"),
            ("switch-out-of-range", "-Wswitch-out-of-range", "Case value out of range"),
            ("switch-unreachable-default", "-Wswitch-unreachable-default", "Default unreachable"),
            ("covered-switch-default", "-Wcovered-switch-default", "All cases covered, default unused"),
        ];
        for (name, flag, desc) in cats {
            let cat = DiagnosticCategory::new(name, flag, desc)
                .with_default_enabled(name == "switch")
                .with_parent("switch");
            self.categories.insert(name.to_string(), cat);
            self.group_to_members
                .entry("switch".to_string())
                .or_default()
                .push(name.to_string());
        }
    }

    fn register_wtautological_categories(&mut self) {
        let cats = vec![
            ("tautological-compare", "-Wtautological-compare", "Self-comparison is tautological"),
            ("tautological-constant-compare", "-Wtautological-constant-compare", "Constant compare"),
            ("tautological-pointer-compare", "-Wtautological-pointer-compare", "Pointer compare"),
            ("tautological-overlap-compare", "-Wtautological-overlap-compare", "Overlap compare"),
            ("tautological-undefined-compare", "-Wtautological-undefined-compare", "UB compare"),
            ("tautological-type-limit-compare", "-Wtautological-type-limit-compare", "Type limit compare"),
            ("tautological-bitwise-compare", "-Wtautological-bitwise-compare", "Bitwise compare"),
            ("tautological-out-of-range", "-Wtautological-out-of-range", "Out of range compare"),
            ("tautological-unsigned-zero-compare", "-Wtautological-unsigned-zero-compare", "Unsigned >= 0"),
            ("tautological-unsigned-enum-zero-compare", "-Wtautological-unsigned-enum-zero-compare", "Unsigned enum >= 0"),
        ];
        for (name, flag, desc) in cats {
            let cat = DiagnosticCategory::new(name, flag, desc)
                .with_default_enabled(name == "tautological-compare")
                .with_parent("tautological");
            self.categories.insert(name.to_string(), cat);
            self.group_to_members
                .entry("tautological".to_string())
                .or_default()
                .push(name.to_string());
        }
    }

    fn register_wnull_dereference_categories(&mut self) {
        let cats = vec![
            ("null-dereference", "-Wnull-dereference", "Null pointer dereference"),
            ("null-arithmetic", "-Wnull-arithmetic", "Arithmetic on null pointer"),
            ("null-conversion", "-Wnull-conversion", "Null passed where non-null expected"),
            ("nonnull", "-Wnonnull", "Null passed to nonnull attribute parameter"),
            ("nonnull-compare", "-Wnonnull-compare", "Nonnull parameter compared to null"),
        ];
        for (name, flag, desc) in cats {
            let cat = DiagnosticCategory::new(name, flag, desc)
                .with_parent("null-dereference");
            self.categories.insert(name.to_string(), cat);
            self.group_to_members
                .entry("null-dereference".to_string())
                .or_default()
                .push(name.to_string());
        }
    }

    fn register_wreturn_type_categories(&mut self) {
        let cats = vec![
            ("return-type", "-Wreturn-type", "Non-void function without return"),
            ("return-local-addr", "-Wreturn-local-addr", "Returning address of local"),
            ("return-stack-address", "-Wreturn-stack-address", "Returning stack address"),
            ("return-std-move", "-Wreturn-std-move", "Unnecessary std::move in return"),
        ];
        for (name, flag, desc) in cats {
            let cat = DiagnosticCategory::new(name, flag, desc)
                .with_default_enabled(name == "return-type")
                .with_parent("return-type");
            self.categories.insert(name.to_string(), cat);
            self.group_to_members
                .entry("return-type".to_string())
                .or_default()
                .push(name.to_string());
        }
    }

    fn register_wuninitialized_categories(&mut self) {
        let cats = vec![
            ("uninitialized", "-Wuninitialized", "Use of uninitialized variable"),
            ("sometimes-uninitialized", "-Wsometimes-uninitialized", "Sometimes uninitialized"),
            ("conditional-uninitialized", "-Wconditional-uninitialized", "Conditionally uninit"),
            ("maybe-uninitialized", "-Wmaybe-uninitialized", "Possibly uninitialized"),
            ("static-self-init", "-Wstatic-self-init", "Static variable self-initialized"),
        ];
        for (name, flag, desc) in cats {
            let cat = DiagnosticCategory::new(name, flag, desc)
                .with_default_enabled(name == "uninitialized")
                .with_parent("uninitialized");
            self.categories.insert(name.to_string(), cat);
            self.group_to_members
                .entry("uninitialized".to_string())
                .or_default()
                .push(name.to_string());
        }
    }

    fn register_wparentheses_categories(&mut self) {
        let cats = vec![
            ("parentheses", "-Wparentheses", "Suggest parentheses in expression"),
            ("parentheses-equality", "-Wparentheses-equality", "= in conditional context"),
            ("bitwise-parentheses", "-Wbitwise-conditional-parentheses", "& or | in conditional"),
            ("logical-op-parentheses", "-Wlogical-op-parentheses", "&& within ||"),
            ("shift-op-parentheses", "-Wshift-op-parentheses", "Shift-parentheses"),
        ];
        for (name, flag, desc) in cats {
            let cat = DiagnosticCategory::new(name, flag, desc)
                .with_default_enabled(name == "parentheses")
                .with_parent("parentheses");
            self.categories.insert(name.to_string(), cat);
            self.group_to_members
                .entry("parentheses".to_string())
                .or_default()
                .push(name.to_string());
        }
    }

    fn register_wmisleading_indentation_categories(&mut self) {
        let cats = vec![
            ("misleading-indentation", "-Wmisleading-indentation", "Misleading indentation"),
        ];
        for (name, flag, desc) in cats {
            let cat = DiagnosticCategory::new(name, flag, desc)
                .with_default_enabled(true);
            self.categories.insert(name.to_string(), cat);
        }
    }

    fn register_wimplicit_fallthrough_categories(&mut self) {
        let cats = vec![
            ("implicit-fallthrough", "-Wimplicit-fallthrough", "Unannotated fallthrough"),
            ("implicit-fallthrough-per-function", "-Wimplicit-fallthrough-per-function",
                "Unannotated fallthrough per function"),
        ];
        for (name, flag, desc) in cats {
            let cat = DiagnosticCategory::new(name, flag, desc)
                .with_default_enabled(name == "implicit-fallthrough");
            self.categories.insert(name.to_string(), cat);
        }
    }

    fn register_wstrict_aliasing_categories(&mut self) {
        let cats = vec![
            ("strict-aliasing", "-Wstrict-aliasing", "Type-punning may violate aliasing"),
            ("strict-aliasing=0", "-Wstrict-aliasing=0", "Strict aliasing level 0"),
            ("strict-aliasing=1", "-Wstrict-aliasing=1", "Strict aliasing level 1"),
            ("strict-aliasing=2", "-Wstrict-aliasing=2", "Strict aliasing level 2"),
        ];
        for (name, flag, desc) in cats {
            let cat = DiagnosticCategory::new(name, flag, desc)
                .with_parent("strict-aliasing");
            self.categories.insert(name.to_string(), cat);
            self.group_to_members
                .entry("strict-aliasing".to_string())
                .or_default()
                .push(name.to_string());
        }
    }

    fn register_wcast_categories(&mut self) {
        let cats = vec![
            ("cast-align", "-Wcast-align", "Cast increases required alignment"),
            ("cast-qual", "-Wcast-qual", "Cast drops const/volatile qualifier"),
            ("bad-function-cast", "-Wbad-function-cast", "Function cast"),
            ("cast-function-type", "-Wcast-function-type", "Incompatible function cast"),
            ("old-style-cast", "-Wold-style-cast", "C-style cast used in C++"),
        ];
        for (name, flag, desc) in cats {
            let cat = DiagnosticCategory::new(name, flag, desc)
                .with_parent("cast");
            self.categories.insert(name.to_string(), cat);
            self.group_to_members
                .entry("cast".to_string())
                .or_default()
                .push(name.to_string());
        }
    }

    fn register_wdouble_promotion_categories(&mut self) {
        let cats = vec![
            ("double-promotion", "-Wdouble-promotion", "Implicit float to double promotion"),
        ];
        for (name, flag, desc) in cats {
            let cat = DiagnosticCategory::new(name, flag, desc)
                .with_default_enabled(false);
            self.categories.insert(name.to_string(), cat);
        }
    }

    fn register_wfloat_equal_categories(&mut self) {
        let cats = vec![
            ("float-equal", "-Wfloat-equal", "Floating-point equality comparison"),
        ];
        for (name, flag, desc) in cats {
            let cat = DiagnosticCategory::new(name, flag, desc)
                .with_default_enabled(false);
            self.categories.insert(name.to_string(), cat);
        }
    }

    fn register_wmissing_field_init_categories(&mut self) {
        let cats = vec![
            ("missing-field-initializers", "-Wmissing-field-initializers",
                "Missing initializer for field"),
            ("excess-initializers", "-Wexcess-initializers", "Too many initializers"),
            ("override-init", "-Woverride-init", "Initializer overrides previous"),
            ("designated-init", "-Wdesignated-init", "C99 designated initializer"),
        ];
        for (name, flag, desc) in cats {
            let cat = DiagnosticCategory::new(name, flag, desc)
                .with_parent("missing-field");
            self.categories.insert(name.to_string(), cat);
        }
    }

    fn register_wmissing_prototypes_categories(&mut self) {
        let cats = vec![
            ("missing-prototypes", "-Wmissing-prototypes", "No previous prototype for global function"),
            ("missing-declarations", "-Wmissing-declarations", "No previous declaration for global"),
        ];
        for (name, flag, desc) in cats {
            let cat = DiagnosticCategory::new(name, flag, desc)
                .with_default_enabled(false);
            self.categories.insert(name.to_string(), cat);
        }
    }

    fn register_wstrict_prototypes_categories(&mut self) {
        let cats = vec![
            ("strict-prototypes", "-Wstrict-prototypes", "Function declaration without prototype"),
        ];
        for (name, flag, desc) in cats {
            let cat = DiagnosticCategory::new(name, flag, desc)
                .with_default_enabled(false);
            self.categories.insert(name.to_string(), cat);
        }
    }

    fn register_wold_style_definition_categories(&mut self) {
        let cats = vec![
            ("old-style-definition", "-Wold-style-definition", "K&R-style function definition"),
        ];
        for (name, flag, desc) in cats {
            let cat = DiagnosticCategory::new(name, flag, desc)
                .with_default_enabled(false);
            self.categories.insert(name.to_string(), cat);
        }
    }

    fn register_wpointer_categories(&mut self) {
        let cats = vec![
            ("pointer-arith", "-Wpointer-arith", "Arithmetic on void* or function pointer"),
            ("pointer-sign", "-Wpointer-sign", "Pointer signedness mismatch in assignment"),
            ("pointer-to-int-cast", "-Wpointer-to-int-cast", "Pointer to integer cast"),
            ("int-to-pointer-cast", "-Wint-to-pointer-cast", "Integer to pointer cast"),
        ];
        for (name, flag, desc) in cats {
            let cat = DiagnosticCategory::new(name, flag, desc)
                .with_parent("pointer");
            self.categories.insert(name.to_string(), cat);
            self.group_to_members
                .entry("pointer".to_string())
                .or_default()
                .push(name.to_string());
        }
    }

    fn register_wunreachable_code_categories(&mut self) {
        let cats = vec![
            ("unreachable-code", "-Wunreachable-code", "Unreachable code"),
            ("unreachable-code-break", "-Wunreachable-code-break", "Unreachable break"),
            ("unreachable-code-return", "-Wunreachable-code-return", "Unreachable return"),
            ("unreachable-code-loop-increment", "-Wunreachable-code-loop-increment",
                "Unreachable loop increment"),
        ];
        for (name, flag, desc) in cats {
            let cat = DiagnosticCategory::new(name, flag, desc)
                .with_default_enabled(false)
                .with_parent("unreachable-code");
            self.categories.insert(name.to_string(), cat);
        }
    }

    fn register_wwrite_strings_categories(&mut self) {
        let cats = vec![
            ("write-strings", "-Wwrite-strings",
                "String literal conversion discards const qualifier"),
            ("c++11-compat-deprecated-writable-strings", "-Wc++11-compat-deprecated-writable-strings",
                "Writable string literals deprecated in C++11"),
        ];
        for (name, flag, desc) in cats {
            let cat = DiagnosticCategory::new(name, flag, desc)
                .with_default_enabled(false);
            self.categories.insert(name.to_string(), cat);
        }
    }

    // ── Syntax Error Categories ────────────────────────────────────────────

    fn register_syntax_error_categories(&mut self) {
        let cats = vec![
            ("expected-semi-after-expr", "-Werror", "Expected ';' after expression"),
            ("expected-semi-after-decl", "-Werror", "Expected ';' after declaration"),
            ("expected-expression", "-Werror", "Expected expression"),
            ("expected-type", "-Werror", "Expected type"),
            ("expected-identifier", "-Werror", "Expected identifier"),
            ("expected-rbrace", "-Werror", "Expected '}'"),
            ("expected-rparen", "-Werror", "Expected ')'"),
            ("expected-rbracket", "-Werror", "Expected ']'"),
            ("expected-comma", "-Werror", "Expected ','"),
            ("expected-colon", "-Werror", "Expected ':'"),
            ("expected-func-body", "-Werror", "Expected function body"),
            ("expected-statement", "-Werror", "Expected statement"),
            ("expected-declaration", "-Werror", "Expected declaration"),
            ("expected-parameter", "-Werror", "Expected parameter declaration"),
            ("expected-template-arg", "-Werror", "Expected template argument"),
            ("expected-qualified-name", "-Werror", "Expected qualified name"),
            ("expected-class-name", "-Werror", "Expected class name"),
            ("expected-enum-name", "-Werror", "Expected enum name"),
            ("expected-constant-expr", "-Werror", "Expected constant expression"),
            ("expected-integer-literal", "-Werror", "Expected integer literal"),
            ("expected-string-literal", "-Werror", "Expected string literal"),
            ("expected-initializer", "-Werror", "Expected initializer"),
            ("expected-equal", "-Werror", "Expected '='"),
            ("missing-token", "-Werror", "Missing token"),
            ("extraneous-token", "-Werror", "Extraneous token before end of statement"),
            ("unexpected-token", "-Werror", "Unexpected token"),
            ("mismatched-delimiter", "-Werror", "Mismatched delimiter"),
            ("invalid-suffix", "-Werror", "Invalid suffix on literal"),
            ("invalid-digit", "-Werror", "Invalid digit in numeric constant"),
            ("duplicate-decl-specifier", "-Werror", "Duplicate declaration specifier"),
            ("duplicate-member", "-Werror", "Duplicate member name"),
        ];
        for (name, flag, desc) in cats {
            let cat = DiagnosticCategory::new(name, flag, desc)
                .with_parent("syntax-error");
            self.categories.insert(name.to_string(), cat);
            self.group_to_members
                .entry("syntax-error".to_string())
                .or_default()
                .push(name.to_string());
        }
    }

    // ── Type Error Categories ────────────────────────────────────────────

    fn register_type_error_categories(&mut self) {
        let cats = vec![
            ("undeclared-identifier", "-Werror", "Use of undeclared identifier"),
            ("undeclared-var-use", "-Werror", "Use of undeclared variable"),
            ("undeclared-function", "-Werror", "Call to undeclared function"),
            ("undeclared-type", "-Werror", "Unknown type name"),
            ("redefinition", "-Werror", "Redefinition of identifier"),
            ("redefinition-different-type", "-Werror", "Redefinition with different type"),
            ("redefinition-of-label", "-Werror", "Redefinition of label"),
            ("redefinition-of-enumerator", "-Werror", "Redefinition of enumerator"),
            ("redefinition-of-typedef", "-Werror", "Redefinition of typedef"),
            ("conflicting-types", "-Werror", "Conflicting type declarations"),
            ("ambiguous-reference", "-Werror", "Ambiguous reference"),
            ("cannot-initialize", "-Werror", "Cannot initialize variable"),
            ("cannot-convert", "-Werror", "Cannot convert between types"),
            ("cannot-convert-to-pointer", "-Werror", "Cannot convert to pointer type"),
            ("cannot-convert-to-reference", "-Werror", "Cannot convert to reference type"),
            ("cannot-bind-bitfield", "-Werror", "Cannot bind non-const ref to bit-field"),
            ("cannot-bind-to-temporary", "-Werror", "Cannot bind to temporary"),
            ("incompatible-types", "-Werror", "Incompatible types"),
            ("incompatible-pointer-types", "-Werror", "Incompatible pointer types"),
            ("invalid-operand-types", "-Werror", "Invalid operand types for operator"),
            ("no-viable-conversion", "-Werror", "No viable conversion"),
            ("abstract-class", "-Werror", "Cannot instantiate abstract class"),
            ("invalid-use-of-incomplete-type", "-Werror", "Use of incomplete type"),
            ("field-has-incomplete-type", "-Werror", "Field has incomplete type"),
            ("arithmetic-on-void-ptr", "-Werror", "Arithmetic on pointer to void"),
            ("assignment-to-const", "-Werror", "Cannot assign to const variable"),
            ("assignment-to-array", "-Werror", "Cannot assign to array"),
            ("member-ref-on-non-struct", "-Werror", "Member reference on non-struct type"),
            ("invalid-use-of-this", "-Werror", "Invalid use of 'this'"),
            ("invalid-use-of-void", "-Werror", "Invalid use of void expression"),
            ("invalid-array-size", "-Werror", "Invalid array size"),
            ("negative-array-size", "-Werror", "Array size is negative"),
            ("zero-size-array", "-Werror", "Zero-size array not allowed in ISO C"),
            ("flexible-array-member", "-Werror", "Flexible array member not at end"),
            ("void-var-decl", "-Werror", "Variable has incomplete type 'void'"),
        ];
        for (name, flag, desc) in cats {
            let cat = DiagnosticCategory::new(name, flag, desc)
                .with_parent("type-error");
            self.categories.insert(name.to_string(), cat);
            self.group_to_members
                .entry("type-error".to_string())
                .or_default()
                .push(name.to_string());
        }
    }

    // ── Semantic Error Categories ────────────────────────────────────────

    fn register_semantic_error_categories(&mut self) {
        let cats = vec![
            ("no-matching-function", "-Werror", "No matching function for call"),
            ("no-matching-member-function", "-Werror", "No matching member function"),
            ("no-matching-constructor", "-Werror", "No matching constructor"),
            ("too-many-args", "-Werror", "Too many arguments to function call"),
            ("too-few-args", "-Werror", "Too few arguments to function call"),
            ("call-to-deleted", "-Werror", "Call to deleted function"),
            ("call-to-inaccessible", "-Werror", "Call to inaccessible member"),
            ("call-to-pure-virtual", "-Werror", "Call to pure virtual function"),
            ("call-to-non-function", "-Werror", "Called object is not a function"),
            ("default-arg-mismatch", "-Werror", "Default argument mismatch"),
            ("access-private", "-Werror", "Inaccessible private member"),
            ("access-protected", "-Werror", "Inaccessible protected member"),
            ("inaccessible-base", "-Werror", "Inaccessible base class"),
            ("override-mismatch", "-Werror", "Function marked override does not override"),
            ("final-override", "-Werror", "Cannot override final function"),
            ("constexpr-body-no-return", "-Werror", "constexpr body has no return stmt"),
            ("constexpr-var-requires-init", "-Werror", "constexpr variable needs init"),
            ("static-assert-failed", "-Werror", "Static assertion failed"),
            ("duplicate-case", "-Werror", "Duplicate case value in switch"),
            ("duplicate-default", "-Werror", "Multiple default labels in switch"),
            ("duplicate-base", "-Werror", "Duplicate base class"),
            ("multiple-storage-classes", "-Werror", "Multiple storage classes"),
            ("thread-non-static", "-Werror", "thread_local on non-static variable"),
            ("constexpr-never-constant", "-Werror", "Constexpr function never constant"),
        ];
        for (name, flag, desc) in cats {
            let cat = DiagnosticCategory::new(name, flag, desc)
                .with_parent("semantic-error");
            self.categories.insert(name.to_string(), cat);
            self.group_to_members
                .entry("semantic-error".to_string())
                .or_default()
                .push(name.to_string());
        }
    }

    // ── Linker Error Categories ────────────────────────────────────────────

    fn register_linker_error_categories(&mut self) {
        let cats = vec![
            ("undefined-reference", "-Werror", "Undefined reference to symbol"),
            ("duplicate-symbol", "-Werror", "Duplicate symbol definition"),
            ("unresolved-symbol", "-Werror", "Unresolved external symbol"),
            ("mismatched-visibility", "-Werror", "Symbol visibility mismatch"),
            ("missing-entry-point", "-Werror", "Missing entry point (main)"),
            ("incompatible-object-format", "-Werror", "Incompatible object file format"),
            ("wrong-architecture", "-Werror", "Object file built for wrong architecture"),
            ("missing-library", "-Werror", "Cannot find library"),
            ("missing-dso-needed", "-Werror", "Shared library referenced but not linked"),
            ("circular-dependency", "-Werror", "Circular library dependency"),
            ("version-mismatch", "-Werror", "Library ABI version mismatch"),
            ("weak-symbol-override", "-Werror", "Weak symbol overridden multiple times"),
            ("common-symbol-size-mismatch", "-Werror", "Common symbol size mismatch"),
        ];
        for (name, flag, desc) in cats {
            let cat = DiagnosticCategory::new(name, flag, desc)
                .with_parent("linker-error");
            self.categories.insert(name.to_string(), cat);
            self.group_to_members
                .entry("linker-error".to_string())
                .or_default()
                .push(name.to_string());
        }
    }

    // ── Preprocessor Error Categories ──────────────────────────────────────

    fn register_preprocessor_error_categories(&mut self) {
        let cats = vec![
            ("pp-hash-error", "-Werror", "#error directive encountered"),
            ("pp-include-not-found", "-Werror", "Cannot open include file"),
            ("pp-include-too-deep", "-Werror", "Include nested too deeply"),
            ("pp-macro-redefined", "-Werror", "Macro redefined"),
            ("pp-macro-not-defined", "-Werror", "Macro not defined for #undef"),
            ("pp-unterminated-comment", "-Werror", "Unterminated /* comment"),
            ("pp-unterminated-string", "-Werror", "Missing terminating \" character"),
            ("pp-unterminated-char", "-Werror", "Missing terminating ' character"),
            ("pp-invalid-token", "-Werror", "Invalid preprocessing token"),
            ("pp-nested-comment", "-Werror", "'/*' within block comment"),
            ("pp-bad-paste", "-Werror", "Pasting formed invalid token"),
            ("pp-bad-stringify", "-Werror", "# operator not followed by macro param"),
            ("pp-extra-endif", "-Werror", "#endif without #if"),
            ("pp-missing-endif", "-Werror", "Unterminated #if/#ifdef/#ifndef"),
            ("pp-else-after-else", "-Werror", "#else after #else"),
            ("pp-elif-after-else", "-Werror", "#elif after #else"),
            ("pp-empty-file-name", "-Werror", "Empty file name in #include"),
            ("pp-invalid-directive", "-Werror", "Invalid preprocessing directive"),
            ("pp-nonportable-path", "-Werror", "Non-portable path in #include"),
        ];
        for (name, flag, desc) in cats {
            let cat = DiagnosticCategory::new(name, flag, desc)
                .with_parent("preprocessor-error");
            self.categories.insert(name.to_string(), cat);
            self.group_to_members
                .entry("preprocessor-error".to_string())
                .or_default()
                .push(name.to_string());
        }
    }

    // ── Template Error Categories ───────────────────────────────────────

    fn register_template_error_categories(&mut self) {
        let cats = vec![
            ("template-arg-deduction-failed", "-Werror", "Template argument deduction failed"),
            ("cannot-deduce-template-args", "-Werror", "Cannot deduce template arguments"),
            ("ambiguous-template-args", "-Werror", "Ambiguous template arguments"),
            ("recursive-template", "-Werror", "Recursive template instantiation"),
            ("template-depth-exceeded", "-Werror", "Template instantiation depth exceeded"),
            ("incomplete-template-def", "-Werror", "Template definition not visible"),
        ];
        for (name, flag, desc) in cats {
            let cat = DiagnosticCategory::new(name, flag, desc)
                .with_parent("template-error");
            self.categories.insert(name.to_string(), cat);
            self.group_to_members
                .entry("template-error".to_string())
                .or_default()
                .push(name.to_string());
        }
    }

    // ── Constraint Error Categories ──────────────────────────────────────

    fn register_constraint_error_categories(&mut self) {
        let cats = vec![
            ("constraint-not-satisfied", "-Werror", "Constraint not satisfied"),
            ("concept-check-failed", "-Werror", "Concept check failed"),
            ("requires-clause-violated", "-Werror", "Requires clause violated"),
        ];
        for (name, flag, desc) in cats {
            let cat = DiagnosticCategory::new(name, flag, desc)
                .with_parent("constraint-error");
            self.categories.insert(name.to_string(), cat);
        }
    }

    // ── Note Categories ─────────────────────────────────────────────────

    fn register_note_categories(&mut self) {
        let cats = vec![
            ("previous-definition", "-Wnote", "Previous definition is here"),
            ("previous-declaration", "-Wnote", "Previous declaration is here"),
            ("previous-builtin-declaration", "-Wnote", "Built-in declaration is here"),
            ("candidate-function", "-Wnote", "Candidate function not viable"),
            ("candidate-constructor", "-Wnote", "Candidate constructor not viable"),
            ("candidate-template", "-Wnote", "Candidate template ignored"),
            ("forward-declaration", "-Wnote", "Forward declaration of X is here"),
            ("declaration-here", "-Wnote", "Declaration here"),
            ("definition-here", "-Wnote", "Definition is here"),
            ("in-instantiation-of", "-Wnote", "In instantiation of template"),
            ("in-expansion-of-macro", "-Wnote", "In expansion of macro"),
            ("in-substitution-of", "-Wnote", "In substitution of template args"),
            ("while-deducing-template-args", "-Wnote", "While deducing template args"),
            ("macro-defined-here", "-Wnote", "Macro defined here"),
            ("macro-expanded-here", "-Wnote", "Macro expanded from here"),
            ("preexpanded-from-macro", "-Wnote", "Expanded from macro"),
            ("template-declared-here", "-Wnote", "Template declared here"),
            ("specialization-here", "-Wnote", "Specialization declared here"),
            ("parameter-here", "-Wnote", "Parameter declared here"),
            ("base-class-here", "-Wnote", "Base class declared here"),
            ("member-declared-here", "-Wnote", "Member declared here"),
            ("type-mismatch", "-Wnote", "Type mismatch: expected X, got Y"),
            ("value-here", "-Wnote", "Value is here"),
            ("uninitialized-here", "-Wnote", "Variable may be uninitialized when used here"),
            ("assigned-here", "-Wnote", "Variable assigned here"),
            ("condition-here", "-Wnote", "Condition evaluated here"),
            ("loop-here", "-Wnote", "Loop body is here"),
            ("field-declared-here", "-Wnote", "Field declared here"),
            ("enum-declared-here", "-Wnote", "Enum declared here"),
            ("typedef-here", "-Wnote", "Typedef declared here"),
            ("namespace-here", "-Wnote", "Namespace declared here"),
            ("conversion-candidate", "-Wnote", "Conversion candidate"),
            ("return-type-here", "-Wnote", "Return type context"),
            ("note-in-evaluating-expr", "-Wnote", "In evaluation of constant expression"),
            ("note-sfinae-explanation", "-Wnote", "SFINAE explanation"),
            ("note-module-import", "-Wnote", "Module imported here"),
        ];
        for (name, flag, desc) in cats {
            let cat = DiagnosticCategory::new(name, flag, desc);
            self.categories.insert(name.to_string(), cat);
        }
    }

    // ── Fix-It Categories ──────────────────────────────────────────────

    fn register_fixit_categories(&mut self) {
        let cats = vec![
            ("fixit-insert-semicolon", "-Wfixit", "Insert ';'"),
            ("fixit-insert-rbrace", "-Wfixit", "Insert '}'"),
            ("fixit-insert-rparen", "-Wfixit", "Insert ')'"),
            ("fixit-insert-rbracket", "-Wfixit", "Insert ']'"),
            ("fixit-insert-cast", "-Wfixit", "Insert cast"),
            ("fixit-insert-const", "-Wfixit", "Insert const qualifier"),
            ("fixit-insert-static", "-Wfixit", "Insert static"),
            ("fixit-replace-identifier", "-Wfixit", "Replace identifier"),
            ("fixit-replace-assign-with-compare", "-Wfixit", "Replace '=' with '=='"),
            ("fixit-insert-missing-include", "-Wfixit", "Insert #include directive"),
            ("fixit-insert-typename", "-Wfixit", "Insert 'typename' keyword"),
            ("fixit-remove-token", "-Wfixit", "Remove extraneous token"),
            ("fixit-wrap-in-parens", "-Wfixit", "Wrap expression in parentheses"),
            ("fixit-insert-braces", "-Wfixit", "Insert braces around statement"),
            ("fixit-insert-null-check", "-Wfixit", "Insert null check"),
            ("fixit-delete-keyword", "-Wfixit", "Delete keyword to resolve ambiguity"),
            ("fixit-change-type", "-Wfixit", "Change type to resolve conversion"),
        ];
        for (name, flag, desc) in cats {
            let cat = DiagnosticCategory::new(name, flag, desc);
            self.categories.insert(name.to_string(), cat);
        }
    }

    /// Look up all categories that include a given diagnostic ID.
    pub fn categories_for(&self, id: DiagID) -> Vec<&str> {
        self.id_to_category
            .get(&id)
            .map(|v| v.iter().map(|s| s.as_str()).collect())
            .unwrap_or_default()
    }

    /// Get the total number of registered categories.
    pub fn len(&self) -> usize {
        self.categories.len()
    }

    pub fn is_empty(&self) -> bool {
        self.categories.is_empty()
    }
}

impl Default for DiagnosticCategoryRegistry {
    fn default() -> Self {
        Self::new()
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// Complete Error Message Catalog (300+ messages)
// ═══════════════════════════════════════════════════════════════════════════════

/// Full catalog of human-readable diagnostic messages for every DiagID variant.
pub struct FullDiagnosticMessageCatalog;

impl FullDiagnosticMessageCatalog {
    /// Returns the full human-readable message template for a given diagnostic ID.
    pub fn message_for(id: DiagID) -> &'static str {
        match id {
            // ── Syntax Errors ─────────────────────────────────────────────
            DiagID::ErrExpectedSemiAfterExpr => "expected ';' after expression",
            DiagID::ErrExpectedSemiAfterDecl => "expected ';' after declaration",
            DiagID::ErrExpectedExpression => "expected expression",
            DiagID::ErrExpectedType => "expected type",
            DiagID::ErrExpectedIdentifier => "expected identifier",
            DiagID::ErrExpectedRBrace => "expected '}'",
            DiagID::ErrExpectedRParen => "expected ')'",
            DiagID::ErrExpectedRBracket => "expected ']'",
            DiagID::ErrExpectedComma => "expected ','",
            DiagID::ErrExpectedColon => "expected ':'",
            DiagID::ErrExpectedFuncBody => "expected function body after function declarator",
            DiagID::ErrExpectedStatement => "expected statement",
            DiagID::ErrExpectedDeclaration => "expected declaration",
            DiagID::ErrExpectedParameter => "expected parameter declarator",
            DiagID::ErrExpectedTemplateArg => "expected template argument",
            DiagID::ErrExpectedQualifiedName => "expected qualified name",
            DiagID::ErrExpectedNamespaceName => "expected namespace name",
            DiagID::ErrExpectedClassName => "expected class name",
            DiagID::ErrExpectedEnumName => "expected enum name",
            DiagID::ErrExpectedConstantExpression => "expected constant expression",
            DiagID::ErrExpectedStringLiteral => "expected string literal",
            DiagID::ErrExpectedIntegerLiteral => "expected integer literal",
            DiagID::ErrExpectedFloatingLiteral => "expected floating-point literal",
            DiagID::ErrExpectedCharLiteral => "expected character literal",
            DiagID::ErrExpectedBooleanLiteral => "expected boolean literal",
            DiagID::ErrExpectedNullPtr => "expected null pointer literal",
            DiagID::ErrExpectedThis => "expected 'this'",
            DiagID::ErrExpectedBaseSpecifier => "expected base specifier",
            DiagID::ErrExpectedMemberName => "expected member name or ';' after declaration",
            DiagID::ErrExpectedInitializer => "expected initializer",
            DiagID::ErrExpectedEqual => "expected '='",
            DiagID::ErrExpectedArrow => "expected '->'",
            DiagID::ErrExpectedDot => "expected '.'",
            DiagID::ErrExpectedStar => "expected '*'",
            DiagID::ErrExpectedAmpersand => "expected '&'",
            DiagID::ErrExpectedOperator => "expected operator",
            DiagID::ErrExpectedLoopBody => "expected loop body",
            DiagID::ErrExpectedSwitchBody => "expected switch body",
            DiagID::ErrExpectedCaseOrDefault => "expected 'case' or 'default'",
            DiagID::ErrExpectedCatch => "expected 'catch'",

            // ── Undeclared/Redefinition Errors ────────────────────────────
            DiagID::ErrUndeclaredIdentifier => "use of undeclared identifier '{}'",
            DiagID::ErrUndeclaredVarUse => "use of undeclared variable '{}'",
            DiagID::ErrUndeclaredFunction => "call to undeclared function '{}'; ISO C99 and later do not support implicit function declarations",
            DiagID::ErrUndeclaredType => "unknown type name '{}'",
            DiagID::ErrUndeclaredLabel => "use of undeclared label '{}'",
            DiagID::ErrUndeclaredNamespace => "no member named '{}' in namespace '{}'",
            DiagID::ErrUndeclaredTemplate => "use of undeclared template '{}'",
            DiagID::ErrRedefinition => "redefinition of '{}'",
            DiagID::ErrRedefinitionDifferentType => "redefinition of '{}' with a different type: '{}' vs '{}'",
            DiagID::ErrRedefinitionOfLabel => "redefinition of label '{}'",
            DiagID::ErrRedefinitionOfEnumerator => "redefinition of enumerator '{}'",
            DiagID::ErrRedefinitionOfTypedef => "redefinition of '{}' as different kind of symbol",
            DiagID::ErrRedefinitionOfModule => "redefinition of module '{}'",
            DiagID::ErrRedeclaration => "redeclaration of '{}'",
            DiagID::ErrRedeclarationDifferentKind => "redeclaration of '{}' as different kind of symbol",
            DiagID::ErrConflictTypes => "conflicting types for '{}'",
            DiagID::ErrAmbiguousReference => "reference to '{}' is ambiguous",
            DiagID::ErrAmbiguousOverload => "call to '{}' is ambiguous",
            DiagID::ErrAmbiguousConversion => "ambiguous conversion from '{}' to '{}'",
            DiagID::ErrAmbiguousBase => "ambiguous conversion from derived class '{}' to base class '{}'",

            // ── Type/Conversion Errors ────────────────────────────────────
            DiagID::ErrCannotInitialize => "cannot initialize a variable of type '{}' with an lvalue of type '{}'",
            DiagID::ErrCannotInitializeWithInitList => "cannot initialize with an initializer list",
            DiagID::ErrCannotConvert => "cannot convert '{}' to '{}' without a conversion operator",
            DiagID::ErrCannotConvertToPointer => "cannot convert '{}' to pointer type '{}'",
            DiagID::ErrCannotConvertToReference => "cannot convert '{}' to reference type '{}'",
            DiagID::ErrCannotConvertFunctionToPointer => "cannot convert function type '{}' to '{}'",
            DiagID::ErrCannotConvertVectorType => "cannot convert between vector values of different size",
            DiagID::ErrCannotBindBitfield => "cannot bind non-const lvalue reference to a bit-field",
            DiagID::ErrCannotBindToTemporary => "cannot bind non-const lvalue reference of type '{}' to a temporary of type '{}'",
            DiagID::ErrCannotPassNonTriviallyCopyable => "cannot pass object of non-trivially-copyable type '{}' through variadic function",
            DiagID::ErrCannotCatchType => "cannot catch '{}' by value because it is abstract",
            DiagID::ErrCannotThrowType => "cannot throw object of incomplete type '{}'",
            DiagID::ErrIncompatibleTypes => "incompatible types: assigning to '{}' from '{}'",
            DiagID::ErrIncompatiblePointerTypes => "incompatible pointer types passing '{}' to parameter of type '{}'",
            DiagID::ErrIncompatibleIntegerConversion => "incompatible integer to pointer conversion passing '{}' to parameter of type '{}'",
            DiagID::ErrIncompatiblePointerToInteger => "incompatible pointer to integer conversion assigning to '{}' from '{}'",
            DiagID::ErrIncompatibleOperandTypes => "invalid operands to binary expression ('{}' and '{}')",
            DiagID::ErrIncompatibleVectorTypes => "incompatible vector types",
            DiagID::ErrIncompatibleBlockPointer => "incompatible block pointer types",

            // ── Function Call Errors ──────────────────────────────────────
            DiagID::ErrNoMatchingFunction => "no matching function for call to '{}'",
            DiagID::ErrNoMatchingMemberFunction => "no matching member function for call to '{}'",
            DiagID::ErrNoMatchingConstructor => "no matching constructor for initialization of '{}'",
            DiagID::ErrNoMatchingConversion => "no matching conversion for '{}'",
            DiagID::ErrTooManyArgs => "too many arguments to function call, expected {}, have {}",
            DiagID::ErrTooFewArgs => "too few arguments to function call, expected {}, have {}",
            DiagID::ErrCallToDeleted => "call to deleted function '{}'",
            DiagID::ErrCallToInaccessible => "calling '{}' is inaccessible",
            DiagID::ErrCallToPureVirtual => "call to pure virtual member function '{}'",
            DiagID::ErrCallToNonCallable => "called object type '{}' is not a function or function pointer",
            DiagID::ErrCallToNonFunction => "called object is not a function or function pointer",
            DiagID::ErrDefaultArgMismatch => "default argument mismatch",
            DiagID::ErrTemplateArgDeductionFailed => "template argument deduction failed for '{}'",
            DiagID::ErrCannotDeduceTemplateArgs => "cannot deduce template arguments for '{}'",
            DiagID::ErrAmbiguousTemplateArgs => "ambiguous template arguments for '{}'",
            DiagID::ErrNoViableConversion => "no viable conversion from '{}' to '{}'",
            DiagID::ErrInvalidExplicitSpecifier => "invalid explicit specifier",
            DiagID::ErrRecursiveTemplateInstantiation => "recursive template instantiation exceeded maximum depth of {}",

            // ── Expression Errors ─────────────────────────────────────────
            DiagID::ErrUnaryAmpersandOnTemporary => "taking the address of a temporary object of type '{}'",
            DiagID::ErrUnaryAmpersandOnBitfield => "taking the address of a bit-field",
            DiagID::ErrUnaryAmpersandOnRegister => "taking the address of a register variable is not allowed",
            DiagID::ErrDivisionByZeroConst => "division by zero in constant expression",
            DiagID::ErrShiftByNegative => "shift count is negative",
            DiagID::ErrShiftByExcess => "shift count >= width of type",
            DiagID::ErrModuloByZero => "remainder by zero in constant expression",
            DiagID::ErrAssignmentToConst => "cannot assign to variable with const-qualified type '{}'",
            DiagID::ErrAssignmentToArray => "array type '{}' is not assignable",
            DiagID::ErrAssignmentToFunction => "cannot assign to a function",
            DiagID::ErrIncrementOfBoolean => "ISO C++17 does not allow incrementing expression of type bool",
            DiagID::ErrIncrementOfEnum => "cannot increment value of type '{}'",
            DiagID::ErrIncrementOfReadOnly => "cannot increment a read-only reference",
            DiagID::ErrDecrementOfBoolean => "ISO C++17 does not allow decrementing expression of type bool",
            DiagID::ErrArithmeticOnVoidPtr => "arithmetic on a pointer to void",
            DiagID::ErrArithmeticOnFunctionPtr => "arithmetic on a pointer to function",
            DiagID::ErrSubscriptOnArray => "subscript of array type '{}'",
            DiagID::ErrSubscriptOnPointer => "subscript of pointer to incomplete type '{}'",
            DiagID::ErrSubscriptOnNonArray => "subscripted value is not an array, pointer, or vector",
            DiagID::ErrMemberRefOnNonStruct => "member reference base type '{}' is not a structure or union",
            DiagID::ErrMemberRefOnNonClass => "member reference type '{}' is not a pointer",

            // ── Access/Inheritance Errors ─────────────────────────────────
            DiagID::ErrAbstractClass => "cannot instantiate abstract class '{}'",
            DiagID::ErrAbstractDecl => "cannot declare variable of abstract type '{}'",
            DiagID::ErrPureVirtualCall => "pure virtual function '{}' called",
            DiagID::ErrPrivateMember => "'{}' is a private member of '{}'",
            DiagID::ErrProtectedMember => "'{}' is a protected member of '{}'",
            DiagID::ErrInaccessibleBase => "inaccessible base class '{}' in conversion",
            DiagID::ErrAmbiguousBaseClass => "ambiguous conversion from '{}' to '{}'",
            DiagID::ErrVirtualBaseClass => "virtual base class '{}' cannot be directly constructed",
            DiagID::ErrOverrideMismatch => "'{}' marked 'override' but does not override any member",
            DiagID::ErrFinalOverride => "cannot override 'final' function '{}'",
            DiagID::ErrMissingVtable => "undefined reference to vtable for '{}'",
            DiagID::ErrMissingTypeinfo => "undefined reference to typeinfo for '{}'",
            DiagID::ErrInvalidUseOfThis => "invalid use of 'this' outside of a non-static member function",
            DiagID::ErrInvalidUseOfMember => "invalid use of non-static data member '{}'",
            DiagID::ErrInvalidUseOfTemplate => "invalid use of template name without argument list",
            DiagID::ErrInvalidUseOfAuto => "invalid use of 'auto'",
            DiagID::ErrInvalidUseOfDecltype => "invalid use of 'decltype'",
            DiagID::ErrInvalidUseOfVoid => "invalid use of void expression",
            DiagID::ErrInvalidUseOfIncompleteType => "invalid use of incomplete type '{}'",
            DiagID::ErrFieldOfIncompleteType => "field has incomplete type '{}'",
            DiagID::ErrSizeOfIncompleteType => "invalid application of 'sizeof' to an incomplete type '{}'",
            DiagID::ErrAlignOfIncompleteType => "invalid application of 'alignof' to an incomplete type '{}'",
            DiagID::ErrDeleteOfIncompleteType => "deletion of pointer to incomplete type '{}'",
            DiagID::ErrDeleteOfAbstractClass => "deletion of abstract class '{}' is not allowed",
            DiagID::ErrNewOfAbstractClass => "cannot allocate an object of abstract type '{}'",
            DiagID::ErrThrowOfIncompleteType => "cannot throw object of incomplete type '{}'",
            DiagID::ErrCatchOfIncompleteType => "cannot catch incomplete type '{}'",
            DiagID::ErrReturnOfIncompleteType => "cannot return incomplete type '{}' from function",
            DiagID::ErrParamOfIncompleteType => "parameter has incomplete type '{}'",

            // ── Preprocessor Errors ───────────────────────────────────────
            DiagID::ErrPPHashError => "{}",
            DiagID::ErrPPExpectedNewline => "expected newline at end of directive",
            DiagID::ErrPPExpectedIdentifier => "expected identifier in directive",
            DiagID::ErrPPExpectedValue => "expected value in expression",
            DiagID::ErrPPExpectedInclude => "expected \"FILENAME\" or <FILENAME>",
            DiagID::ErrPPExpectedConditional => "expected expression in preprocessor conditional",
            DiagID::ErrPPExpectedEndif => "expected #endif",
            DiagID::ErrPPIncludeNotFound => "'{}' file not found",
            DiagID::ErrPPIncludeTooDeep => "#include nested too deeply",
            DiagID::ErrPPMacroRedefined => "'{}' macro redefined",
            DiagID::ErrPPMacroNotDefined => "'{}' is not defined, evaluates to 0",
            DiagID::ErrPPMacroExpansionTooDeep => "macro expansion too deeply nested",
            DiagID::ErrPPInvalidToken => "invalid preprocessing token",
            DiagID::ErrPPUnterminatedComment => "unterminated /* comment",
            DiagID::ErrPPUnterminatedString => "missing terminating '\"' character",
            DiagID::ErrPPUnterminatedChar => "missing terminating '\\'' character",
            DiagID::ErrPPDirectiveInMacroArg => "directive in macro argument list",
            DiagID::ErrPPEmptyFileName => "empty filename in #include directive",
            DiagID::ErrPPInvalidDirective => "invalid preprocessing directive",
            DiagID::ErrPPNestedComment => "'/*' within block comment",
            DiagID::ErrPPBadPaste => "pasting formed '{}', an invalid preprocessing token",
            DiagID::ErrPPBadStringify => "'#' is not followed by a macro parameter",
            DiagID::ErrPPVariadicMacro => "ISO C++11 requires at least one argument for the '...' in a variadic macro",
            DiagID::ErrPPExpectedFeatureName => "expected feature name",
            DiagID::ErrPPDefinedInMacro => "operator 'defined' requires an identifier",
            DiagID::ErrPPLineDirective => "#line directive requires a simple digit sequence",
            DiagID::ErrPPPragmaExpected => "expected string literal in #pragma directive",
            DiagID::ErrPPPragmaUnsupported => "unsupported #pragma '{}'",
            DiagID::ErrPPMissingEndif => "unterminated conditional directive",
            DiagID::ErrPPExtraEndif => "#endif without #if",
            DiagID::ErrPPElseAfterElse => "#else after #else",
            DiagID::ErrPPElifAfterElse => "#elif after #else",
            DiagID::ErrPPNonPortablePath => "non-portable path to file '{}'",
            DiagID::ErrPPIncludeNextOutsideHeader => "#include_next in primary source file",
            DiagID::ErrPPImportInMacro => "import of module '{}' in macro body",

            // ── Static Analysis / Constexpr Errors ────────────────────────
            DiagID::ErrStaticAssert => "static assertion failed",
            DiagID::ErrStaticAssertFailed => "static assertion failed: {}",
            DiagID::ErrInvalidArraySize => "invalid array size",
            DiagID::ErrNegativeArraySize => "array size is negative",
            DiagID::ErrZeroSizeArray => "zero size arrays are an extension",
            DiagID::ErrFlexibleArrayMember => "flexible array member '{}' not at end of struct",
            DiagID::ErrFlexibleArrayInUnion => "flexible array member '{}' in a union is not allowed",
            DiagID::ErrFlexibleArrayInEmptyStruct => "flexible array member in an otherwise empty struct",
            DiagID::ErrConstexprBodyNoReturn => "constexpr function never produces a constant expression",
            DiagID::ErrConstexprFunctionNeverConstant => "constexpr function's return type '{}' is not a literal type",
            DiagID::ErrConstexprVarRequiresInit => "default initialization of an object of const type '{}' without a user-provided default constructor",
            DiagID::ErrConstexprVarRequiresConstInit => "constexpr variable '{}' must be initialized by a constant expression",
            DiagID::ErrDuplicateMember => "duplicate member '{}'",
            DiagID::ErrDuplicateBase => "duplicate base class '{}'",
            DiagID::ErrDuplicateCase => "duplicate case value '{}'",
            DiagID::ErrDuplicateDefault => "multiple default labels in switch statement",
            DiagID::ErrDuplicateAttribute => "duplicate attribute '{}'",
            DiagID::ErrDuplicateAttributeArg => "duplicate argument in attribute '{}'",
            DiagID::ErrMultipleStorageClasses => "multiple storage classes in declaration",
            DiagID::ErrStorageClassForFunction => "storage class specified for function parameter",
            DiagID::ErrTypeSpecifierRepeated => "cannot combine with previous '{}' declaration specifier",
            DiagID::ErrIllegalStorageClassOnMember => "storage class on member is not allowed",
            DiagID::ErrThreadNonStatic => "'thread_local' can only be applied to variables with static or thread storage duration",
            DiagID::ErrThreadLocalInPlainObject => "thread-local storage class is not valid for this declaration",
            DiagID::ErrAtomicSpecifierBadType => "_Atomic cannot be applied to function type",
            DiagID::ErrAlignasOnBitField => "'_Alignas' attribute cannot be applied to a bit-field",
            DiagID::ErrAlignasUnderaligned => "requested alignment is less than minimum alignment of {} for type '{}'",

            // ── Notes ─────────────────────────────────────────────────────
            DiagID::NotePreviousDefinition => "previous definition is here",
            DiagID::NotePreviousDeclaration => "previous declaration is here",
            DiagID::NotePreviousImplicitDeclaration => "previous implicit declaration is here",
            DiagID::NotePreviousBuiltinDeclaration => "built-in declaration here",
            DiagID::NotePreviousUse => "previous use is here",
            DiagID::NotePreviousIf => "to match this '{'",
            DiagID::NotePreviousElse => "previous 'else' is here",
            DiagID::NoteMatching => "to match this '('",
            DiagID::NoteCandidateFunction => "candidate function not viable: {}",
            DiagID::NoteCandidateConstructor => "candidate constructor not viable: {}",
            DiagID::NoteCandidateTemplate => "candidate template ignored: {}",
            DiagID::NoteCandidateOperator => "candidate operator not viable: {}",
            DiagID::NoteTemplateParamDeclaredHere => "template parameter is declared here",
            DiagID::NoteDefaultArgInstantiatedHere => "default argument instantiated here",
            DiagID::NoteInInstantiationOf => "in instantiation of template class '{}' requested here",
            DiagID::NoteInExpansionOf => "in expansion of macro '{}'",
            DiagID::NoteInSubstitutionOf => "in substitution of template argument '{}'",
            DiagID::NoteInDefinitionOf => "in definition of '{}'",
            DiagID::NoteInMemberFunction => "in member function '{}'",
            DiagID::NoteInLambdaFunction => "in lambda function",
            DiagID::NoteWhileDeducingTemplateArgs => "while deducing template arguments for '{}'",
            DiagID::NoteDuringTemplateArgDeduction => "during template argument deduction for '{}'",
            DiagID::NoteForwardDeclaration => "forward declaration of '{}' is here",
            DiagID::NoteForwardDeclarationOfClass => "forward declaration of class '{}' is here",
            DiagID::NoteForwardDeclarationOfEnum => "forward declaration of enum '{}' is here",
            DiagID::NoteDeclarationHere => "declaration of '{}' here",
            DiagID::NoteDefinitionHere => "definition of '{}' here",
            DiagID::NoteHeaderHere => "header is here",
            DiagID::NoteIncludeLocation => "in file included from {}:{}:",
            DiagID::NotePreviousInclude => "previous include is here",
            DiagID::NoteMacroDefinedHere => "macro '{}' defined here",
            DiagID::NoteMacroExpandedHere => "expanded from macro '{}'",
            DiagID::NotePragmaHere => "#pragma is here",
            DiagID::NoteUninitializedHere => "variable '{}' may be uninitialized when used here",
            DiagID::NoteAssignedHere => "assigned here",
            DiagID::NoteConditionHere => "condition is here",
            DiagID::NoteLoopHere => "loop is here",
            DiagID::NoteExceptionHere => "exception thrown here",
            DiagID::NoteConversionHere => "conversion candidate is here",
            DiagID::NoteOverloadResolutionHere => "in overload resolution for '{}'",
            DiagID::NoteTypeMismatch => "type mismatch: expected '{}', got '{}'",
            DiagID::NoteValueHere => "value {}",
            DiagID::NoteParameterHere => "passing argument to parameter '{}' here",
            DiagID::NoteReturnValueHere => "returning from function here",
            DiagID::NoteDestructorHere => "destructor called here",
            DiagID::NoteConstructorHere => "constructor called here",
            DiagID::NoteBaseClassHere => "base class '{}' declared here",
            DiagID::NoteMemberDeclaredHere => "member '{}' declared here",
            DiagID::NoteLambdaCaptureHere => "lambda capture is here",
            DiagID::NoteLambdaHere => "lambda expression begins here",
            DiagID::NoteTemplateHere => "template declared here",
            DiagID::NoteSpecializationHere => "specialization declared here",
            DiagID::NoteExplicitInstantiationHere => "explicit instantiation is here",
            DiagID::NotePartialSpecializationHere => "partial specialization is here",
            DiagID::NoteDefaultTemplateArgHere => "default template argument declared here",
            DiagID::NoteDeducedTypeHere => "type '{}' deduced as '{}' here",
            DiagID::NoteWhileBuildingDeductionGuide => "while building deduction guide for '{}'",
            DiagID::NoteShadowDeclHere => "previous declaration is here",
            DiagID::NotePreviousShadowDecl => "previous shadow declaration is here",
            DiagID::NoteFieldDeclaredHere => "field '{}' is declared here",
            DiagID::NoteBitFieldDeclaredHere => "bit-field is declared here",
            DiagID::NoteAnonymousStructHere => "anonymous struct declared here",
            DiagID::NoteAnonymousUnionHere => "anonymous union declared here",
            DiagID::NoteEnumDeclaredHere => "enum '{}' declared here",
            DiagID::NoteTypedefHere => "typedef '{}' is here",
            DiagID::NoteUsingDeclHere => "using declaration is here",
            DiagID::NoteNamespaceHere => "namespace '{}' declared here",
            DiagID::NoteModuleHere => "module '{}' declared here",
            DiagID::NoteImportHere => "imported here",
            DiagID::NoteExportHere => "exported here",
            DiagID::NoteInEvaluatingExpr => "in evaluation of constant expression",
            DiagID::NoteInEvaluatingConstraint => "in evaluation of constraint",
            DiagID::NoteInConstraintNormalization => "while normalizing constraint",
            DiagID::NoteWhileCheckingConstraintSatisfaction => "while checking constraint satisfaction",
            DiagID::NoteConceptHere => "concept '{}' defined here",
            DiagID::NoteRequirementHere => "requirement declared here",
            DiagID::NoteReturnTypeHere => "return type is here",
            DiagID::NoteCallableHere => "called object is here",
            DiagID::NoteInstantiatedFrom => "instantiated from '{}'",
            DiagID::NoteExpandedFrom => "expanded from '{}'",
            DiagID::NoteInRecursiveInstantiation => "in recursive instantiation of '{}'",
            DiagID::NoteArrayInitHere => "array initialization here",
            DiagID::NoteDesignatedInitHere => "designated initializer here",
            DiagID::NoteAggregateInitHere => "aggregate initialization here",
            DiagID::NoteDefaultInitHere => "default initialization here",
            DiagID::NoteValueInitHere => "value initialization here",
            DiagID::NoteCopyInitHere => "copy initialization here",
            DiagID::NoteDirectInitHere => "direct initialization here",
            DiagID::NoteListInitHere => "list initialization here",
            DiagID::NoteReferenceInitHere => "reference initialization here",

            // ── Warnings ──────────────────────────────────────────────────
            // (Only show unique/new full messages; the diagnostics.rs already has
            //  default_message() for each DiagID, this is a supplementary extended catalog)
            DiagID::WUnusedVariable => "unused variable '{}'",
            DiagID::WUnusedParameter => "unused parameter '{}'",
            DiagID::WUnusedFunction => "unused function '{}'",
            DiagID::WUnusedLabel => "unused label '{}'",
            DiagID::WUnusedValue => "expression result unused",
            DiagID::WUnusedResult => "ignoring return value of function declared with 'warn_unused_result' attribute",
            DiagID::WSignCompare => "comparison of integers of different signs: '{}' and '{}'",
            DiagID::WSignConversion => "implicit conversion changes signedness: '{}' to '{}'",
            DiagID::WSignPromo => "overload resolution chose to promote from unsigned to signed",
            DiagID::WConversion => "implicit conversion loses precision: '{}' to '{}'",
            DiagID::WConversionLoss => "implicit conversion loses integer precision: '{}' to '{}'",
            DiagID::WFormat => "format specifies type '{}' but the argument has type '{}'",
            DiagID::WFormatSecurity => "format string is not a string literal (potentially insecure)",
            DiagID::WShadow => "declaration shadows a local variable",
            DiagID::WParentheses => "'&'/'|' within '&&'/'||' [-Wparentheses]",
            DiagID::WImplicitFallthrough => "unannotated fall-through between switch labels",
            DiagID::WStrictAliasing => "dereferencing type-punned pointer will break strict-aliasing rules",
            DiagID::WReturnType => "control reaches end of non-void function",
            DiagID::WUninitialized => "variable '{}' is uninitialized when used here",
            DiagID::WTautologicalCompare => "self-comparison always evaluates to {}",
            DiagID::WNullDereference => "null pointer dereference",
            DiagID::WDivisionByZero => "division by zero is undefined",
            DiagID::WArrayBounds => "array index {} is past the end of the array",
            DiagID::WUnreachableCode => "code will never be executed",
            DiagID::WCharSubscripts => "array subscript is of type 'char'",
            DiagID::WMainReturnType => "return type of 'main' is not 'int'",
            DiagID::WMain => "first parameter of 'main' (argument count) should be of type 'int'",
            DiagID::WMissingBraces => "suggest braces around initialization of subobject",
            DiagID::WMissingFieldInitializers => "missing field '{}' initializer",
            DiagID::WMissingPrototypes => "no previous prototype for function '{}'",
            DiagID::WDeprecated => "'{}' is deprecated",
            DiagID::WDeprecatedDeclarations => "'{}' is deprecated: {}",
            DiagID::WExtra => "extra tokens at end of #include directive",
            DiagID::WExtraSemi => "extra ';' after member function definition",
            DiagID::WOverloadedVirtual => "'{}' hides overloaded virtual function",
            DiagID::WReorder => "field '{}' will be initialized after field '{}'",
            DiagID::WAttributes => "unknown attribute '{}' ignored",
            DiagID::WUserDefinedLiterals => "user-defined literal suffixes not starting with '_' are reserved",
            DiagID::WPointerArith => "arithmetic on a pointer to void is a GNU extension",
            DiagID::WPointerToIntCast => "cast to smaller integer type '{}' from '{}' may lose data",
            DiagID::WIntToPointerCast => "cast to '{}' from smaller integer type '{}' may lose data",
            DiagID::WCastAlign => "cast from '{}' to '{}' increases required alignment from {} to {}",
            DiagID::WCastQual => "cast from '{}' to '{}' drops const qualifier",
            DiagID::WBadFunctionCast => "cast from '{}' to '{}' converts to incompatible function type",
            DiagID::WFloatEqual => "comparing floating point with == or != is unsafe",
            DiagID::WUndef => "'{}' is not defined, evaluates to 0",
            DiagID::WTrigraphs => "trigraph ignored",
            DiagID::WComment => "'/*' within block comment",
            DiagID::WMultichar => "multi-character character constant",
            DiagID::WCharLiteralAscii => "char literal with non-ASCII character",
            DiagID::WImplicitInt => "type specifier missing, defaults to 'int'",
            DiagID::WImplicitFunctionDeclarations => "implicit declaration of function '{}' is invalid in C99",
            DiagID::WIntConversion => "implicit conversion from '{}' to '{}' changes value from {} to {}",
            DiagID::WIntInBoolContext => "converting integer to bool",
            DiagID::WLogicalNotParentheses => "logical not is only applied to left hand side of comparison",
            DiagID::WLongLong => "'long long' is an extension when C90 mode is enabled",
            DiagID::WNestedExterns => "'extern' inside function scope",
            DiagID::WNoNewlineEOF => "no newline at end of file",
            DiagID::WOldStyleCast => "use of old-style cast",
            DiagID::WOldStyleDefinition => "old-style function definition",
            DiagID::WOverlengthStrings => "string literal of length {} bytes exceeds maximum length {}",
            DiagID::WPointerSign => "pointer targets in passing argument {} of '{}' differ in signedness",
            DiagID::WRedundantDecls => "redundant redeclaration of '{}'",
            DiagID::WReturnTypeCVQual => "'const'/'volatile' type qualifiers on return type have no effect",
            DiagID::WSequencePoint => "unsequenced modification and access to '{}'",
            DiagID::WShiftCountOverflow => "shift count >= width of type",
            DiagID::WShiftCountNegative => "shift count is negative",
            DiagID::WShiftNegativeValue => "shifting a negative signed value is undefined",
            DiagID::WStringPlusInt => "adding 'int' to a string does not append to the string",
            DiagID::WStringPlusChar => "adding 'char' to a string pointer",
            DiagID::WStringConversion => "implicit conversion turns string literal into bool",
            DiagID::WTypeLimits => "comparison of unsigned expression {} 0 is always {}",
            DiagID::WVexingParse => "parentheses were disambiguated as a function declaration",
            DiagID::WVLA => "variable length array used",
            DiagID::WVolatileRegisterVar => "a volatile register variable is used",
            DiagID::WWriteStrings => "converting a string literal to 'char *' discards const qualifier",
            DiagID::WZeroLengthArray => "zero size arrays are an extension",
            DiagID::WClangSpecific => "clang-specific diagnostic",
            DiagID::WUnknownPragmas => "unknown pragma ignored",
            DiagID::WUnknownWarningOption => "unknown warning option '{}'",
            DiagID::WVariadicMacros => "variadic macros are a C99 feature",
            DiagID::WMost => "most-related warnings",
            DiagID::WInfiniteRecursion => "all paths through this function will call itself",
            DiagID::WSuspiciousBzero => "suspicious bzero call; size may be wrong",
            DiagID::WSuspiciousMemaccess => "suspicious memaccess call; size may be wrong",
            DiagID::WNonPODVarargs => "passing object of non-trivially-copyable type through variadic arguments",
            DiagID::WStringCompression => "string compression",
            DiagID::WEnumTooLarge => "enumeration values exceed range of largest integer",
            DiagID::WExcessInitializers => "excess elements in initializer",
            DiagID::WFloatOverflowConversion => "implicit conversion from '{}' to '{}' changes value",
            DiagID::WFloatZeroConversion => "implicit conversion from '{}' to '{}' turns non-zero to zero",
            DiagID::WMismatchedNewDelete => "'operator delete' does not match 'operator new'",
            DiagID::WMismatchedReturnTypes => "mismatched return types",
            DiagID::WMismatchedTags => "class '{}' was previously declared as a struct",
            DiagID::WNonVirtualDtor => "'{}' has virtual functions but non-virtual destructor",
            DiagID::WSentinel => "missing sentinel in function call",
            DiagID::WStringConcat => "string concatenation",
            DiagID::WStringLiteralConversion => "implicit conversion of string literal to 'bool'",
            DiagID::WStructPadding => "padding struct to align '{}'",
            DiagID::WThreadSafety => "thread safety analysis warning",
            DiagID::WThreadSafetyAnalysis => "thread safety analysis: {}",
            DiagID::WUnavailableDeclarations => "'{}' is unavailable",
            DiagID::WUnguardedAvailability => "'{}' is only available on {} {} or newer",
            DiagID::WUnsupportedTargetOpt => "unsupported target option '{}'",
            DiagID::WEmptyBody => "empty body in {} statement",
            DiagID::WEmptyTranslationUnit => "empty translation unit",

            // ── Fatal Errors ──────────────────────────────────────────────
            DiagID::FatalNoInputFiles => "no input files",
            DiagID::FatalCannotOpenFile => "cannot open file '{}': {}",
            DiagID::FatalCannotWriteFile => "cannot write to file '{}': {}",
            DiagID::FatalCannotExecuteBinary => "cannot execute binary '{}'",
            DiagID::FatalOutOfMemory => "out of memory",
            DiagID::FatalStackExhausted => "stack exhausted",
            DiagID::FatalIncludeTooDeep => "#include nested too deeply",
            DiagID::FatalMacroExpansionTooDeep => "macro expansion too deeply nested",
            DiagID::FatalTemplateInstantiationTooDeep => "template instantiation depth exceeded",
            DiagID::FatalRecursiveTemplateInstantiation => "recursive template instantiation",
            DiagID::FatalTooManyErrors => "too many errors emitted, stopping now",
            DiagID::FatalInvalidTarget => "invalid target '{}'",
            DiagID::FatalUnsupportedTarget => "unsupported target '{}'",
            DiagID::FatalInvalidArch => "invalid architecture '{}' for target '{}'",
            DiagID::FatalInvalidCPU => "invalid CPU '{}' for target '{}'",
            DiagID::FatalInvalidFeature => "invalid feature '{}' for target '{}'",
            DiagID::FatalInvalidOption => "invalid option '{}'",
            DiagID::FatalConflictingOptions => "conflicting options '{}' and '{}'",
            DiagID::FatalMissingArg => "argument to '{}' is missing (expected {} value)",
            DiagID::FatalUnknownArg => "unknown argument '{}'",
            DiagID::FatalInvalidValue => "invalid value '{}' for '{}'",
            DiagID::FatalModuleBuildFailed => "module build failed for '{}'",
            DiagID::FatalModuleFileOutOfDate => "module file is out of date",
            DiagID::FatalModuleFileInvalid => "module file is invalid",
            DiagID::FatalCyclicModuleDependency => "cyclic module dependency detected",
            DiagID::FatalPCHOutOfDate => "PCH file is out of date",
            DiagID::FatalPCHInvalid => "PCH file is invalid",
            DiagID::FatalPCHCXXMismatch => "PCH file was built for a different C++ dialect",
            DiagID::FatalPCHTargetMismatch => "PCH file was built for a different target",
            DiagID::FatalPCHLanguageMismatch => "PCH file was built for a different language",
            DiagID::FatalPCHVersionMismatch => "PCH file was built by a different compiler version",
            DiagID::FatalPCHHasDifferentModuleCache => "PCH file uses a different module cache path",
            DiagID::FatalCannotFindHeaders => "cannot find headers for target '{}'",
            DiagID::FatalCannotFindSDK => "cannot find SDK for target '{}'",
            DiagID::FatalSDKTooOld => "SDK version {} is too old (minimum: {})",
            DiagID::FatalSDKMissingRequired => "SDK is missing required component '{}'",
            DiagID::FatalBrokenInstallation => "broken compiler installation; check your paths",
            DiagID::FatalInternalError => "internal compiler error: {}",
            DiagID::FatalUnimplemented => "unimplemented feature: {}",
            DiagID::FatalUnreachable => "unreachable code executed in compiler",
            DiagID::FatalLTOError => "LTO error: {}",
            DiagID::FatalBackendError => "backend error: {}",
            DiagID::FatalCodeGenError => "code generation error: {}",
            DiagID::FatalLinkError => "linker error: {}",
            DiagID::FatalProfileDataError => "profile data error: {}",
            DiagID::FatalSanitizerError => "sanitizer runtime error: {}",

            // ── Remarks ───────────────────────────────────────────────────
            DiagID::RemarkBackendOptimization => "{}",
            DiagID::RemarkLoopVectorized => "vectorized loop",
            DiagID::RemarkLoopInterleaved => "interleaved loop",
            DiagID::RemarkSLPVectorized => "SLP vectorized",
            DiagID::RemarkInlinedFunction => "'{}' inlined into '{}'",
            DiagID::RemarkNotInlined => "'{}' not inlined into '{}' because {}",
            DiagID::RemarkLoopUnrolled => "unrolled loop by a factor of {}",
            DiagID::RemarkLoopUnrolledPartial => "partially unrolled loop by a factor of {}",
            DiagID::RemarkLoopUnrolledComplete => "completely unrolled loop",
            DiagID::RemarkLoopUnrolledFailed => "failed to unroll loop",
            DiagID::RemarkLoopFused => "fused {} loops",
            DiagID::RemarkLoopDistributed => "distributed loop",
            DiagID::RemarkLoopInterchanged => "interchanged loop",
            DiagID::RemarkLoopUnswitched => "unswitched loop",
            DiagID::RemarkLoopVersioned => "versioned loop",
            DiagID::RemarkLicmMoved => "LICM moved",
            DiagID::RemarkGVNLoadEliminated => "load eliminated by GVN",
            DiagID::RemarkMemCpyOptimized => "memcpy optimized",
            DiagID::RemarkDivRemExpanded => "div/rem expanded",
            DiagID::RemarkFloat2IntConverted => "float to int conversion",
            DiagID::RemarkConstantHoisted => "constant hoisted",
            DiagID::RemarkReassociated => "reassociated expression",
            DiagID::RemarkInstructionCount => "instruction count: {}",
            DiagID::RemarkInlineCost => "inline cost",
            DiagID::RemarkFunctionCloned => "function cloned",
            DiagID::RemarkCallSiteInlined => "call site inlined",
            DiagID::RemarkCallSiteNotInlined => "call site not inlined: {}",
            DiagID::RemarkProfileDataApplied => "profile data applied",
            DiagID::RemarkProfileDataMissing => "profile data missing",
            DiagID::RemarkInstrProfApplied => "instrumentation profile applied",
            DiagID::RemarkSampleProfileApplied => "sample profile applied",
            DiagID::RemarkAnnotationStackAccess => "stack access annotation",
            DiagID::RemarkAnnotationJumpTable => "jump table annotation",
            DiagID::RemarkAnnotationVectorWidth => "vector width annotation: {}",
            DiagID::RemarkAnnotationInterleaveCount => "interleave count annotation: {}",
            DiagID::RemarkAnnotationUnrollCount => "unroll count annotation: {}",
            DiagID::RemarkAnnotationLoopDistribution => "loop distribution annotation",
            DiagID::RemarkSFINAEExplanation => "{}",
            DiagID::RemarkCandidateDisabled => "candidate disabled: {}",
            DiagID::RemarkCandidateNotViable => "candidate not viable: {}",
            DiagID::RemarkTemplateDeductionFailure => "template deduction failure: {}",
            DiagID::RemarkOverloadCandidateDropped => "overload candidate dropped: {}",
            DiagID::RemarkUsingShadowDecl => "using shadow declaration",
            DiagID::RemarkModuleImport => "module '{}' imported",
            DiagID::RemarkModuleExport => "module '{}' exported",
            DiagID::RemarkModuleBuild => "module '{}' built",
            DiagID::RemarkPragmaWarning => "{}",
            DiagID::RemarkPragmaMessage => "{}",
            DiagID::RemarkBackendPlugin => "{}",

            // ── Ignored ───────────────────────────────────────────────────
            DiagID::IgnoredMacroDefined => "macro is defined",
            DiagID::IgnoredMacroUndefined => "macro is undefined",
            DiagID::IgnoredPragma => "unknown pragma ignored",
            DiagID::IgnoredAttribute => "unknown attribute ignored",
            DiagID::IgnoredExtension => "extension ignored",
            DiagID::IgnoredWarning => "warning ignored",
            DiagID::IgnoredDeprecated => "deprecated diagnostic ignored",

            _ => "unknown diagnostic",
        }
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// Color-Coded Diagnostic Output
// ═══════════════════════════════════════════════════════════════════════════════

/// ANSI terminal colors for diagnostic output.
/// Follows Clang convention:
///   error  = red + bold
///   warning = magenta + bold
///   note    = cyan
///   fixit   = green
///   caret   = green
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AnsiColor {
    Bold,
    Red,
    Green,
    Yellow,
    Blue,
    Magenta,
    Cyan,
    White,
    Reset,
}

impl AnsiColor {
    /// Return the ANSI SGR escape sequence for this color.
    pub fn code(&self) -> &'static str {
        match self {
            AnsiColor::Bold => "\x1b[1m",
            AnsiColor::Red => "\x1b[31m",
            AnsiColor::Green => "\x1b[32m",
            AnsiColor::Yellow => "\x1b[33m",
            AnsiColor::Blue => "\x1b[34m",
            AnsiColor::Magenta => "\x1b[35m",
            AnsiColor::Cyan => "\x1b[36m",
            AnsiColor::White => "\x1b[37m",
            AnsiColor::Reset => "\x1b[0m",
        }
    }

    /// Wrap `text` in the start code for this color and a reset.
    pub fn wrap(&self, text: &str) -> String {
        format!("{}{}{}", self.code(), text, AnsiColor::Reset.code())
    }
}

/// Color scheme for diagnostic rendering.
#[derive(Debug, Clone)]
pub struct DiagnosticColorScheme {
    pub error_colors: Vec<AnsiColor>,
    pub warning_colors: Vec<AnsiColor>,
    pub note_colors: Vec<AnsiColor>,
    pub fixit_colors: Vec<AnsiColor>,
    pub caret_colors: Vec<AnsiColor>,
    pub source_line_colors: Vec<AnsiColor>,
    pub filename_colors: Vec<AnsiColor>,
    pub line_number_colors: Vec<AnsiColor>,
    pub range_highlight_colors: Vec<AnsiColor>,
    pub use_colors: bool,
}

impl DiagnosticColorScheme {
    /// Creates the standard Clang-style color scheme.
    pub fn clang_style() -> Self {
        Self {
            error_colors: vec![AnsiColor::Bold, AnsiColor::Red],
            warning_colors: vec![AnsiColor::Bold, AnsiColor::Magenta],
            note_colors: vec![AnsiColor::Cyan],
            fixit_colors: vec![AnsiColor::Green],
            caret_colors: vec![AnsiColor::Green],
            source_line_colors: vec![AnsiColor::White],
            filename_colors: vec![AnsiColor::Bold],
            line_number_colors: vec![AnsiColor::Blue],
            range_highlight_colors: vec![AnsiColor::Green],
            use_colors: true,
        }
    }

    /// Creates a monochrome (no color) scheme.
    pub fn monochrome() -> Self {
        Self {
            error_colors: vec![],
            warning_colors: vec![],
            note_colors: vec![],
            fixit_colors: vec![],
            caret_colors: vec![],
            source_line_colors: vec![],
            filename_colors: vec![],
            line_number_colors: vec![],
            range_highlight_colors: vec![],
            use_colors: false,
        }
    }

    /// Apply color sequence to text using stored colors.
    fn apply_colors(&self, colors: &[AnsiColor], text: &str) -> String {
        if !self.use_colors || colors.is_empty() {
            return text.to_string();
        }
        let start: String = colors.iter().map(|c| c.code()).collect();
        format!("{}{}{}", start, text, AnsiColor::Reset.code())
    }

    pub fn error_open(&self) -> String {
        if !self.use_colors { String::new() } else {
            self.error_colors.iter().map(|c| c.code()).collect()
        }
    }

    pub fn warning_open(&self) -> String {
        if !self.use_colors { String::new() } else {
            self.warning_colors.iter().map(|c| c.code()).collect()
        }
    }

    pub fn note_open(&self) -> String {
        if !self.use_colors { String::new() } else {
            self.note_colors.iter().map(|c| c.code()).collect()
        }
    }

    pub fn fixit_open(&self) -> String {
        if !self.use_colors { String::new() } else {
            self.fixit_colors.iter().map(|c| c.code()).collect()
        }
    }

    pub fn caret_open(&self) -> String {
        if !self.use_colors { String::new() } else {
            self.caret_colors.iter().map(|c| c.code()).collect()
        }
    }

    pub fn close(&self) -> String {
        if !self.use_colors { String::new() } else {
            AnsiColor::Reset.code().to_string()
        }
    }

    pub fn colorize_error(&self, text: &str) -> String {
        self.apply_colors(&self.error_colors, text)
    }

    pub fn colorize_warning(&self, text: &str) -> String {
        self.apply_colors(&self.warning_colors, text)
    }

    pub fn colorize_note(&self, text: &str) -> String {
        self.apply_colors(&self.note_colors, text)
    }

    pub fn colorize_fixit(&self, text: &str) -> String {
        self.apply_colors(&self.fixit_colors, text)
    }

    pub fn colorize_caret(&self, text: &str) -> String {
        self.apply_colors(&self.caret_colors, text)
    }

    pub fn colorize_filename(&self, text: &str) -> String {
        self.apply_colors(&self.filename_colors, text)
    }

    pub fn colorize_line_number(&self, text: &str) -> String {
        self.apply_colors(&self.line_number_colors, text)
    }
}

impl Default for DiagnosticColorScheme {
    fn default() -> Self {
        Self::clang_style()
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// Caret-Range Rendering with Multiple Range Highlighting
// ═══════════════════════════════════════════════════════════════════════════════

/// Represents a diagnostic range within a source line for caret rendering.
#[derive(Debug, Clone)]
pub struct CaretRange {
    /// Start column (0-based).
    pub start_col: usize,
    /// End column (0-based, exclusive).
    pub end_col: usize,
    /// Whether this is the primary range (marked with ^).
    pub is_primary: bool,
    /// Message to show below the caret line for this range.
    pub message: Option<String>,
}

impl CaretRange {
    pub fn new(start: usize, end: usize) -> Self {
        Self { start_col: start, end_col: end, is_primary: false, message: None }
    }

    pub fn primary(start: usize, end: usize) -> Self {
        Self { start_col: start, end_col: end, is_primary: true, message: None }
    }

    pub fn with_message(start: usize, end: usize, msg: &str) -> Self {
        Self { start_col: start, end_col: end, is_primary: false, message: Some(msg.to_string()) }
    }
}

/// Builds a caret line with tildes and carets for multiple highlighted ranges.
/// Example output:
/// ```
///   int x = 1 + "hello";
///           ~~~~~^~~~~~
/// ```
pub struct CaretRenderer;

impl CaretRenderer {
    /// Build a caret string like `~~~~^~~~~~` given a source line and a set of ranges.
    pub fn build_caret_line(
        line: &str,
        ranges: &[CaretRange],
        scheme: &DiagnosticColorScheme,
    ) -> String {
        if ranges.is_empty() {
            return String::new();
        }

        let line_len = line.len();
        // We build the caret line character-by-character.
        let mut caret_line = String::with_capacity(line_len + 10);

        // Sort ranges by start column.
        let mut sorted = ranges.to_vec();
        sorted.sort_by_key(|r| r.start_col);

        // Track which columns have tildes vs carets.
        let mut col_markers: Vec<char> = vec![' '; line_len.max(1)];

        for range in &sorted {
            let start = range.start_col.min(line_len.saturating_sub(1));
            let end = range.end_col.min(line_len);
            if start >= end || start >= line_len {
                continue;
            }
            let primary_pos = if range.is_primary { start } else { start };
            for col in start..end.min(line_len) {
                if col == primary_pos && range.is_primary {
                    col_markers[col] = '^';
                } else if col_markers[col] == ' ' {
                    col_markers[col] = '~';
                } else if col_markers[col] != '^' {
                    col_markers[col] = '~';
                }
            }
        }

        // Trim trailing spaces from the caret line.
        while !col_markers.is_empty() && col_markers.last() == Some(&' ') {
            col_markers.pop();
        }

        // Now build the output: we add spaces up to the first non-space column
        // then the tildes/carets with color.
        let first_marker = col_markers.iter().position(|&c| c != ' ').unwrap_or(0);
        for _ in 0..first_marker {
            caret_line.push(' ');
        }

        for ch in col_markers.iter().skip(first_marker) {
            caret_line.push(*ch);
        }

        let colored = scheme.colorize_caret(&caret_line);
        colored
    }

    /// Build a multi-range caret line that shows all ranges on one line, plus
    /// messages below for secondary ranges.
    pub fn build_caret_with_messages(
        line: &str,
        ranges: &[CaretRange],
        scheme: &DiagnosticColorScheme,
    ) -> Vec<String> {
        let mut result = Vec::new();
        let caret_line = Self::build_caret_line(line, ranges, scheme);
        result.push(caret_line);

        // Add message lines for ranges with messages.
        for range in ranges {
            if let Some(ref msg) = range.message {
                let indent = " ".repeat(range.start_col);
                let msg_line = format!("{}|", indent);
                result.push(msg_line);
                let msg_indent = " ".repeat(range.start_col + 1);
                result.push(format!("{}{}", msg_indent, scheme.colorize_note(msg)));
            }
        }

        result
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// Source Snippet Display — context lines with line numbers
// ═══════════════════════════════════════════════════════════════════════════════

/// A source snippet showing context lines around a diagnostic location.
#[derive(Debug, Clone)]
pub struct SourceSnippet {
    /// The source lines and their line numbers.
    pub lines: Vec<(usize, String)>,
    /// The line number where the primary diagnostic is.
    pub primary_line: usize,
    /// The caret ranges within the primary line.
    pub ranges: Vec<CaretRange>,
}

impl SourceSnippet {
    /// Create a new source snippet with the given lines and primary marker.
    pub fn new(lines: Vec<(usize, String)>, primary_line: usize) -> Self {
        Self { lines, primary_line, ranges: Vec::new() }
    }

    /// Add a highlighted range.
    pub fn add_range(&mut self, range: CaretRange) {
        self.ranges.push(range);
    }

    /// Render the snippet with line numbers, source text, and caret lines.
    pub fn render(&self, scheme: &DiagnosticColorScheme) -> String {
        let mut output = String::new();
        let max_line_num = self.lines.iter().map(|(n, _)| *n).max().unwrap_or(0);
        let line_num_width = max_line_num.to_string().len().max(4);

        for (line_num, line_text) in &self.lines {
            // Line number column.
            let num_str = format!("{:>width$} | ", line_num, width = line_num_width);
            output.push_str(&scheme.colorize_line_number(&num_str));

            // Source line.
            if *line_num == self.primary_line {
                output.push_str(&line_text);
            } else {
                output.push_str(&scheme.apply_colors(&scheme.source_line_colors, line_text));
            }
            output.push('\n');

            // Caret line for primary line.
            if *line_num == self.primary_line && !self.ranges.is_empty() {
                let indent = " ".repeat(line_num_width + 3);
                output.push_str(&indent);
                let caret = CaretRenderer::build_caret_line(line_text, &self.ranges, scheme);
                output.push_str(&caret);
                output.push('\n');
            }
        }

        output
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// Full Diagnostic Renderer — combines color, snippets, carets, fixits
// ═══════════════════════════════════════════════════════════════════════════════

/// Complete diagnostic renderer that outputs Clang-style formatted diagnostics
/// with color-coded output, caret-ranges, source snippets, fix-it hints,
/// and diagnostic category display.
pub struct FullDiagnosticRenderer {
    pub color_scheme: DiagnosticColorScheme,
    pub show_column: bool,
    pub show_source_line: bool,
    pub show_carets: bool,
    pub show_fixits: bool,
    pub show_option_names: bool,
    pub show_categories: bool,
    pub context_lines: usize,
    pub tab_stop: usize,
    pub message_length: usize,
}

impl FullDiagnosticRenderer {
    /// Creates a renderer with Clang-style defaults.
    pub fn new() -> Self {
        Self {
            color_scheme: DiagnosticColorScheme::clang_style(),
            show_column: true,
            show_source_line: true,
            show_carets: true,
            show_fixits: true,
            show_option_names: true,
            show_categories: true,
            context_lines: 1,
            tab_stop: 8,
            message_length: 120,
        }
    }

    /// Creates a renderer for IDE/tooling consumption (no colors).
    pub fn for_ide() -> Self {
        Self {
            color_scheme: DiagnosticColorScheme::monochrome(),
            ..Self::new()
        }
    }

    /// Render a single diagnostic to a string (terminal output format).
    pub fn render_diagnostic(&self, diag: &StructuredDiagnostic, lines: &[(usize, String)]) -> String {
        let mut output = String::new();

        // ── Header: file:line:col: severity: message ──
        let severity_str = match diag.severity {
            DiagSeverity::Error | DiagSeverity::Fatal => self.color_scheme.colorize_error("error:"),
            DiagSeverity::Warning => self.color_scheme.colorize_warning("warning:"),
            DiagSeverity::Note => self.color_scheme.colorize_note("note:"),
            DiagSeverity::Remark => self.color_scheme.colorize_note("remark:"),
            DiagSeverity::Ignored => "ignored:".to_string(),
        };

        if diag.location.is_valid {
            let file = self.color_scheme.colorize_filename(
                &diag.location.file.display().to_string()
            );
            output.push_str(&format!("{}:{}:{}: ", file, diag.location.line, diag.location.column));
        }
        output.push_str(&format!("{} {}\n", severity_str, diag.message));

        // ── Category display [-Wcategory-name] ──
        if self.show_categories && !diag.category.is_empty() {
            output.push_str(&format!(
                "{}",
                self.color_scheme.colorize_note(&format!("[-W{}] ", diag.category))
            ));
            output.push('\n');
        }

        // ── Source snippet with context ──
        if self.show_source_line && !lines.is_empty() {
            let snippet = SourceSnippet::new(lines.to_vec(), diag.location.line as usize);
            output.push_str(&snippet.render(&self.color_scheme));
        }

        // ── Caret ranges ──
        if self.show_carets && !diag.ranges.is_empty() {
            let line_num = diag.location.line as usize;
            let primary_line = lines.iter().find(|(n, _)| *n == line_num)
                .map(|(_, t)| t.clone())
                .unwrap_or_default();
            let caret_ranges: Vec<CaretRange> = diag.ranges.iter().map(|r| {
                CaretRange::primary(r.begin.column as usize - 1, r.end.column as usize)
            }).collect();
            if !caret_ranges.is_empty() {
                let indent = " ".repeat(5);
                output.push_str(&indent);
                let caret = CaretRenderer::build_caret_line(&primary_line, &caret_ranges, &self.color_scheme);
                output.push_str(&caret);
                output.push('\n');
            }
        }

        // ── Fix-its ──
        if self.show_fixits && !diag.fixits.is_empty() {
            for fixit in &diag.fixits {
                if !fixit.code.is_empty() && !fixit.remove_range.begin.is_valid {
                    output.push_str(&format!(
                        "{}: {}\n",
                        self.color_scheme.colorize_note("fix-it"),
                        self.color_scheme.colorize_fixit(&fixit.code)
                    ));
                }
            }
        }

        // ── Notes ──
        for note in &diag.notes {
            if note.location.is_valid {
                let file = note.location.file.display().to_string();
                output.push_str(&format!(
                    "{}:{}:{}: {} {}\n",
                    self.color_scheme.colorize_filename(&file),
                    note.location.line,
                    note.location.column,
                    self.color_scheme.colorize_note("note:"),
                    note.message
                ));
            } else {
                output.push_str(&format!(
                    "{} {}\n",
                    self.color_scheme.colorize_note("note:"),
                    note.message
                ));
            }
        }

        // ── Flag name ──
        if self.show_option_names {
            if let Some(ref flag) = diag.flag_name {
                output.push_str(&format!(
                    "{}",
                    self.color_scheme.colorize_note(&format!("[-W{}] ", flag))
                ));
            }
        }

        output
    }

    /// Render a set of diagnostics as full Clang-style output.
    pub fn render_all<W: Write>(
        &self,
        writer: &mut W,
        diagnostics: &[StructuredDiagnostic],
        source_lines: &[(usize, String)],
    ) -> io::Result<()> {
        for diag in diagnostics {
            let rendered = self.render_diagnostic(diag, source_lines);
            write!(writer, "{}", rendered)?;
            if !rendered.ends_with('\n') {
                writeln!(writer)?;
            }
        }
        Ok(())
    }
}

impl Default for FullDiagnosticRenderer {
    fn default() -> Self {
        Self::new()
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// Diagnostic Suggestion Engine — "did you mean X?"
// ═══════════════════════════════════════════════════════════════════════════════

/// Suggests fixes based on identifier typos (Levenshtein distance).
pub struct DiagnosticSuggestionEngine {
    /// Known identifier names to suggest from.
    pub known_names: HashSet<String>,
    /// Maximum edit distance for suggestions (default: 2).
    pub max_distance: usize,
}

impl DiagnosticSuggestionEngine {
    pub fn new() -> Self {
        Self { known_names: HashSet::new(), max_distance: 2 }
    }

    pub fn with_names(names: impl IntoIterator<Item = String>) -> Self {
        Self { known_names: names.into_iter().collect(), max_distance: 2 }
    }

    /// Register a known identifier for suggestion.
    pub fn register(&mut self, name: &str) {
        self.known_names.insert(name.to_string());
    }

    /// Register multiple known identifiers.
    pub fn register_all(&mut self, names: &[&str]) {
        for name in names {
            self.known_names.insert(name.to_string());
        }
    }

    /// Suggest the closest known identifier to `input`.
    /// Returns `None` if no suggestion within max_distance is found.
    pub fn suggest(&self, input: &str) -> Option<String> {
        let mut best: Option<(usize, String)> = None;
        for name in &self.known_names {
            let dist = levenshtein_distance(input, name);
            if dist <= self.max_distance {
                if best.as_ref().map_or(true, |(d, _)| dist < *d) {
                    best = Some((dist, name.clone()));
                }
            }
        }
        best.map(|(_, name)| name)
    }

    /// Generate a "did you mean X?" message for a given identifier.
    pub fn did_you_mean(&self, input: &str) -> Option<String> {
        self.suggest(input).map(|s| format!("did you mean '{}'?", s))
    }

    /// Generate a fix-it hint for replacing a misspelled identifier.
    pub fn fixit_for(&self, input: &str) -> Option<FixItHint> {
        self.suggest(input).map(|replacement| FixItHint::replacement(
            SourceRange::point(ClangSourceLocation::invalid()),
            &replacement,
        ))
    }
}

impl Default for DiagnosticSuggestionEngine {
    fn default() -> Self {
        Self::new()
    }
}

/// Compute the Levenshtein edit distance between two strings.
fn levenshtein_distance(a: &str, b: &str) -> usize {
    let a_chars: Vec<char> = a.chars().collect();
    let b_chars: Vec<char> = b.chars().collect();
    let alen = a_chars.len();
    let blen = b_chars.len();

    if alen == 0 { return blen; }
    if blen == 0 { return alen; }

    let mut prev: Vec<usize> = (0..=blen).collect();
    let mut curr = vec![0usize; blen + 1];

    for i in 1..=alen {
        curr[0] = i;
        for j in 1..=blen {
            let cost = if a_chars[i - 1] == b_chars[j - 1] { 0 } else { 1 };
            curr[j] = (prev[j] + 1)
                .min(curr[j - 1] + 1)
                .min(prev[j - 1] + cost);
        }
        std::mem::swap(&mut prev, &mut curr);
    }
    prev[blen]
}

// ═══════════════════════════════════════════════════════════════════════════════
// Diagnostic Severity Manager — per-file and per-category overrides
// ═══════════════════════════════════════════════════════════════════════════════

/// Maps files and categories to custom diagnostic severities.
pub struct DiagnosticSeverityManager {
    /// Per-file severity overrides (file path -> DiagSeverity).
    pub file_overrides: HashMap<String, DiagSeverity>,
    /// Per-category severity overrides (category name -> DiagSeverity).
    pub category_overrides: HashMap<String, DiagSeverity>,
    /// Per-file, per-category severity overrides.
    pub file_category_overrides: HashMap<(String, String), DiagSeverity>,
    /// Globally suppressed diagnostics.
    pub globally_suppressed: HashSet<DiagID>,
}

impl DiagnosticSeverityManager {
    pub fn new() -> Self {
        Self {
            file_overrides: HashMap::new(),
            category_overrides: HashMap::new(),
            file_category_overrides: HashMap::new(),
            globally_suppressed: HashSet::new(),
        }
    }

    /// Set severity for all diagnostics in a file.
    pub fn set_file_severity(&mut self, file: &str, severity: DiagSeverity) {
        self.file_overrides.insert(file.to_string(), severity);
    }

    /// Set severity for a category.
    pub fn set_category_severity(&mut self, category: &str, severity: DiagSeverity) {
        self.category_overrides.insert(category.to_string(), severity);
    }

    /// Supress a specific diagnostic ID globally.
    pub fn suppress(&mut self, id: DiagID) {
        self.globally_suppressed.insert(id);
    }

    /// Check if a diagnostic ID is suppressed.
    pub fn is_suppressed(&self, id: &DiagID) -> bool {
        self.globally_suppressed.contains(id)
    }

    /// Resolve effective severity for a diagnostic.
    pub fn effective_severity(
        &self,
        id: &DiagID,
        file: Option<&str>,
        category: Option<&str>,
        default: DiagSeverity,
    ) -> DiagSeverity {
        if self.globally_suppressed.contains(id) {
            return DiagSeverity::Ignored;
        }
        if let (Some(f), Some(c)) = (file, category) {
            if let Some(sev) = self.file_category_overrides.get(&(f.to_string(), c.to_string())) {
                return *sev;
            }
        }
        if let Some(c) = category {
            if let Some(sev) = self.category_overrides.get(c) {
                return *sev;
            }
        }
        if let Some(f) = file {
            if let Some(sev) = self.file_overrides.get(f) {
                return *sev;
            }
        }
        default
    }
}

impl Default for DiagnosticSeverityManager {
    fn default() -> Self {
        Self::new()
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// Complete Diagnostic Printer — integrates all rendering features
// ═══════════════════════════════════════════════════════════════════════════════

/// The full diagnostic printer that binds together: color scheme, message catalog,
/// category registry, suggestion engine, snippet rendering, and severity management.
pub struct CompleteDiagnosticPrinter<W: Write> {
    pub writer: W,
    pub renderer: FullDiagnosticRenderer,
    pub message_catalog: &'static FullDiagnosticMessageCatalog,
    pub suggestion_engine: DiagnosticSuggestionEngine,
    pub severity_manager: DiagnosticSeverityManager,
    pub error_count: usize,
    pub warning_count: usize,
    pub note_count: usize,
}

impl<W: Write> CompleteDiagnosticPrinter<W> {
    pub fn new(writer: W) -> Self {
        Self {
            writer,
            renderer: FullDiagnosticRenderer::new(),
            message_catalog: &FullDiagnosticMessageCatalog,
            suggestion_engine: DiagnosticSuggestionEngine::new(),
            severity_manager: DiagnosticSeverityManager::new(),
            error_count: 0,
            warning_count: 0,
            note_count: 0,
        }
    }

    /// Print a diagnostic with full rendering.
    pub fn print(
        &mut self,
        id: DiagID,
        severity: DiagSeverity,
        message: &str,
        location: ClangSourceLocation,
        ranges: Vec<SourceRange>,
        notes: Vec<DiagnosticNote>,
        fixits: Vec<FixItHint>,
        source_lines: &[(usize, String)],
    ) -> io::Result<()> {
        // Track counts
        match severity {
            DiagSeverity::Error | DiagSeverity::Fatal => self.error_count += 1,
            DiagSeverity::Warning => self.warning_count += 1,
            DiagSeverity::Note => self.note_count += 1,
            _ => {}
        }

        let flag_name: Option<String> = DiagID::flag_name(&id).map(|s| s.to_string());
        let category_name: String = id.category().to_string();

        let diag = StructuredDiagnostic {
            id,
            severity,
            message: message.to_string(),
            location,
            ranges,
            notes,
            fixits,
            flag_name,
            category: category_name,
        };

        let rendered = self.renderer.render_diagnostic(&diag, source_lines);
        write!(self.writer, "{}", rendered)?;
        Ok(())
    }

    /// Print an error with message catalog lookup.
    pub fn error(
        &mut self,
        id: DiagID,
        location: ClangSourceLocation,
        args: &[String],
        source_lines: &[(usize, String)],
    ) -> io::Result<()> {
        let template = FullDiagnosticMessageCatalog::message_for(id);
        let message = Self::format_message(template, args);
        self.print(id, DiagSeverity::Error, &message, location, Vec::new(), Vec::new(), Vec::new(), source_lines)
    }

    /// Print a warning with message catalog lookup.
    pub fn warning(
        &mut self,
        id: DiagID,
        location: ClangSourceLocation,
        args: &[String],
        source_lines: &[(usize, String)],
    ) -> io::Result<()> {
        let template = FullDiagnosticMessageCatalog::message_for(id);
        let message = Self::format_message(template, args);
        self.print(id, DiagSeverity::Warning, &message, location, Vec::new(), Vec::new(), Vec::new(), source_lines)
    }

    /// Substitute `{}` placeholders in a message template.
    fn format_message(template: &str, args: &[String]) -> String {
        let mut result = template.to_string();
        for arg in args {
            if let Some(pos) = result.find("{}") {
                result.replace_range(pos..pos + 2, arg);
            }
        }
        result
    }

    /// Flush the writer.
    pub fn flush(&mut self) -> io::Result<()> {
        self.writer.flush()
    }
}

impl<W: Write> fmt::Debug for CompleteDiagnosticPrinter<W> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("CompleteDiagnosticPrinter")
            .field("error_count", &self.error_count)
            .field("warning_count", &self.warning_count)
            .field("note_count", &self.note_count)
            .finish()
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// Tests
// ═══════════════════════════════════════════════════════════════════════════════

#[cfg(test)]
mod tests {
    use super::*;
    use crate::clang::diagnostics::{
        ClangSourceLocation, DiagID, DiagSeverity, DiagnosticNote, FixItHint, SourceRange,
    };

    // ── Category Registry ─────────────────────────────────────────────────

    #[test]
    fn test_category_registry_has_wall_categories() {
        let reg = DiagnosticCategoryRegistry::new();
        assert!(reg.categories.contains_key("unused-variable"));
        assert!(reg.categories.contains_key("sign-compare"));
        assert!(reg.categories.contains_key("format"));
        assert!(!reg.is_empty());
    }

    #[test]
    fn test_category_registry_has_group_hierarchy() {
        let reg = DiagnosticCategoryRegistry::new();
        assert!(reg.group_to_members.contains_key("all"));
        assert!(reg.group_to_members.contains_key("extra"));
        assert!(reg.group_to_members.contains_key("conversion"));
    }

    #[test]
    fn test_category_registry_default_enabled() {
        let reg = DiagnosticCategoryRegistry::new();
        let cat = reg.categories.get("unused-variable").unwrap();
        assert!(cat.enabled_by_default);
    }

    #[test]
    fn test_category_registry_default_disabled() {
        let reg = DiagnosticCategoryRegistry::new();
        let cat = reg.categories.get("pedantic").unwrap();
        assert!(!cat.enabled_by_default);
    }

    #[test]
    fn test_category_registry_len() {
        let reg = DiagnosticCategoryRegistry::new();
        // Should have 100+ categories
        assert!(reg.len() > 100);
    }

    // ── Message Catalog ──────────────────────────────────────────────────

    #[test]
    fn test_message_catalog_syntax_errors() {
        assert_eq!(
            FullDiagnosticMessageCatalog::message_for(DiagID::ErrExpectedSemiAfterExpr),
            "expected ';' after expression"
        );
        assert_eq!(
            FullDiagnosticMessageCatalog::message_for(DiagID::ErrExpectedRBrace),
            "expected '}'"
        );
    }

    #[test]
    fn test_message_catalog_undeclared() {
        assert_eq!(
            FullDiagnosticMessageCatalog::message_for(DiagID::ErrUndeclaredIdentifier),
            "use of undeclared identifier '{}'"
        );
    }

    #[test]
    fn test_message_catalog_warnings() {
        assert_eq!(
            FullDiagnosticMessageCatalog::message_for(DiagID::WUnusedVariable),
            "unused variable '{}'"
        );
        assert_eq!(
            FullDiagnosticMessageCatalog::message_for(DiagID::WSignCompare),
            "comparison of integers of different signs: '{}' and '{}'"
        );
    }

    #[test]
    fn test_message_catalog_fatal() {
        assert_eq!(
            FullDiagnosticMessageCatalog::message_for(DiagID::FatalNoInputFiles),
            "no input files"
        );
    }

    // ── ANSI Colors ───────────────────────────────────────────────────────

    #[test]
    fn test_ansi_color_codes() {
        assert_eq!(AnsiColor::Red.code(), "\x1b[31m");
        assert_eq!(AnsiColor::Bold.code(), "\x1b[1m");
        assert_eq!(AnsiColor::Reset.code(), "\x1b[0m");
    }

    #[test]
    fn test_ansi_color_wrap() {
        let wrapped = AnsiColor::Red.wrap("error");
        assert!(wrapped.starts_with("\x1b[31m"));
        assert!(wrapped.ends_with("\x1b[0m"));
        assert!(wrapped.contains("error"));
    }

    // ── Color Scheme ──────────────────────────────────────────────────────

    #[test]
    fn test_color_scheme_clang() {
        let scheme = DiagnosticColorScheme::clang_style();
        assert!(scheme.use_colors);
        assert!(!scheme.error_colors.is_empty());
    }

    #[test]
    fn test_color_scheme_monochrome() {
        let scheme = DiagnosticColorScheme::monochrome();
        assert!(!scheme.use_colors);
    }

    #[test]
    fn test_color_scheme_colorize() {
        let scheme = DiagnosticColorScheme::clang_style();
        let text = scheme.colorize_error("error");
        assert!(text.contains("error"));
    }

    // ── Caret Renderer ───────────────────────────────────────────────────

    #[test]
    fn test_caret_line_single_range() {
        let scheme = DiagnosticColorScheme::monochrome();
        let line = "int x = 1 + y;";
        let ranges = vec![CaretRange::primary(12, 13)];
        let caret = CaretRenderer::build_caret_line(line, &ranges, &scheme);
        // Should contain at least a caret character
        assert!(caret.contains('^'));
    }

    #[test]
    fn test_caret_line_multiple_ranges() {
        let scheme = DiagnosticColorScheme::monochrome();
        let line = "int x = a + b;";
        let ranges = vec![
            CaretRange::primary(8, 9),
            CaretRange::new(12, 13),
        ];
        let caret = CaretRenderer::build_caret_line(line, &ranges, &scheme);
        assert!(caret.contains('^'));
        assert!(caret.contains('~'));
    }

    #[test]
    fn test_caret_line_empty_ranges() {
        let scheme = DiagnosticColorScheme::monochrome();
        let caret = CaretRenderer::build_caret_line("test", &[], &scheme);
        assert!(caret.is_empty());
    }

    // ── Source Snippet ───────────────────────────────────────────────────

    #[test]
    fn test_source_snippet_render() {
        let scheme = DiagnosticColorScheme::monochrome();
        let lines = vec![
            (1, "int main() {".to_string()),
            (2, "  return 0;".to_string()),
            (3, "}".to_string()),
        ];
        let snippet = SourceSnippet::new(lines.clone(), 2);
        let rendered = snippet.render(&scheme);
        assert!(rendered.contains("return 0;"));
    }

    // ── Full Diagnostic Renderer ──────────────────────────────────────────

    #[test]
    fn test_full_renderer_new() {
        let renderer = FullDiagnosticRenderer::new();
        assert!(renderer.show_column);
        assert!(renderer.show_carets);
    }

    #[test]
    fn test_full_renderer_for_ide() {
        let renderer = FullDiagnosticRenderer::for_ide();
        assert!(!renderer.color_scheme.use_colors);
    }

    // ── Suggestion Engine ─────────────────────────────────────────────────

    #[test]
    fn test_suggestion_engine_exact_match() {
        let engine = DiagnosticSuggestionEngine::with_names(
            ["printf", "scanf", "malloc"].iter().map(|s| s.to_string()),
        );
        let suggestion = engine.suggest("printf");
        assert_eq!(suggestion, Some("printf".to_string()));
    }

    #[test]
    fn test_suggestion_engine_close_match() {
        let engine = DiagnosticSuggestionEngine::with_names(
            ["printf", "scanf", "malloc", "free"].iter().map(|s| s.to_string()),
        );
        let suggestion = engine.suggest("pritnf"); // swapped t,n
        assert_eq!(suggestion, Some("printf".to_string()));
    }

    #[test]
    fn test_suggestion_engine_no_match() {
        let engine = DiagnosticSuggestionEngine::with_names(
            ["printf", "scanf"].iter().map(|s| s.to_string()),
        );
        let suggestion = engine.suggest("abcdefghijkl"); // far away
        assert!(suggestion.is_none());
    }

    #[test]
    fn test_suggestion_engine_did_you_mean() {
        let engine = DiagnosticSuggestionEngine::with_names(
            ["vector", "deque"].iter().map(|s| s.to_string()),
        );
        let msg = engine.did_you_mean("vectro");
        assert_eq!(msg, Some("did you mean 'vector'?".to_string()));
    }

    #[test]
    fn test_levenshtein_basic() {
        assert_eq!(levenshtein_distance("kitten", "sitting"), 3);
        assert_eq!(levenshtein_distance("abc", "abc"), 0);
        assert_eq!(levenshtein_distance("", "abc"), 3);
        assert_eq!(levenshtein_distance("abc", ""), 3);
    }

    // ── Severity Manager ──────────────────────────────────────────────────

    #[test]
    fn test_severity_manager_suppress() {
        let mut mgr = DiagnosticSeverityManager::new();
        mgr.suppress(DiagID::WUnusedVariable);
        assert!(mgr.is_suppressed(&DiagID::WUnusedVariable));
        assert!(!mgr.is_suppressed(&DiagID::WUnusedParameter));
    }

    #[test]
    fn test_severity_manager_file_override() {
        let mut mgr = DiagnosticSeverityManager::new();
        mgr.set_file_severity("test.c", DiagSeverity::Warning);
        let effective = mgr.effective_severity(
            &DiagID::WUnusedVariable,
            Some("test.c"),
            None,
            DiagSeverity::Ignored,
        );
        assert_eq!(effective, DiagSeverity::Warning);
    }

    #[test]
    fn test_severity_manager_category_override() {
        let mut mgr = DiagnosticSeverityManager::new();
        mgr.set_category_severity("unused-variable", DiagSeverity::Error);
        let effective = mgr.effective_severity(
            &DiagID::WUnusedVariable,
            None,
            Some("unused-variable"),
            DiagSeverity::Warning,
        );
        assert_eq!(effective, DiagSeverity::Error);
    }

    #[test]
    fn test_severity_manager_default_fallback() {
        let mgr = DiagnosticSeverityManager::new();
        let effective = mgr.effective_severity(
            &DiagID::WUnusedVariable,
            None,
            None,
            DiagSeverity::Warning,
        );
        assert_eq!(effective, DiagSeverity::Warning);
    }

    // ── Complete Diagnostic Printer ────────────────────────────────────────

    #[test]
    fn test_complete_printer_new() {
        let writer = Vec::new();
        let printer = CompleteDiagnosticPrinter::new(writer);
        assert_eq!(printer.error_count, 0);
        assert_eq!(printer.warning_count, 0);
    }

    #[test]
    fn test_format_message_simple() {
        let result = CompleteDiagnosticPrinter::<Vec<u8>>::format_message(
            "hello {} world",
            &["beautiful".to_string()],
        );
        assert_eq!(result, "hello beautiful world");
    }

    #[test]
    fn test_format_message_multiple() {
        let result = CompleteDiagnosticPrinter::<Vec<u8>>::format_message(
            "expected '{}' but got '{}'",
            &["int".to_string(), "float".to_string()],
        );
        assert_eq!(result, "expected 'int' but got 'float'");
    }

    #[test]
    fn test_caret_range_new() {
        let r = CaretRange::new(10, 15);
        assert_eq!(r.start_col, 10);
        assert_eq!(r.end_col, 15);
        assert!(!r.is_primary);
    }

    #[test]
    fn test_caret_range_primary() {
        let r = CaretRange::primary(5, 10);
        assert!(r.is_primary);
    }

    #[test]
    fn test_caret_range_with_message() {
        let r = CaretRange::with_message(3, 7, "type mismatch here");
        assert_eq!(r.message, Some("type mismatch here".to_string()));
    }
}