|
| 1 | +//===--- DiagnosticBehaviorTests.cpp --------------------------------------===// |
| 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/DiagnosticsFrontend.h" |
| 15 | +#include "swift/Basic/SourceManager.h" |
| 16 | +#include "gtest/gtest.h" |
| 17 | + |
| 18 | +using namespace swift; |
| 19 | + |
| 20 | +namespace { |
| 21 | +class TestDiagnosticConsumer : public DiagnosticConsumer { |
| 22 | + llvm::function_ref<void(const DiagnosticInfo &)> callback; |
| 23 | + |
| 24 | +public: |
| 25 | + TestDiagnosticConsumer(decltype(callback) callback) : callback(callback) {} |
| 26 | + |
| 27 | + void handleDiagnostic(SourceManager &SM, |
| 28 | + const DiagnosticInfo &Info) override { |
| 29 | + this->callback(Info); |
| 30 | + } |
| 31 | +}; |
| 32 | +} // end anonymous namespace |
| 33 | + |
| 34 | +static void diagnosticBehaviorTestCase( |
| 35 | + llvm::function_ref<void(DiagnosticEngine &)> diagnose, |
| 36 | + llvm::function_ref<void(DiagnosticEngine &, const DiagnosticInfo &)> |
| 37 | + callback, |
| 38 | + unsigned expectedNumCallbackCalls) { |
| 39 | + SourceManager sourceMgr; |
| 40 | + DiagnosticEngine diags(sourceMgr); |
| 41 | + |
| 42 | + unsigned count = 0; |
| 43 | + |
| 44 | + const auto countingCallback = [&](const DiagnosticInfo &info) { |
| 45 | + ++count; |
| 46 | + callback(diags, info); |
| 47 | + }; |
| 48 | + |
| 49 | + TestDiagnosticConsumer consumer(countingCallback); |
| 50 | + diags.addConsumer(consumer); |
| 51 | + diagnose(diags); |
| 52 | + diags.removeConsumer(consumer); |
| 53 | + |
| 54 | + EXPECT_EQ(count, expectedNumCallbackCalls); |
| 55 | +} |
| 56 | + |
| 57 | +TEST(DiagnosticBehavior, WarnUntilSwiftLangMode) { |
| 58 | + diagnosticBehaviorTestCase( |
| 59 | + [](DiagnosticEngine &diags) { |
| 60 | + diags.setLanguageVersion(version::Version({5})); |
| 61 | + diags.diagnose(SourceLoc(), diag::error_immediate_mode_missing_stdlib) |
| 62 | + .warnUntilSwiftVersion(4); |
| 63 | + }, |
| 64 | + [](DiagnosticEngine &diags, const DiagnosticInfo &info) { |
| 65 | + EXPECT_EQ(info.Kind, DiagnosticKind::Error); |
| 66 | + EXPECT_EQ(info.FormatString, |
| 67 | + diags.diagnosticStringFor( |
| 68 | + diag::error_immediate_mode_missing_stdlib.ID)); |
| 69 | + }, |
| 70 | + /*expectedNumCallbackCalls=*/1); |
| 71 | + |
| 72 | + diagnosticBehaviorTestCase( |
| 73 | + [](DiagnosticEngine &diags) { |
| 74 | + diags.setLanguageVersion(version::Version({4})); |
| 75 | + diags.diagnose(SourceLoc(), diag::error_immediate_mode_missing_stdlib) |
| 76 | + .warnUntilSwiftVersion(5); |
| 77 | + }, |
| 78 | + [](DiagnosticEngine &diags, const DiagnosticInfo &info) { |
| 79 | + EXPECT_EQ(info.Kind, DiagnosticKind::Warning); |
| 80 | + EXPECT_EQ(info.FormatString, |
| 81 | + diags.diagnosticStringFor(diag::error_in_swift_lang_mode.ID)); |
| 82 | + |
| 83 | + auto wrappedDiagInfo = info.FormatArgs.front().getAsDiagnostic(); |
| 84 | + EXPECT_EQ(wrappedDiagInfo->FormatString, |
| 85 | + diags.diagnosticStringFor( |
| 86 | + diag::error_immediate_mode_missing_stdlib.ID)); |
| 87 | + }, |
| 88 | + /*expectedNumCallbackCalls=*/1); |
| 89 | + |
| 90 | + diagnosticBehaviorTestCase( |
| 91 | + [](DiagnosticEngine &diags) { |
| 92 | + diags.setLanguageVersion(version::Version({4})); |
| 93 | + diags.diagnose(SourceLoc(), diag::error_immediate_mode_missing_stdlib) |
| 94 | + .warnUntilSwiftVersion(99); |
| 95 | + }, |
| 96 | + [](DiagnosticEngine &diags, const DiagnosticInfo &info) { |
| 97 | + EXPECT_EQ(info.Kind, DiagnosticKind::Warning); |
| 98 | + EXPECT_EQ(info.FormatString, |
| 99 | + diags.diagnosticStringFor( |
| 100 | + diag::error_in_a_future_swift_lang_mode.ID)); |
| 101 | + |
| 102 | + auto wrappedDiagInfo = info.FormatArgs.front().getAsDiagnostic(); |
| 103 | + EXPECT_EQ(wrappedDiagInfo->FormatString, |
| 104 | + diags.diagnosticStringFor( |
| 105 | + diag::error_immediate_mode_missing_stdlib.ID)); |
| 106 | + }, |
| 107 | + /*expectedNumCallbackCalls=*/1); |
| 108 | +} |
0 commit comments