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
|
//===--- DiagnosticEngine.cpp - Diagnostic Display Engine -----------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file defines the DiagnosticEngine class, which manages any diagnostics
// emitted by Swift.
//
//===----------------------------------------------------------------------===//
#include "swift/AST/DiagnosticEngine.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/ASTPrinter.h"
#include "swift/AST/Decl.h"
#include "swift/AST/DiagnosticSuppression.h"
#include "swift/AST/DiagnosticsCommon.h"
#include "swift/AST/Expr.h"
#include "swift/AST/Module.h"
#include "swift/AST/Pattern.h"
#include "swift/AST/PrintOptions.h"
#include "swift/AST/SourceFile.h"
#include "swift/AST/Stmt.h"
#include "swift/AST/TypeRepr.h"
#include "swift/Basic/SourceManager.h"
#include "swift/Config.h"
#include "swift/Localization/LocalizationFormat.h"
#include "swift/Parse/Lexer.h" // bad dependency
#include "clang/AST/ASTContext.h"
#include "clang/AST/Decl.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/raw_ostream.h"
using namespace swift;
static_assert(IsTriviallyDestructible<ZeroArgDiagnostic>::value,
"ZeroArgDiagnostic is meant to be trivially destructable");
namespace {
enum class DiagnosticOptions {
/// No options.
none,
/// The location of this diagnostic points to the beginning of the first
/// token that the parser considers invalid. If this token is located at the
/// beginning of the line, then the location is adjusted to point to the end
/// of the previous token.
///
/// This behavior improves experience for "expected token X" diagnostics.
PointsToFirstBadToken,
/// After a fatal error subsequent diagnostics are suppressed.
Fatal,
/// An API or ABI breakage diagnostic emitted by the API digester.
APIDigesterBreakage,
/// A deprecation warning or error.
Deprecation,
/// A diagnostic warning about an unused element.
NoUsage,
};
struct StoredDiagnosticInfo {
DiagnosticKind kind : 2;
bool pointsToFirstBadToken : 1;
bool isFatal : 1;
bool isAPIDigesterBreakage : 1;
bool isDeprecation : 1;
bool isNoUsage : 1;
constexpr StoredDiagnosticInfo(DiagnosticKind k, bool firstBadToken,
bool fatal, bool isAPIDigesterBreakage,
bool deprecation, bool noUsage)
: kind(k), pointsToFirstBadToken(firstBadToken), isFatal(fatal),
isAPIDigesterBreakage(isAPIDigesterBreakage), isDeprecation(deprecation),
isNoUsage(noUsage) {}
constexpr StoredDiagnosticInfo(DiagnosticKind k, DiagnosticOptions opts)
: StoredDiagnosticInfo(k,
opts == DiagnosticOptions::PointsToFirstBadToken,
opts == DiagnosticOptions::Fatal,
opts == DiagnosticOptions::APIDigesterBreakage,
opts == DiagnosticOptions::Deprecation,
opts == DiagnosticOptions::NoUsage) {}
};
// Reproduce the DiagIDs, as we want both the size and access to the raw ids
// themselves.
enum LocalDiagID : uint32_t {
#define DIAG(KIND, ID, Options, Text, Signature) ID,
#include "swift/AST/DiagnosticsAll.def"
NumDiags
};
} // end anonymous namespace
// TODO: categorization
static const constexpr StoredDiagnosticInfo storedDiagnosticInfos[] = {
#define ERROR(ID, Options, Text, Signature) \
StoredDiagnosticInfo(DiagnosticKind::Error, DiagnosticOptions::Options),
#define WARNING(ID, Options, Text, Signature) \
StoredDiagnosticInfo(DiagnosticKind::Warning, DiagnosticOptions::Options),
#define NOTE(ID, Options, Text, Signature) \
StoredDiagnosticInfo(DiagnosticKind::Note, DiagnosticOptions::Options),
#define REMARK(ID, Options, Text, Signature) \
StoredDiagnosticInfo(DiagnosticKind::Remark, DiagnosticOptions::Options),
#include "swift/AST/DiagnosticsAll.def"
};
static_assert(sizeof(storedDiagnosticInfos) / sizeof(StoredDiagnosticInfo) ==
LocalDiagID::NumDiags,
"array size mismatch");
static constexpr const char * const diagnosticStrings[] = {
#define DIAG(KIND, ID, Options, Text, Signature) Text,
#include "swift/AST/DiagnosticsAll.def"
"<not a diagnostic>",
};
static constexpr const char *const debugDiagnosticStrings[] = {
#define DIAG(KIND, ID, Options, Text, Signature) Text " [" #ID "]",
#include "swift/AST/DiagnosticsAll.def"
"<not a diagnostic>",
};
static constexpr const char *const diagnosticIDStrings[] = {
#define DIAG(KIND, ID, Options, Text, Signature) #ID,
#include "swift/AST/DiagnosticsAll.def"
"<not a diagnostic>",
};
static constexpr const char *const fixItStrings[] = {
#define DIAG(KIND, ID, Options, Text, Signature)
#define FIXIT(ID, Text, Signature) Text,
#include "swift/AST/DiagnosticsAll.def"
"<not a fix-it>",
};
#define EDUCATIONAL_NOTES(DIAG, ...) \
static constexpr const char *const DIAG##_educationalNotes[] = {__VA_ARGS__, \
nullptr};
#include "swift/AST/EducationalNotes.def"
// NOTE: sadly, while GCC and Clang support array designators in C++, they are
// not part of the standard at the moment, so Visual C++ doesn't support them.
// This construct allows us to provide a constexpr array initialized to empty
// values except in the cases that EducationalNotes.def are provided, similar to
// what the C array would have looked like.
template<int N>
struct EducationalNotes {
constexpr EducationalNotes() : value() {
for (auto i = 0; i < N; ++i) value[i] = {};
#define EDUCATIONAL_NOTES(DIAG, ...) \
value[LocalDiagID::DIAG] = DIAG##_educationalNotes;
#include "swift/AST/EducationalNotes.def"
}
const char *const *value[N];
};
static constexpr EducationalNotes<LocalDiagID::NumDiags> _EducationalNotes = EducationalNotes<LocalDiagID::NumDiags>();
static constexpr auto educationalNotes = _EducationalNotes.value;
DiagnosticState::DiagnosticState() {
// Initialize our ignored diagnostics to default
ignoredDiagnostics.resize(LocalDiagID::NumDiags);
}
static CharSourceRange toCharSourceRange(SourceManager &SM, SourceRange SR) {
return CharSourceRange(SM, SR.Start, Lexer::getLocForEndOfToken(SM, SR.End));
}
static CharSourceRange toCharSourceRange(SourceManager &SM, SourceLoc Start,
SourceLoc End) {
return CharSourceRange(SM, Start, End);
}
/// Extract a character at \p Loc. If \p Loc is the end of the buffer,
/// return '\f'.
static char extractCharAfter(SourceManager &SM, SourceLoc Loc) {
auto chars = SM.extractText({Loc, 1});
return chars.empty() ? '\f' : chars[0];
}
/// Extract a character immediately before \p Loc. If \p Loc is the
/// start of the buffer, return '\f'.
static char extractCharBefore(SourceManager &SM, SourceLoc Loc) {
// We have to be careful not to go off the front of the buffer.
auto bufferID = SM.findBufferContainingLoc(Loc);
auto bufferRange = SM.getRangeForBuffer(bufferID);
if (bufferRange.getStart() == Loc)
return '\f';
auto chars = SM.extractText({Loc.getAdvancedLoc(-1), 1}, bufferID);
assert(!chars.empty() && "Couldn't extractText with valid range");
return chars[0];
}
InFlightDiagnostic &InFlightDiagnostic::highlight(SourceRange R) {
assert(IsActive && "Cannot modify an inactive diagnostic");
if (Engine && R.isValid())
Engine->getActiveDiagnostic()
.addRange(toCharSourceRange(Engine->SourceMgr, R));
return *this;
}
InFlightDiagnostic &InFlightDiagnostic::highlightChars(SourceLoc Start,
SourceLoc End) {
assert(IsActive && "Cannot modify an inactive diagnostic");
if (Engine && Start.isValid())
Engine->getActiveDiagnostic()
.addRange(toCharSourceRange(Engine->SourceMgr, Start, End));
return *this;
}
/// Add an insertion fix-it to the currently-active diagnostic. The
/// text is inserted immediately *after* the token specified.
///
InFlightDiagnostic &
InFlightDiagnostic::fixItInsertAfter(SourceLoc L, StringRef FormatString,
ArrayRef<DiagnosticArgument> Args) {
L = Lexer::getLocForEndOfToken(Engine->SourceMgr, L);
return fixItInsert(L, FormatString, Args);
}
/// Add a token-based removal fix-it to the currently-active
/// diagnostic.
InFlightDiagnostic &InFlightDiagnostic::fixItRemove(SourceRange R) {
assert(IsActive && "Cannot modify an inactive diagnostic");
if (R.isInvalid() || !Engine) return *this;
// Convert from a token range to a CharSourceRange, which points to the end of
// the token we want to remove.
auto &SM = Engine->SourceMgr;
auto charRange = toCharSourceRange(SM, R);
// If we're removing something (e.g. a keyword), do a bit of extra work to
// make sure that we leave the code in a good place, without extraneous white
// space around its hole. Specifically, check to see there is whitespace
// before and after the end of range. If so, nuke the space afterward to keep
// things consistent.
if (extractCharAfter(SM, charRange.getEnd()) == ' ' &&
isspace(extractCharBefore(SM, charRange.getStart()))) {
charRange = CharSourceRange(charRange.getStart(),
charRange.getByteLength()+1);
}
Engine->getActiveDiagnostic().addFixIt(Diagnostic::FixIt(charRange, {}, {}));
return *this;
}
InFlightDiagnostic &
InFlightDiagnostic::fixItReplace(SourceRange R, StringRef FormatString,
ArrayRef<DiagnosticArgument> Args) {
auto &SM = Engine->SourceMgr;
auto charRange = toCharSourceRange(SM, R);
Engine->getActiveDiagnostic().addFixIt(
Diagnostic::FixIt(charRange, FormatString, Args));
return *this;
}
InFlightDiagnostic &InFlightDiagnostic::fixItReplace(SourceRange R,
StringRef Str) {
if (Str.empty())
return fixItRemove(R);
assert(IsActive && "Cannot modify an inactive diagnostic");
if (R.isInvalid() || !Engine) return *this;
auto &SM = Engine->SourceMgr;
auto charRange = toCharSourceRange(SM, R);
// If we're replacing with something that wants spaces around it, do a bit of
// extra work so that we don't suggest extra spaces.
// FIXME: This could probably be applied to structured fix-its as well.
if (Str.back() == ' ') {
if (isspace(extractCharAfter(SM, charRange.getEnd())))
Str = Str.drop_back();
}
if (!Str.empty() && Str.front() == ' ') {
if (isspace(extractCharBefore(SM, charRange.getStart())))
Str = Str.drop_front();
}
return fixItReplace(R, "%0", {Str});
}
InFlightDiagnostic &
InFlightDiagnostic::fixItReplaceChars(SourceLoc Start, SourceLoc End,
StringRef FormatString,
ArrayRef<DiagnosticArgument> Args) {
assert(IsActive && "Cannot modify an inactive diagnostic");
if (Engine && Start.isValid())
Engine->getActiveDiagnostic().addFixIt(
Diagnostic::FixIt(toCharSourceRange(Engine->SourceMgr, Start, End),
FormatString, Args));
return *this;
}
SourceLoc
DiagnosticEngine::getBestAddImportFixItLoc(const Decl *Member,
SourceFile *sourceFile) const {
auto &SM = SourceMgr;
SourceLoc bestLoc;
auto SF =
sourceFile ? sourceFile : Member->getDeclContext()->getParentSourceFile();
if (!SF) {
return bestLoc;
}
for (auto item : SF->getTopLevelItems()) {
// If we found an import declaration, we want to insert after it.
if (auto importDecl =
dyn_cast_or_null<ImportDecl>(item.dyn_cast<Decl *>())) {
SourceLoc loc = importDecl->getEndLoc();
if (loc.isValid()) {
bestLoc = Lexer::getLocForEndOfLine(SM, loc);
}
// Keep looking for more import declarations.
continue;
}
// If we got a location based on import declarations, we're done.
if (bestLoc.isValid())
break;
// For any other item, we want to insert before it.
SourceLoc loc = item.getStartLoc();
if (loc.isValid()) {
bestLoc = Lexer::getLocForStartOfLine(SM, loc);
break;
}
}
return bestLoc;
}
InFlightDiagnostic &InFlightDiagnostic::fixItAddImport(StringRef ModuleName) {
assert(IsActive && "Cannot modify an inactive diagnostic");
auto Member = Engine->ActiveDiagnostic->getDecl();
SourceLoc bestLoc = Engine->getBestAddImportFixItLoc(Member);
if (bestLoc.isValid()) {
llvm::SmallString<64> importText;
// @_spi imports.
if (Member->isSPI()) {
auto spiGroups = Member->getSPIGroups();
if (!spiGroups.empty()) {
importText += "@_spi(";
importText += spiGroups[0].str();
importText += ") ";
}
}
importText += "import ";
importText += ModuleName;
importText += "\n";
return fixItInsert(bestLoc, importText);
}
return *this;
}
InFlightDiagnostic &InFlightDiagnostic::fixItExchange(SourceRange R1,
SourceRange R2) {
assert(IsActive && "Cannot modify an inactive diagnostic");
auto &SM = Engine->SourceMgr;
// Convert from a token range to a CharSourceRange
auto charRange1 = toCharSourceRange(SM, R1);
auto charRange2 = toCharSourceRange(SM, R2);
// Extract source text.
auto text1 = SM.extractText(charRange1);
auto text2 = SM.extractText(charRange2);
Engine->getActiveDiagnostic().addFixIt(
Diagnostic::FixIt(charRange1, "%0", {text2}));
Engine->getActiveDiagnostic().addFixIt(
Diagnostic::FixIt(charRange2, "%0", {text1}));
return *this;
}
InFlightDiagnostic &
InFlightDiagnostic::limitBehavior(DiagnosticBehavior limit) {
Engine->getActiveDiagnostic().setBehaviorLimit(limit);
return *this;
}
InFlightDiagnostic &
InFlightDiagnostic::limitBehaviorUntilSwiftVersion(
DiagnosticBehavior limit, unsigned majorVersion) {
if (!Engine->languageVersion.isVersionAtLeast(majorVersion)) {
// If the behavior limit is a warning or less, wrap the diagnostic
// in a message that this will become an error in a later Swift
// version. We do this before limiting the behavior, because
// wrapIn will result in the behavior of the wrapping diagnostic.
if (limit >= DiagnosticBehavior::Warning)
wrapIn(diag::error_in_future_swift_version, majorVersion);
limitBehavior(limit);
}
if (majorVersion == 6) {
if (auto stats = Engine->statsReporter) {
++stats->getFrontendCounters().NumSwift6Errors;
}
}
return *this;
}
InFlightDiagnostic &
InFlightDiagnostic::warnUntilSwiftVersion(unsigned majorVersion) {
return limitBehaviorUntilSwiftVersion(DiagnosticBehavior::Warning,
majorVersion);
}
InFlightDiagnostic &
InFlightDiagnostic::warnInSwiftInterface(const DeclContext *context) {
auto sourceFile = context->getParentSourceFile();
if (sourceFile && sourceFile->Kind == SourceFileKind::Interface) {
return limitBehavior(DiagnosticBehavior::Warning);
}
return *this;
}
InFlightDiagnostic &
InFlightDiagnostic::wrapIn(const Diagnostic &wrapper) {
// Save current active diagnostic into WrappedDiagnostics, ignoring state
// so we don't get a None return or influence future diagnostics.
DiagnosticState tempState;
Engine->state.swap(tempState);
llvm::SaveAndRestore<DiagnosticBehavior>
limit(Engine->getActiveDiagnostic().BehaviorLimit,
DiagnosticBehavior::Unspecified);
Engine->WrappedDiagnostics.push_back(
*Engine->diagnosticInfoForDiagnostic(Engine->getActiveDiagnostic()));
Engine->state.swap(tempState);
auto &wrapped = Engine->WrappedDiagnostics.back();
// Copy and update its arg list.
Engine->WrappedDiagnosticArgs.emplace_back(wrapped.FormatArgs);
wrapped.FormatArgs = Engine->WrappedDiagnosticArgs.back();
// Overwrite the ID and argument with those from the wrapper.
Engine->getActiveDiagnostic().ID = wrapper.ID;
Engine->getActiveDiagnostic().Args = wrapper.Args;
// Set the argument to the diagnostic being wrapped.
assert(wrapper.getArgs().front().getKind() == DiagnosticArgumentKind::Diagnostic);
Engine->getActiveDiagnostic().Args.front() = &wrapped;
return *this;
}
void InFlightDiagnostic::flush() {
if (!IsActive)
return;
IsActive = false;
if (Engine)
Engine->flushActiveDiagnostic();
}
void Diagnostic::addChildNote(Diagnostic &&D) {
assert(storedDiagnosticInfos[(unsigned)D.ID].kind == DiagnosticKind::Note &&
"Only notes can have a parent.");
assert(storedDiagnosticInfos[(unsigned)ID].kind != DiagnosticKind::Note &&
"Notes can't have children.");
ChildNotes.push_back(std::move(D));
}
bool DiagnosticEngine::isDiagnosticPointsToFirstBadToken(DiagID ID) const {
return storedDiagnosticInfos[(unsigned) ID].pointsToFirstBadToken;
}
bool DiagnosticEngine::isAPIDigesterBreakageDiagnostic(DiagID ID) const {
return storedDiagnosticInfos[(unsigned)ID].isAPIDigesterBreakage;
}
bool DiagnosticEngine::isDeprecationDiagnostic(DiagID ID) const {
return storedDiagnosticInfos[(unsigned)ID].isDeprecation;
}
bool DiagnosticEngine::isNoUsageDiagnostic(DiagID ID) const {
return storedDiagnosticInfos[(unsigned)ID].isNoUsage;
}
bool DiagnosticEngine::finishProcessing() {
bool hadError = false;
for (auto &Consumer : Consumers) {
hadError |= Consumer->finishProcessing();
}
return hadError;
}
/// Skip forward to one of the given delimiters.
///
/// \param Text The text to search through, which will be updated to point
/// just after the delimiter.
///
/// \param Delim The first character delimiter to search for.
///
/// \param FoundDelim On return, true if the delimiter was found, or false
/// if the end of the string was reached.
///
/// \returns The string leading up to the delimiter, or the empty string
/// if no delimiter is found.
static StringRef
skipToDelimiter(StringRef &Text, char Delim, bool *FoundDelim = nullptr) {
unsigned Depth = 0;
if (FoundDelim)
*FoundDelim = false;
unsigned I = 0;
for (unsigned N = Text.size(); I != N; ++I) {
if (Text[I] == '{') {
++Depth;
continue;
}
if (Depth > 0) {
if (Text[I] == '}')
--Depth;
continue;
}
if (Text[I] == Delim) {
if (FoundDelim)
*FoundDelim = true;
break;
}
}
assert(Depth == 0 && "Unbalanced {} set in diagnostic text");
StringRef Result = Text.substr(0, I);
Text = Text.substr(I + 1);
return Result;
}
/// Handle the integer 'select' modifier. This is used like this:
/// %select{foo|bar|baz}2. This means that the integer argument "%2" has a
/// value from 0-2. If the value is 0, the diagnostic prints 'foo'.
/// If the value is 1, it prints 'bar'. If it has the value 2, it prints 'baz'.
/// This is very useful for certain classes of variant diagnostics.
static void formatSelectionArgument(StringRef ModifierArguments,
ArrayRef<DiagnosticArgument> Args,
unsigned SelectedIndex,
DiagnosticFormatOptions FormatOpts,
llvm::raw_ostream &Out) {
bool foundPipe = false;
do {
assert((!ModifierArguments.empty() || foundPipe) &&
"Index beyond bounds in %select modifier");
StringRef Text = skipToDelimiter(ModifierArguments, '|', &foundPipe);
if (SelectedIndex == 0) {
DiagnosticEngine::formatDiagnosticText(Out, Text, Args, FormatOpts);
break;
}
--SelectedIndex;
} while (true);
}
static bool isInterestingTypealias(Type type) {
// Dig out the typealias declaration, if there is one.
TypeAliasDecl *aliasDecl = nullptr;
if (auto aliasTy = dyn_cast<TypeAliasType>(type.getPointer()))
aliasDecl = aliasTy->getDecl();
else
return false;
if (type->isVoid())
return false;
// The 'Swift.AnyObject' typealias is not 'interesting'.
if (aliasDecl->getName() ==
aliasDecl->getASTContext().getIdentifier("AnyObject") &&
(aliasDecl->getParentModule()->isStdlibModule() ||
aliasDecl->getParentModule()->isBuiltinModule())) {
return false;
}
// Compatibility aliases are only interesting insofar as their underlying
// types are interesting.
if (aliasDecl->isCompatibilityAlias()) {
auto underlyingTy = aliasDecl->getUnderlyingType();
return isInterestingTypealias(underlyingTy);
}
// Builtin types are never interesting typealiases.
if (type->is<BuiltinType>()) return false;
return true;
}
/// Walks the type recursively desugaring types to display, but skipping
/// `GenericTypeParamType` because we would lose association with its original
/// declaration and end up presenting the parameter in τ_0_0 format on
/// diagnostic.
static Type getAkaTypeForDisplay(Type type) {
return type.transform([](Type visitTy) -> Type {
if (isa<SugarType>(visitTy.getPointer()) &&
!isa<GenericTypeParamType>(visitTy.getPointer()))
return getAkaTypeForDisplay(visitTy->getDesugaredType());
return visitTy;
});
}
/// Decide whether to show the desugared type or not. We filter out some
/// cases to avoid too much noise.
static bool shouldShowAKA(Type type, StringRef typeName) {
// Canonical types are already desugared.
if (type->isCanonical())
return false;
// Only show 'aka' if there's a typealias involved; other kinds of sugar
// are easy enough for people to read on their own.
if (!type.findIf(isInterestingTypealias))
return false;
// If they are textually the same, don't show them. This can happen when
// they are actually different types, because they exist in different scopes
// (e.g. everyone names their type parameters 'T').
if (typeName == getAkaTypeForDisplay(type).getString())
return false;
return true;
}
/// If a type is part of an argument list which includes another, distinct type
/// with the same string representation, it should be qualified during
/// formatting.
static bool typeSpellingIsAmbiguous(Type type,
ArrayRef<DiagnosticArgument> Args,
PrintOptions &PO) {
for (auto arg : Args) {
if (arg.getKind() == DiagnosticArgumentKind::Type) {
auto argType = arg.getAsType();
if (argType && argType->getWithoutParens().getPointer() != type.getPointer() &&
argType->getWithoutParens().getString(PO) == type.getString(PO)) {
// Currently, existential types are spelled the same way
// as protocols and compositions. We can remove this once
// existenials are printed with 'any'.
if (type->is<ExistentialType>() || argType->isExistentialType()) {
auto constraint = type;
if (auto existential = type->getAs<ExistentialType>())
constraint = existential->getConstraintType();
auto argConstraint = argType;
if (auto existential = argType->getAs<ExistentialType>())
argConstraint = existential->getConstraintType();
if (constraint.getPointer() != argConstraint.getPointer())
return true;
continue;
}
return true;
}
}
}
return false;
}
void swift::printClangDeclName(const clang::NamedDecl *ND,
llvm::raw_ostream &os) {
ND->getNameForDiagnostic(os, ND->getASTContext().getPrintingPolicy(), false);
}
/// Format a single diagnostic argument and write it to the given
/// stream.
static void formatDiagnosticArgument(StringRef Modifier,
StringRef ModifierArguments,
ArrayRef<DiagnosticArgument> Args,
unsigned ArgIndex,
DiagnosticFormatOptions FormatOpts,
llvm::raw_ostream &Out) {
const DiagnosticArgument &Arg = Args[ArgIndex];
switch (Arg.getKind()) {
case DiagnosticArgumentKind::Integer:
if (Modifier == "select") {
assert(Arg.getAsInteger() >= 0 && "Negative selection index");
formatSelectionArgument(ModifierArguments, Args, Arg.getAsInteger(),
FormatOpts, Out);
} else if (Modifier == "s") {
if (Arg.getAsInteger() != 1)
Out << 's';
} else {
assert(Modifier.empty() && "Improper modifier for integer argument");
Out << Arg.getAsInteger();
}
break;
case DiagnosticArgumentKind::Unsigned:
if (Modifier == "select") {
formatSelectionArgument(ModifierArguments, Args, Arg.getAsUnsigned(),
FormatOpts, Out);
} else if (Modifier == "s") {
if (Arg.getAsUnsigned() != 1)
Out << 's';
} else {
assert(Modifier.empty() && "Improper modifier for unsigned argument");
Out << Arg.getAsUnsigned();
}
break;
case DiagnosticArgumentKind::String:
if (Modifier == "select") {
formatSelectionArgument(ModifierArguments, Args,
Arg.getAsString().empty() ? 0 : 1, FormatOpts,
Out);
} else {
assert(Modifier.empty() && "Improper modifier for string argument");
Out << Arg.getAsString();
}
break;
case DiagnosticArgumentKind::Identifier:
if (Modifier == "select") {
formatSelectionArgument(ModifierArguments, Args,
Arg.getAsIdentifier() ? 1 : 0, FormatOpts,
Out);
} else {
assert(Modifier.empty() && "Improper modifier for identifier argument");
Out << FormatOpts.OpeningQuotationMark;
Arg.getAsIdentifier().printPretty(Out);
Out << FormatOpts.ClosingQuotationMark;
}
break;
case DiagnosticArgumentKind::ObjCSelector:
assert(Modifier.empty() && "Improper modifier for selector argument");
Out << FormatOpts.OpeningQuotationMark << Arg.getAsObjCSelector()
<< FormatOpts.ClosingQuotationMark;
break;
case DiagnosticArgumentKind::Decl: {
auto D = Arg.getAsDecl();
if (Modifier == "select") {
formatSelectionArgument(ModifierArguments, Args, D ? 1 : 0, FormatOpts,
Out);
break;
}
// Parse info out of modifier
bool includeKind = false;
bool includeName = true;
bool baseNameOnly = false;
if (Modifier == "kind") {
includeKind = true;
} else if (Modifier == "base") {
baseNameOnly = true;
} else if (Modifier == "kindbase") {
includeKind = true;
baseNameOnly = true;
} else if (Modifier == "kindonly") {
includeName = false;
} else {
assert(Modifier.empty() && "Improper modifier for ValueDecl argument");
}
// If it's an accessor, describe that and then switch to discussing its
// storage.
if (auto accessor = dyn_cast<AccessorDecl>(D)) {
Out << Decl::getDescriptiveKindName(D->getDescriptiveKind()) << " for ";
D = accessor->getStorage();
}
// If it's an extension, describe that and then switch to discussing its
// nominal type.
if (auto ext = dyn_cast<ExtensionDecl>(D)) {
Out << Decl::getDescriptiveKindName(D->getDescriptiveKind()) << " of ";
D = ext->getSelfNominalTypeDecl();
}
// Figure out the name we want to print.
DeclName name;
if (includeName) {
if (auto VD = dyn_cast<ValueDecl>(D))
name = VD->getName();
else if (auto PGD = dyn_cast<PrecedenceGroupDecl>(D))
name = PGD->getName();
else if (auto OD = dyn_cast<OperatorDecl>(D))
name = OD->getName();
else if (auto MMD = dyn_cast<MissingMemberDecl>(D))
name = MMD->getName();
if (baseNameOnly && name)
name = name.getBaseName();
}
// If the declaration is anonymous or we asked for a descriptive kind, print
// it.
if (!name || includeKind) {
Out << Decl::getDescriptiveKindName(D->getDescriptiveKind());
if (name)
Out << " ";
}
// Print the name.
if (name) {
Out << FormatOpts.OpeningQuotationMark;
name.printPretty(Out);
Out << FormatOpts.ClosingQuotationMark;
}
break;
}
case DiagnosticArgumentKind::FullyQualifiedType:
case DiagnosticArgumentKind::Type:
case DiagnosticArgumentKind::WitnessType: {
std::optional<DiagnosticFormatOptions> TypeFormatOpts;
if (Modifier == "noformat") {
TypeFormatOpts.emplace(DiagnosticFormatOptions::formatForFixIts());
} else {
assert(Modifier.empty() && "Improper modifier for Type argument");
TypeFormatOpts.emplace(FormatOpts);
}
// Strip extraneous parentheses; they add no value.
Type type;
bool needsQualification = false;
// Compute the appropriate print options for this argument.
auto printOptions = PrintOptions::forDiagnosticArguments();
if (Arg.getKind() == DiagnosticArgumentKind::Type) {
type = Arg.getAsType()->getWithoutParens();
if (type.isNull()) {
// FIXME: We should never receive a nullptr here, but this is causing
// crashes (rdar://75740683). Remove once ParenType never contains
// nullptr as the underlying type.
Out << "<null>";
break;
}
if (type->getASTContext().TypeCheckerOpts.PrintFullConvention)
printOptions.PrintFunctionRepresentationAttrs =
PrintOptions::FunctionRepresentationMode::Full;
needsQualification = typeSpellingIsAmbiguous(type, Args, printOptions);
} else if (Arg.getKind() == DiagnosticArgumentKind::FullyQualifiedType) {
type = Arg.getAsFullyQualifiedType().getType()->getWithoutParens();
if (type.isNull()) {
// FIXME: We should never receive a nullptr here, but this is causing
// crashes (rdar://75740683). Remove once ParenType never contains
// nullptr as the underlying type.
Out << "<null>";
break;
}
if (type->getASTContext().TypeCheckerOpts.PrintFullConvention)
printOptions.PrintFunctionRepresentationAttrs =
PrintOptions::FunctionRepresentationMode::Full;
needsQualification = true;
} else {
assert(Arg.getKind() == DiagnosticArgumentKind::WitnessType);
type = Arg.getAsWitnessType().getType()->getWithoutParens();
if (type.isNull()) {
// FIXME: We should never receive a nullptr here, but this is causing
// crashes (rdar://75740683). Remove once ParenType never contains
// nullptr as the underlying type.
Out << "<null>";
break;
}
printOptions.PrintGenericRequirements = false;
printOptions.PrintInverseRequirements = false;
needsQualification = typeSpellingIsAmbiguous(type, Args, printOptions);
}
// If a type has an unresolved type, print it with syntax sugar removed for
// clarity. For example, print `Array<_>` instead of `[_]`.
if (type->hasUnresolvedType()) {
type = type->getWithoutSyntaxSugar();
}
if (needsQualification &&
isa<OpaqueTypeArchetypeType>(type.getPointer()) &&
cast<ArchetypeType>(type.getPointer())->isRoot()) {
auto opaqueTypeDecl = type->castTo<OpaqueTypeArchetypeType>()->getDecl();
llvm::SmallString<256> NamingDeclText;
llvm::raw_svector_ostream OutNaming(NamingDeclText);
auto namingDecl = opaqueTypeDecl->getNamingDecl();
if (namingDecl->getDeclContext()->isTypeContext()) {
auto selfTy = namingDecl->getDeclContext()->getSelfInterfaceType();
selfTy->print(OutNaming);
OutNaming << '.';
}
namingDecl->getName().printPretty(OutNaming);
auto descriptiveKind = opaqueTypeDecl->getDescriptiveKind();
Out << llvm::format(TypeFormatOpts->OpaqueResultFormatString.c_str(),
type->getString(printOptions).c_str(),
Decl::getDescriptiveKindName(descriptiveKind).data(),
NamingDeclText.c_str());
} else {
printOptions.FullyQualifiedTypes = needsQualification;
std::string typeName = type->getString(printOptions);
if (shouldShowAKA(type, typeName)) {
llvm::SmallString<256> AkaText;
llvm::raw_svector_ostream OutAka(AkaText);
getAkaTypeForDisplay(type)->print(OutAka, printOptions);
Out << llvm::format(TypeFormatOpts->AKAFormatString.c_str(),
typeName.c_str(), AkaText.c_str());
} else {
Out << TypeFormatOpts->OpeningQuotationMark << typeName
<< TypeFormatOpts->ClosingQuotationMark;
}
}
break;
}
case DiagnosticArgumentKind::TypeRepr:
assert(Modifier.empty() && "Improper modifier for TypeRepr argument");
assert(Arg.getAsTypeRepr() && "TypeRepr argument is null");
Out << FormatOpts.OpeningQuotationMark << Arg.getAsTypeRepr()
<< FormatOpts.ClosingQuotationMark;
break;
case DiagnosticArgumentKind::DescriptivePatternKind:
assert(Modifier.empty() &&
"Improper modifier for DescriptivePatternKind argument");
Out << Pattern::getDescriptivePatternKindName(
Arg.getAsDescriptivePatternKind());
break;
case DiagnosticArgumentKind::SelfAccessKind:
if (Modifier == "select") {
formatSelectionArgument(ModifierArguments, Args,
unsigned(Arg.getAsSelfAccessKind()),
FormatOpts, Out);
} else {
assert(Modifier.empty() &&
"Improper modifier for SelfAccessKind argument");
Out << Arg.getAsSelfAccessKind();
}
break;
case DiagnosticArgumentKind::ReferenceOwnership:
if (Modifier == "select") {
formatSelectionArgument(ModifierArguments, Args,
unsigned(Arg.getAsReferenceOwnership()),
FormatOpts, Out);
} else {
assert(Modifier.empty() &&
"Improper modifier for ReferenceOwnership argument");
Out << Arg.getAsReferenceOwnership();
}
break;
case DiagnosticArgumentKind::StaticSpellingKind:
if (Modifier == "select") {
formatSelectionArgument(ModifierArguments, Args,
unsigned(Arg.getAsStaticSpellingKind()),
FormatOpts, Out);
} else {
assert(Modifier.empty() &&
"Improper modifier for StaticSpellingKind argument");
Out << Arg.getAsStaticSpellingKind();
}
break;
case DiagnosticArgumentKind::DescriptiveDeclKind:
assert(Modifier.empty() &&
"Improper modifier for DescriptiveDeclKind argument");
Out << Decl::getDescriptiveKindName(Arg.getAsDescriptiveDeclKind());
break;
case DiagnosticArgumentKind::DescriptiveStmtKind:
assert(Modifier.empty() && "Improper modifier for StmtKind argument");
Out << Stmt::getDescriptiveKindName(Arg.getAsDescriptiveStmtKind());
break;
case DiagnosticArgumentKind::DeclAttribute:
assert(Modifier.empty() &&
"Improper modifier for DeclAttribute argument");
if (Arg.getAsDeclAttribute()->isDeclModifier())
Out << FormatOpts.OpeningQuotationMark
<< Arg.getAsDeclAttribute()->getAttrName()
<< FormatOpts.ClosingQuotationMark;
else
Out << '@' << Arg.getAsDeclAttribute()->getAttrName();
break;
case DiagnosticArgumentKind::VersionTuple:
assert(Modifier.empty() &&
"Improper modifier for VersionTuple argument");
Out << Arg.getAsVersionTuple().getAsString();
break;
case DiagnosticArgumentKind::LayoutConstraint:
assert(Modifier.empty() && "Improper modifier for LayoutConstraint argument");
Out << FormatOpts.OpeningQuotationMark << Arg.getAsLayoutConstraint()
<< FormatOpts.ClosingQuotationMark;
break;
case DiagnosticArgumentKind::ActorIsolation: {
assert(Modifier.empty() && "Improper modifier for ActorIsolation argument");
auto isolation = Arg.getAsActorIsolation();
isolation.printForDiagnostics(Out, FormatOpts.OpeningQuotationMark);
break;
}
case DiagnosticArgumentKind::Diagnostic: {
assert(Modifier.empty() && "Improper modifier for Diagnostic argument");
auto diagArg = Arg.getAsDiagnostic();
DiagnosticEngine::formatDiagnosticText(Out, diagArg->FormatString,
diagArg->FormatArgs);
break;
}
case DiagnosticArgumentKind::ClangDecl:
assert(Modifier.empty() && "Improper modifier for ClangDecl argument");
Out << FormatOpts.OpeningQuotationMark;
printClangDeclName(Arg.getAsClangDecl(), Out);
Out << FormatOpts.ClosingQuotationMark;
break;
}
}
/// Format the given diagnostic text and place the result in the given
/// buffer.
void DiagnosticEngine::formatDiagnosticText(
llvm::raw_ostream &Out, StringRef InText, ArrayRef<DiagnosticArgument> Args,
DiagnosticFormatOptions FormatOpts) {
while (!InText.empty()) {
size_t Percent = InText.find('%');
if (Percent == StringRef::npos) {
// Write the rest of the string; we're done.
Out.write(InText.data(), InText.size());
break;
}
// Write the string up to (but not including) the %, then drop that text
// (including the %).
Out.write(InText.data(), Percent);
InText = InText.substr(Percent + 1);
// '%%' -> '%'.
if (InText[0] == '%') {
Out.write('%');
InText = InText.substr(1);
continue;
}
// Parse an optional modifier.
StringRef Modifier;
{
size_t Length = InText.find_if_not(isalpha);
Modifier = InText.substr(0, Length);
InText = InText.substr(Length);
}
if (Modifier == "error") {
Out << StringRef("<<INTERNAL ERROR: encountered %error in diagnostic text>>");
continue;
}
// Parse the optional argument list for a modifier, which is brace-enclosed.
StringRef ModifierArguments;
if (InText[0] == '{') {
InText = InText.substr(1);
ModifierArguments = skipToDelimiter(InText, '}');
}
// Find the digit sequence, and parse it into an argument index.
size_t Length = InText.find_if_not(isdigit);
unsigned ArgIndex;
bool IndexParseFailed = InText.substr(0, Length).getAsInteger(10, ArgIndex);
if (IndexParseFailed) {
Out << StringRef("<<INTERNAL ERROR: unparseable argument index in diagnostic text>>");
continue;
}
InText = InText.substr(Length);
if (ArgIndex >= Args.size()) {
Out << StringRef("<<INTERNAL ERROR: out-of-range argument index in diagnostic text>>");
continue;
}
// Convert the argument to a string.
formatDiagnosticArgument(Modifier, ModifierArguments, Args, ArgIndex,
FormatOpts, Out);
}
}
static DiagnosticKind toDiagnosticKind(DiagnosticBehavior behavior) {
switch (behavior) {
case DiagnosticBehavior::Unspecified:
llvm_unreachable("unspecified behavior");
case DiagnosticBehavior::Ignore:
llvm_unreachable("trying to map an ignored diagnostic");
case DiagnosticBehavior::Error:
case DiagnosticBehavior::Fatal:
return DiagnosticKind::Error;
case DiagnosticBehavior::Note:
return DiagnosticKind::Note;
case DiagnosticBehavior::Warning:
return DiagnosticKind::Warning;
case DiagnosticBehavior::Remark:
return DiagnosticKind::Remark;
}
llvm_unreachable("Unhandled DiagnosticKind in switch.");
}
static
DiagnosticBehavior toDiagnosticBehavior(DiagnosticKind kind, bool isFatal) {
switch (kind) {
case DiagnosticKind::Note:
return DiagnosticBehavior::Note;
case DiagnosticKind::Error:
return isFatal ? DiagnosticBehavior::Fatal : DiagnosticBehavior::Error;
case DiagnosticKind::Warning:
return DiagnosticBehavior::Warning;
case DiagnosticKind::Remark:
return DiagnosticBehavior::Remark;
}
llvm_unreachable("Unhandled DiagnosticKind in switch.");
}
// A special option only for compiler writers that causes Diagnostics to assert
// when a failure diagnostic is emitted. Intended for use in the debugger.
llvm::cl::opt<bool> AssertOnError("swift-diagnostics-assert-on-error",
llvm::cl::init(false));
// A special option only for compiler writers that causes Diagnostics to assert
// when a warning diagnostic is emitted. Intended for use in the debugger.
llvm::cl::opt<bool> AssertOnWarning("swift-diagnostics-assert-on-warning",
llvm::cl::init(false));
DiagnosticBehavior DiagnosticState::determineBehavior(const Diagnostic &diag) {
// We determine how to handle a diagnostic based on the following rules
// 1) Map the diagnostic to its "intended" behavior, applying the behavior
// limit for this particular emission
// 2) If current state dictates a certain behavior, follow that
// 3) If the user ignored this specific diagnostic, follow that
// 4) If the user substituted a different behavior for this behavior, apply
// that change
// 5) Update current state for use during the next diagnostic
// 1) Map the diagnostic to its "intended" behavior, applying the behavior
// limit for this particular emission
auto diagInfo = storedDiagnosticInfos[(unsigned)diag.getID()];
DiagnosticBehavior lvl =
std::max(toDiagnosticBehavior(diagInfo.kind, diagInfo.isFatal),
diag.getBehaviorLimit());
assert(lvl != DiagnosticBehavior::Unspecified);
// 2) If current state dictates a certain behavior, follow that
// Notes relating to ignored diagnostics should also be ignored
if (previousBehavior == DiagnosticBehavior::Ignore
&& lvl == DiagnosticBehavior::Note)
lvl = DiagnosticBehavior::Ignore;
// Suppress diagnostics when in a fatal state, except for follow-on notes
if (fatalErrorOccurred)
if (!showDiagnosticsAfterFatalError && lvl != DiagnosticBehavior::Note)
lvl = DiagnosticBehavior::Ignore;
// 3) If the user ignored this specific diagnostic, follow that
if (ignoredDiagnostics[(unsigned)diag.getID()])
lvl = DiagnosticBehavior::Ignore;
// 4) If the user substituted a different behavior for this behavior, apply
// that change
if (lvl == DiagnosticBehavior::Warning) {
if (warningsAsErrors)
lvl = DiagnosticBehavior::Error;
if (suppressWarnings)
lvl = DiagnosticBehavior::Ignore;
}
if (lvl == DiagnosticBehavior::Remark) {
if (suppressRemarks)
lvl = DiagnosticBehavior::Ignore;
}
// 5) Update current state for use during the next diagnostic
if (lvl == DiagnosticBehavior::Fatal) {
fatalErrorOccurred = true;
anyErrorOccurred = true;
} else if (lvl == DiagnosticBehavior::Error) {
anyErrorOccurred = true;
}
assert((!AssertOnError || !anyErrorOccurred) && "We emitted an error?!");
assert((!AssertOnWarning || (lvl != DiagnosticBehavior::Warning)) &&
"We emitted a warning?!");
previousBehavior = lvl;
return lvl;
}
void DiagnosticEngine::flushActiveDiagnostic() {
assert(ActiveDiagnostic && "No active diagnostic to flush");
handleDiagnostic(std::move(*ActiveDiagnostic));
ActiveDiagnostic.reset();
}
void DiagnosticEngine::handleDiagnostic(Diagnostic &&diag) {
if (TransactionCount == 0) {
emitDiagnostic(diag);
WrappedDiagnostics.clear();
WrappedDiagnosticArgs.clear();
} else {
onTentativeDiagnosticFlush(diag);
TentativeDiagnostics.emplace_back(std::move(diag));
}
}
void DiagnosticEngine::clearTentativeDiagnostics() {
TentativeDiagnostics.clear();
WrappedDiagnostics.clear();
WrappedDiagnosticArgs.clear();
}
void DiagnosticEngine::emitTentativeDiagnostics() {
for (auto &diag : TentativeDiagnostics) {
emitDiagnostic(diag);
}
clearTentativeDiagnostics();
}
void DiagnosticEngine::forwardTentativeDiagnosticsTo(
DiagnosticEngine &targetEngine) {
for (auto &diag : TentativeDiagnostics) {
targetEngine.handleDiagnostic(std::move(diag));
}
clearTentativeDiagnostics();
}
/// Returns the access level of the least accessible PrettyPrintedDeclarations
/// buffer that \p decl should appear in.
///
/// This is always \c Public unless \p decl is a \c ValueDecl and its
/// access level is below \c Public. (That can happen with @testable and
/// @_private imports.)
static AccessLevel getBufferAccessLevel(const Decl *decl) {
AccessLevel level = AccessLevel::Public;
if (auto *VD = dyn_cast<ValueDecl>(decl))
level = VD->getFormalAccessScope().accessLevelForDiagnostics();
if (level > AccessLevel::Public) level = AccessLevel::Public;
return level;
}
std::optional<DiagnosticInfo>
DiagnosticEngine::diagnosticInfoForDiagnostic(const Diagnostic &diagnostic) {
auto behavior = state.determineBehavior(diagnostic);
if (behavior == DiagnosticBehavior::Ignore)
return std::nullopt;
// Figure out the source location.
SourceLoc loc = diagnostic.getLoc();
if (loc.isInvalid() && diagnostic.getDecl()) {
const Decl *decl = diagnostic.getDecl();
// If a declaration was provided instead of a location, and that declaration
// has a location we can point to, use that location.
loc = decl->getLoc();
if (loc.isInvalid()) {
// There is no location we can point to. Pretty-print the declaration
// so we can point to it.
SourceLoc ppLoc = PrettyPrintedDeclarations[decl];
if (ppLoc.isInvalid()) {
class TrackingPrinter : public StreamPrinter {
SmallVectorImpl<std::pair<const Decl *, uint64_t>> &Entries;
AccessLevel bufferAccessLevel;
public:
TrackingPrinter(
SmallVectorImpl<std::pair<const Decl *, uint64_t>> &Entries,
raw_ostream &OS, AccessLevel bufferAccessLevel) :
StreamPrinter(OS), Entries(Entries),
bufferAccessLevel(bufferAccessLevel) {}
void printDeclLoc(const Decl *D) override {
if (getBufferAccessLevel(D) == bufferAccessLevel)
Entries.push_back({ D, OS.tell() });
}
};
SmallVector<std::pair<const Decl *, uint64_t>, 8> entries;
llvm::SmallString<128> buffer;
llvm::SmallString<128> bufferName;
const Decl *ppDecl = decl;
{
// The access level of the buffer we want to print. Declarations below
// this access level will be omitted from the buffer; declarations
// above it will be printed, but (except for Open declarations in the
// Public buffer) will not be recorded in PrettyPrintedDeclarations as
// the "true" SourceLoc for the declaration.
AccessLevel bufferAccessLevel = getBufferAccessLevel(decl);
// Figure out which declaration to print. It's the top-most
// declaration (not a module).
auto dc = decl->getDeclContext();
// FIXME: Horrible, horrible hackaround. We're not getting a
// DeclContext everywhere we should.
if (!dc) {
return std::nullopt;
}
while (!dc->isModuleContext()) {
switch (dc->getContextKind()) {
case DeclContextKind::Package:
llvm_unreachable("Not in a package context!");
break;
case DeclContextKind::Module:
llvm_unreachable("Not in a module context!");
break;
case DeclContextKind::FileUnit:
case DeclContextKind::TopLevelCodeDecl:
case DeclContextKind::SerializedTopLevelCodeDecl:
break;
case DeclContextKind::ExtensionDecl:
ppDecl = cast<ExtensionDecl>(dc);
break;
case DeclContextKind::GenericTypeDecl:
ppDecl = cast<GenericTypeDecl>(dc);
break;
case DeclContextKind::Initializer:
case DeclContextKind::AbstractClosureExpr:
case DeclContextKind::SerializedAbstractClosure:
case DeclContextKind::AbstractFunctionDecl:
case DeclContextKind::SubscriptDecl:
case DeclContextKind::EnumElementDecl:
case DeclContextKind::MacroDecl:
break;
}
dc = dc->getParent();
}
// Build the module name path (in reverse), which we use to
// build the name of the buffer.
SmallVector<StringRef, 4> nameComponents;
while (dc) {
nameComponents.push_back(cast<ModuleDecl>(dc)->getName().str());
dc = dc->getParent();
}
for (unsigned i = nameComponents.size(); i; --i) {
bufferName += nameComponents[i-1];
bufferName += '.';
}
if (auto value = dyn_cast<ValueDecl>(ppDecl)) {
bufferName += value->getBaseName().userFacingName();
} else if (auto ext = dyn_cast<ExtensionDecl>(ppDecl)) {
bufferName += ext->getExtendedType().getString();
}
// If we're using a lowered access level, give the buffer a distinct
// name.
if (bufferAccessLevel != AccessLevel::Public) {
assert(bufferAccessLevel < AccessLevel::Public
&& "Above-public access levels should use public buffer");
bufferName += " (";
bufferName += getAccessLevelSpelling(bufferAccessLevel);
bufferName += ")";
}
// Pretty-print the declaration we've picked.
llvm::raw_svector_ostream out(buffer);
TrackingPrinter printer(entries, out, bufferAccessLevel);
llvm::SaveAndRestore<bool> isPrettyPrinting(
IsPrettyPrintingDecl, true);
ppDecl->print(
printer,
PrintOptions::printForDiagnostics(
bufferAccessLevel,
decl->getASTContext().TypeCheckerOpts.PrintFullConvention));
}
// Build a buffer with the pretty-printed declaration.
auto bufferID = SourceMgr.addMemBufferCopy(buffer, bufferName);
auto memBufferStartLoc = SourceMgr.getLocForBufferStart(bufferID);
SourceMgr.setGeneratedSourceInfo(
bufferID,
GeneratedSourceInfo{
GeneratedSourceInfo::PrettyPrinted,
CharSourceRange(),
CharSourceRange(memBufferStartLoc, buffer.size()),
ASTNode(const_cast<Decl *>(ppDecl)).getOpaqueValue(),
nullptr
}
);
// Go through all of the pretty-printed entries and record their
// locations.
for (auto entry : entries) {
PrettyPrintedDeclarations[entry.first] =
memBufferStartLoc.getAdvancedLoc(entry.second);
}
// Grab the pretty-printed location.
ppLoc = PrettyPrintedDeclarations[decl];
}
loc = ppLoc;
}
}
StringRef Category;
if (isAPIDigesterBreakageDiagnostic(diagnostic.getID()))
Category = "api-digester-breaking-change";
else if (isDeprecationDiagnostic(diagnostic.getID()))
Category = "deprecation";
else if (isNoUsageDiagnostic(diagnostic.getID()))
Category = "no-usage";
auto fixIts = diagnostic.getFixIts();
if (loc.isValid()) {
// If the diagnostic is being emitted in a generated buffer, drop the
// fix-its, as the user will have no way of applying them.
auto bufferID = SourceMgr.findBufferContainingLoc(loc);
if (auto generatedInfo = SourceMgr.getGeneratedSourceInfo(bufferID)) {
switch (generatedInfo->kind) {
#define MACRO_ROLE(Name, Description) \
case GeneratedSourceInfo::Name##MacroExpansion:
#include "swift/Basic/MacroRoles.def"
case GeneratedSourceInfo::PrettyPrinted:
case GeneratedSourceInfo::DefaultArgument:
fixIts = {};
break;
case GeneratedSourceInfo::ReplacedFunctionBody:
// A replaced function body is for user-written code, so fix-its are
// still valid.
break;
}
}
}
return DiagnosticInfo(
diagnostic.getID(), loc, toDiagnosticKind(behavior),
diagnosticStringFor(diagnostic.getID(), getPrintDiagnosticNames()),
diagnostic.getArgs(), Category, getDefaultDiagnosticLoc(),
/*child note info*/ {}, diagnostic.getRanges(), fixIts,
diagnostic.isChildNote());
}
std::vector<Diagnostic>
DiagnosticEngine::getGeneratedSourceBufferNotes(SourceLoc loc) {
// The set of child notes we're building up.
std::vector<Diagnostic> childNotes;
// If the location is invalid, there's nothing to do.
if (loc.isInvalid())
return childNotes;
// If we already emitted these notes for a prior part of the diagnostic,
// don't do so again.
auto currentBufferID = SourceMgr.findBufferContainingLoc(loc);
SourceLoc currentLoc = loc;
do {
auto generatedInfo = SourceMgr.getGeneratedSourceInfo(currentBufferID);
if (!generatedInfo)
return childNotes;
ASTNode expansionNode =
ASTNode::getFromOpaqueValue(generatedInfo->astNode);
switch (generatedInfo->kind) {
#define MACRO_ROLE(Name, Description) \
case GeneratedSourceInfo::Name##MacroExpansion:
#include "swift/Basic/MacroRoles.def"
{
DeclName macroName = getGeneratedSourceInfoMacroName(*generatedInfo);
// If it was an expansion of an attached macro, increase the range to
// include the decl's attributes. Also add the name of the decl the macro
// is attached to.
CustomAttr *attachedAttr = generatedInfo->attachedMacroCustomAttr;
Decl *attachedDecl =
attachedAttr ? expansionNode.dyn_cast<Decl *>() : nullptr;
SourceRange origRange = attachedDecl
? attachedDecl->getSourceRangeIncludingAttrs()
: expansionNode.getSourceRange();
Diagnostic expansionNote(diag::in_macro_expansion, macroName,
attachedDecl);
if (attachedAttr) {
expansionNote.setLoc(attachedAttr->getLocation());
} else {
expansionNote.setLoc(origRange.Start);
}
expansionNote.addRange(
Lexer::getCharSourceRangeFromSourceRange(SourceMgr, origRange));
expansionNote.setIsChildNote(true);
childNotes.push_back(std::move(expansionNote));
break;
}
case GeneratedSourceInfo::PrettyPrinted:
break;
case GeneratedSourceInfo::DefaultArgument:
case GeneratedSourceInfo::ReplacedFunctionBody:
return childNotes;
}
// Walk up the stack.
currentLoc = expansionNode.getStartLoc();
if (currentLoc.isInvalid())
return childNotes;
currentBufferID = SourceMgr.findBufferContainingLoc(currentLoc);
} while (true);
}
void DiagnosticEngine::emitDiagnostic(const Diagnostic &diagnostic) {
ArrayRef<Diagnostic> childNotes = diagnostic.getChildNotes();
std::vector<Diagnostic> extendedChildNotes;
if (auto info = diagnosticInfoForDiagnostic(diagnostic)) {
// If the diagnostic location is within a buffer containing generated
// source code, add child notes showing where the generation occurred.
// We need to avoid doing this if this is itself a child note, as otherwise
// we'd end up doubling up on notes.
if (!info->IsChildNote) {
extendedChildNotes = getGeneratedSourceBufferNotes(info->Loc);
}
if (!extendedChildNotes.empty()) {
extendedChildNotes.insert(extendedChildNotes.end(),
childNotes.begin(), childNotes.end());
childNotes = extendedChildNotes;
}
SmallVector<DiagnosticInfo, 1> childInfo;
for (unsigned i : indices(childNotes)) {
auto child = diagnosticInfoForDiagnostic(childNotes[i]);
assert(child);
assert(child->Kind == DiagnosticKind::Note &&
"Expected child diagnostics to all be notes?!");
childInfo.push_back(*child);
}
TinyPtrVector<DiagnosticInfo *> childInfoPtrs;
for (unsigned i : indices(childInfo)) {
childInfoPtrs.push_back(&childInfo[i]);
}
info->ChildDiagnosticInfo = childInfoPtrs;
SmallVector<std::string, 1> educationalNotePaths;
auto associatedNotes = educationalNotes[(uint32_t)diagnostic.getID()];
while (associatedNotes && *associatedNotes) {
SmallString<128> notePath(getDiagnosticDocumentationPath());
llvm::sys::path::append(notePath, *associatedNotes);
educationalNotePaths.push_back(notePath.str().str());
++associatedNotes;
}
info->EducationalNotePaths = educationalNotePaths;
for (auto &consumer : Consumers) {
consumer->handleDiagnostic(SourceMgr, *info);
}
}
// For compatibility with DiagnosticConsumers which don't know about child
// notes. These can be ignored by consumers which do take advantage of the
// grouping.
for (auto &childNote : childNotes)
emitDiagnostic(childNote);
}
DiagnosticKind DiagnosticEngine::declaredDiagnosticKindFor(const DiagID id) {
return storedDiagnosticInfos[(unsigned)id].kind;
}
llvm::StringRef
DiagnosticEngine::diagnosticStringFor(const DiagID id,
bool printDiagnosticNames) {
auto defaultMessage = printDiagnosticNames
? debugDiagnosticStrings[(unsigned)id]
: diagnosticStrings[(unsigned)id];
if (auto producer = localization.get()) {
auto localizedMessage = producer->getMessageOr(id, defaultMessage);
return localizedMessage;
}
return defaultMessage;
}
llvm::StringRef
DiagnosticEngine::diagnosticIDStringFor(const DiagID id) {
return diagnosticIDStrings[(unsigned)id];
}
const char *InFlightDiagnostic::fixItStringFor(const FixItID id) {
return fixItStrings[(unsigned)id];
}
void DiagnosticEngine::setBufferIndirectlyCausingDiagnosticToInput(
SourceLoc loc) {
// If in the future, nested BufferIndirectlyCausingDiagnosticRAII need be
// supported, the compiler will need a stack for
// bufferIndirectlyCausingDiagnostic.
assert(bufferIndirectlyCausingDiagnostic.isInvalid() &&
"Buffer should not already be set.");
bufferIndirectlyCausingDiagnostic = loc;
assert(bufferIndirectlyCausingDiagnostic.isValid() &&
"Buffer must be valid for previous assertion to work.");
}
void DiagnosticEngine::resetBufferIndirectlyCausingDiagnostic() {
bufferIndirectlyCausingDiagnostic = SourceLoc();
}
DiagnosticSuppression::DiagnosticSuppression(DiagnosticEngine &diags)
: diags(diags)
{
consumers = diags.takeConsumers();
}
DiagnosticSuppression::~DiagnosticSuppression() {
for (auto consumer : consumers)
diags.addConsumer(*consumer);
}
bool DiagnosticSuppression::isEnabled(const DiagnosticEngine &diags) {
return diags.getConsumers().empty();
}
BufferIndirectlyCausingDiagnosticRAII::BufferIndirectlyCausingDiagnosticRAII(
const SourceFile &SF)
: Diags(SF.getASTContext().Diags) {
auto id = SF.getBufferID();
if (!id)
return;
auto loc = SF.getASTContext().SourceMgr.getLocForBufferStart(*id);
if (loc.isValid())
Diags.setBufferIndirectlyCausingDiagnosticToInput(loc);
}
void DiagnosticEngine::onTentativeDiagnosticFlush(Diagnostic &diagnostic) {
for (auto &argument : diagnostic.Args) {
if (argument.getKind() != DiagnosticArgumentKind::String)
continue;
auto content = argument.getAsString();
if (content.empty())
continue;
auto I = TransactionStrings.insert(content).first;
argument = DiagnosticArgument(StringRef(I->getKeyData()));
}
}
EncodedDiagnosticMessage::EncodedDiagnosticMessage(StringRef S)
: Message(Lexer::getEncodedStringSegment(S, Buf, /*IsFirstSegment=*/true,
/*IsLastSegment=*/true,
/*IndentToStrip=*/~0U)) {}
DeclName
swift::getGeneratedSourceInfoMacroName(const GeneratedSourceInfo &info) {
ASTNode expansionNode = ASTNode::getFromOpaqueValue(info.astNode);
switch (info.kind) {
#define MACRO_ROLE(Name, Description) \
case GeneratedSourceInfo::Name##MacroExpansion:
#include "swift/Basic/MacroRoles.def"
{
DeclName macroName;
if (auto customAttr = info.attachedMacroCustomAttr) {
// FIXME: How will we handle deserialized custom attributes like this?
auto declRefType = cast<DeclRefTypeRepr>(customAttr->getTypeRepr());
return declRefType->getNameRef().getFullName();
}
if (auto expansionExpr = dyn_cast_or_null<MacroExpansionExpr>(
expansionNode.dyn_cast<Expr *>())) {
return expansionExpr->getMacroName().getFullName();
}
auto expansionDecl =
cast<MacroExpansionDecl>(expansionNode.get<Decl *>());
return expansionDecl->getMacroName().getFullName();
}
case GeneratedSourceInfo::PrettyPrinted:
case GeneratedSourceInfo::ReplacedFunctionBody:
case GeneratedSourceInfo::DefaultArgument:
return DeclName();
}
}
|