emitted_diagnostics_test.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include <gtest/gtest.h>
  5. #include <fstream>
  6. #include <string>
  7. #include "common/set.h"
  8. #include "llvm/ADT/StringExtras.h"
  9. #include "re2/re2.h"
  10. #include "toolchain/diagnostics/diagnostic_kind.h"
  11. namespace Carbon {
  12. namespace {
  13. constexpr DiagnosticKind Diagnostics[] = {
  14. #define CARBON_DIAGNOSTIC_KIND(Name) DiagnosticKind::Name,
  15. #include "toolchain/diagnostics/diagnostic_kind.def"
  16. };
  17. // Returns true for diagnostics which have no tests. In general, diagnostics
  18. // should be tested.
  19. static auto IsUntestedDiagnostic(DiagnosticKind diagnostic_kind) -> bool {
  20. switch (diagnostic_kind) {
  21. case DiagnosticKind::TestDiagnostic:
  22. case DiagnosticKind::TestDiagnosticNote:
  23. // These exist only for unit tests.
  24. return true;
  25. case DiagnosticKind::ErrorReadingFile:
  26. case DiagnosticKind::ErrorStattingFile:
  27. case DiagnosticKind::FileTooLarge:
  28. // These diagnose filesystem issues that are hard to unit test.
  29. return true;
  30. case DiagnosticKind::ArrayBoundTooLarge:
  31. // Int literals are currently limited to i32. Once that's fixed, this
  32. // should be tested.
  33. return true;
  34. case DiagnosticKind::ExternLibraryInImporter:
  35. case DiagnosticKind::ExternLibraryOnDefinition:
  36. case DiagnosticKind::HexadecimalEscapeMissingDigits:
  37. case DiagnosticKind::ImplOfUndefinedInterface:
  38. case DiagnosticKind::IncompleteTypeInFunctionParam:
  39. case DiagnosticKind::InvalidDigit:
  40. case DiagnosticKind::InvalidDigitSeparator:
  41. case DiagnosticKind::InvalidHorizontalWhitespaceInString:
  42. case DiagnosticKind::MismatchedIndentInString:
  43. case DiagnosticKind::ModifierPrivateNotAllowed:
  44. case DiagnosticKind::MultiLineStringWithDoubleQuotes:
  45. case DiagnosticKind::NameAmbiguousDueToExtend:
  46. case DiagnosticKind::TooManyDigits:
  47. case DiagnosticKind::UnaryOperatorRequiresWhitespace:
  48. case DiagnosticKind::UnicodeEscapeSurrogate:
  49. case DiagnosticKind::UnicodeEscapeTooLarge:
  50. case DiagnosticKind::UnknownBaseSpecifier:
  51. case DiagnosticKind::UnsupportedCRLineEnding:
  52. case DiagnosticKind::UnsupportedLFCRLineEnding:
  53. // TODO: Should look closer at these, but adding tests is a high risk of
  54. // loss in merge conflicts due to the amount of tests being changed right
  55. // now.
  56. return true;
  57. default:
  58. return false;
  59. }
  60. }
  61. TEST(EmittedDiagnostics, Verify) {
  62. std::ifstream manifest_in("toolchain/diagnostics/all_testdata.txt");
  63. ASSERT_TRUE(manifest_in.good());
  64. RE2 diagnostic_re(R"(\w\((\w+)\): )");
  65. ASSERT_TRUE(diagnostic_re.ok()) << diagnostic_re.error();
  66. Set<std::string> emitted_diagnostics;
  67. std::string test_filename;
  68. while (std::getline(manifest_in, test_filename)) {
  69. std::ifstream test_in(test_filename);
  70. ASSERT_TRUE(test_in.good());
  71. std::string line;
  72. while (std::getline(test_in, line)) {
  73. std::string diagnostic;
  74. if (RE2::PartialMatch(line, diagnostic_re, &diagnostic)) {
  75. emitted_diagnostics.Insert(diagnostic);
  76. }
  77. }
  78. }
  79. llvm::SmallVector<llvm::StringRef> missing_diagnostics;
  80. for (auto diagnostic_kind : Diagnostics) {
  81. if (IsUntestedDiagnostic(diagnostic_kind)) {
  82. EXPECT_FALSE(emitted_diagnostics.Erase(diagnostic_kind.name()))
  83. << diagnostic_kind
  84. << " was previously untested, and is now tested. That's good, but "
  85. "please remove it from IsUntestedDiagnostic.";
  86. continue;
  87. }
  88. if (!emitted_diagnostics.Erase(diagnostic_kind.name())) {
  89. missing_diagnostics.push_back(diagnostic_kind.name());
  90. }
  91. }
  92. constexpr llvm::StringLiteral Bullet = "\n - ";
  93. std::sort(missing_diagnostics.begin(), missing_diagnostics.end());
  94. EXPECT_TRUE(missing_diagnostics.empty())
  95. << "Some diagnostics have no tests:" << Bullet
  96. << llvm::join(missing_diagnostics, Bullet);
  97. llvm::SmallVector<std::string> unexpected_matches;
  98. emitted_diagnostics.ForEach(
  99. [&](const std::string& match) { unexpected_matches.push_back(match); });
  100. std::sort(unexpected_matches.begin(), unexpected_matches.end());
  101. EXPECT_TRUE(unexpected_matches.empty())
  102. << "Matched things that don't appear to be diagnostics:" << Bullet
  103. << llvm::join(unexpected_matches, Bullet);
  104. }
  105. } // namespace
  106. } // namespace Carbon