|
| 1 | +//===--- DiagnosticInfoTests.cpp --------------------------------*- C++ -*-===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2025 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include "swift/AST/DiagnosticEngine.h" |
| 14 | +#include "swift/AST/DiagnosticGroups.h" |
| 15 | +#include "swift/AST/DiagnosticsFrontend.h" |
| 16 | +#include "swift/Basic/SourceManager.h" |
| 17 | +#include "gtest/gtest.h" |
| 18 | + |
| 19 | +using namespace swift; |
| 20 | + |
| 21 | +namespace { |
| 22 | +class TestDiagnosticConsumer : public DiagnosticConsumer { |
| 23 | + llvm::function_ref<void(const DiagnosticInfo &)> callback; |
| 24 | + |
| 25 | +public: |
| 26 | + TestDiagnosticConsumer(decltype(callback) callback) : callback(callback) {} |
| 27 | + |
| 28 | + void handleDiagnostic(SourceManager &SM, |
| 29 | + const DiagnosticInfo &Info) override { |
| 30 | + this->callback(Info); |
| 31 | + } |
| 32 | +}; |
| 33 | + |
| 34 | +struct TestDiagnostic : public Diagnostic { |
| 35 | + TestDiagnostic(DiagID ID, DiagGroupID GroupID) : Diagnostic(ID, GroupID) {} |
| 36 | +}; |
| 37 | + |
| 38 | +static void |
| 39 | +testCase(llvm::function_ref<void(DiagnosticEngine &)> diagnose, |
| 40 | + llvm::function_ref<void(DiagnosticEngine &, const DiagnosticInfo &)> |
| 41 | + callback, |
| 42 | + unsigned expectedNumCallbackCalls) { |
| 43 | + SourceManager sourceMgr; |
| 44 | + DiagnosticEngine diags(sourceMgr); |
| 45 | + |
| 46 | + unsigned count = 0; |
| 47 | + |
| 48 | + const auto countingCallback = [&](const DiagnosticInfo &info) { |
| 49 | + ++count; |
| 50 | + callback(diags, info); |
| 51 | + }; |
| 52 | + |
| 53 | + TestDiagnosticConsumer consumer(countingCallback); |
| 54 | + diags.addConsumer(consumer); |
| 55 | + diagnose(diags); |
| 56 | + diags.removeConsumer(consumer); |
| 57 | + |
| 58 | + EXPECT_EQ(count, expectedNumCallbackCalls); |
| 59 | +} |
| 60 | + |
| 61 | +// MARK: PrintDiagnosticNamesMode |
| 62 | + |
| 63 | +TEST(DiagnosticInfo, PrintDiagnosticNamesMode_None) { |
| 64 | + // Test that we don't embed anything in the format string if told so. |
| 65 | + testCase( |
| 66 | + [](DiagnosticEngine &diags) { |
| 67 | + diags.setPrintDiagnosticNamesMode(PrintDiagnosticNamesMode::None); |
| 68 | + |
| 69 | + TestDiagnostic diagnostic(diag::error_immediate_mode_missing_stdlib.ID, |
| 70 | + DiagGroupID::DeprecatedDeclaration); |
| 71 | + diags.diagnose(SourceLoc(), diagnostic); |
| 72 | + }, |
| 73 | + [](DiagnosticEngine &diags, const DiagnosticInfo &info) { |
| 74 | + EXPECT_EQ(info.FormatString, |
| 75 | + diags.getFormatStringForDiagnostic( |
| 76 | + diag::error_immediate_mode_missing_stdlib.ID)); |
| 77 | + }, |
| 78 | + /*expectedNumCallbackCalls=*/1); |
| 79 | +} |
| 80 | + |
| 81 | +TEST(DiagnosticInfo, PrintDiagnosticNamesMode_Identifier) { |
| 82 | + // Test that we embed correct identifier in the format string. |
| 83 | + testCase( |
| 84 | + [](DiagnosticEngine &diags) { |
| 85 | + diags.setPrintDiagnosticNamesMode(PrintDiagnosticNamesMode::Identifier); |
| 86 | + |
| 87 | + diags.diagnose(SourceLoc(), diag::error_immediate_mode_missing_stdlib); |
| 88 | + }, |
| 89 | + [](DiagnosticEngine &diags, const DiagnosticInfo &info) { |
| 90 | + EXPECT_TRUE(info.FormatString.ends_with( |
| 91 | + " [error_immediate_mode_missing_stdlib]")); |
| 92 | + }, |
| 93 | + /*expectedNumCallbackCalls=*/1); |
| 94 | +} |
| 95 | + |
| 96 | +TEST(DiagnosticInfo, PrintDiagnosticNamesMode_Identifier_WrappedDiag) { |
| 97 | + // For a wrapper diagnostic, test that we embed the identifier of the wrapped |
| 98 | + // diagnostic. |
| 99 | + testCase( |
| 100 | + [](DiagnosticEngine &diags) { |
| 101 | + diags.setPrintDiagnosticNamesMode(PrintDiagnosticNamesMode::Identifier); |
| 102 | + |
| 103 | + diags.diagnose(SourceLoc(), diag::error_immediate_mode_missing_stdlib) |
| 104 | + .limitBehaviorUntilSwiftVersion(DiagnosticBehavior::Warning, 99); |
| 105 | + }, |
| 106 | + [](DiagnosticEngine &diags, const DiagnosticInfo &info) { |
| 107 | + EXPECT_EQ(info.ID, diag::error_in_a_future_swift_lang_mode.ID); |
| 108 | + EXPECT_TRUE(info.FormatString.ends_with( |
| 109 | + " [error_immediate_mode_missing_stdlib]")); |
| 110 | + }, |
| 111 | + /*expectedNumCallbackCalls=*/1); |
| 112 | +} |
| 113 | + |
| 114 | +TEST(DiagnosticInfo, PrintDiagnosticNamesMode_Group_NoGroup) { |
| 115 | + // Test that we don't embed anything in the format string if the diagnostic |
| 116 | + // has no group. |
| 117 | + testCase( |
| 118 | + [](DiagnosticEngine &diags) { |
| 119 | + diags.setPrintDiagnosticNamesMode(PrintDiagnosticNamesMode::Group); |
| 120 | + |
| 121 | + TestDiagnostic diagnostic(diag::error_immediate_mode_missing_stdlib.ID, |
| 122 | + DiagGroupID::no_group); |
| 123 | + diags.diagnose(SourceLoc(), diagnostic); |
| 124 | + }, |
| 125 | + [](DiagnosticEngine &diags, const DiagnosticInfo &info) { |
| 126 | + EXPECT_EQ(info.FormatString, |
| 127 | + diags.getFormatStringForDiagnostic( |
| 128 | + diag::error_immediate_mode_missing_stdlib.ID)); |
| 129 | + }, |
| 130 | + /*expectedNumCallbackCalls=*/1); |
| 131 | +} |
| 132 | + |
| 133 | +TEST(DiagnosticInfo, PrintDiagnosticNamesMode_Group) { |
| 134 | + // Test that we embed the correct group in the format string. |
| 135 | + testCase( |
| 136 | + [](DiagnosticEngine &diags) { |
| 137 | + diags.setPrintDiagnosticNamesMode(PrintDiagnosticNamesMode::Group); |
| 138 | + |
| 139 | + TestDiagnostic diagnostic(diag::error_immediate_mode_missing_stdlib.ID, |
| 140 | + DiagGroupID::DeprecatedDeclaration); |
| 141 | + diags.diagnose(SourceLoc(), diagnostic); |
| 142 | + }, |
| 143 | + [](DiagnosticEngine &, const DiagnosticInfo &info) { |
| 144 | + EXPECT_TRUE(info.FormatString.ends_with(" [DeprecatedDeclaration]")); |
| 145 | + }, |
| 146 | + /*expectedNumCallbackCalls=*/1); |
| 147 | +} |
| 148 | + |
| 149 | +TEST(DiagnosticInfo, PrintDiagnosticNamesMode_Group_WrappedDiag) { |
| 150 | + // For a wrapper diagnostic, test that we embed the group name of the wrapped |
| 151 | + // diagnostic. |
| 152 | + testCase( |
| 153 | + [](DiagnosticEngine &diags) { |
| 154 | + diags.setPrintDiagnosticNamesMode(PrintDiagnosticNamesMode::Group); |
| 155 | + |
| 156 | + TestDiagnostic diagnostic(diag::error_immediate_mode_missing_stdlib.ID, |
| 157 | + DiagGroupID::DeprecatedDeclaration); |
| 158 | + diags.diagnose(SourceLoc(), diagnostic) |
| 159 | + .limitBehaviorUntilSwiftVersion(DiagnosticBehavior::Warning, 99); |
| 160 | + }, |
| 161 | + [](DiagnosticEngine &, const DiagnosticInfo &info) { |
| 162 | + EXPECT_EQ(info.ID, diag::error_in_a_future_swift_lang_mode.ID); |
| 163 | + EXPECT_TRUE(info.FormatString.ends_with(" [DeprecatedDeclaration]")); |
| 164 | + }, |
| 165 | + /*expectedNumCallbackCalls=*/1); |
| 166 | +} |
| 167 | + |
| 168 | +} // end anonymous namespace |
0 commit comments