Skip to content

Commit a95785c

Browse files
Merge pull request #80260 from AnthonyLatsis/eutrema-japonicum
DiagnosticEngine: Print the ID of the wrapped, not wrapper, diagnostic
2 parents 97de71b + 9bfc163 commit a95785c

File tree

8 files changed

+237
-102
lines changed

8 files changed

+237
-102
lines changed

include/swift/AST/DiagnosticEngine.h

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1530,16 +1530,17 @@ namespace swift {
15301530
public:
15311531
DiagnosticKind declaredDiagnosticKindFor(const DiagID id);
15321532

1533-
/// Get a localized format string for a given `DiagID`. If no localization
1534-
/// available returns the default string for that `DiagID`.
1535-
llvm::StringRef diagnosticStringFor(DiagID id);
1536-
1537-
/// Get a localized format string with an optional diagnostic name appended
1538-
/// to it. The diagnostic name type is defined by
1539-
/// `PrintDiagnosticNamesMode`.
1540-
llvm::StringRef diagnosticStringWithNameFor(
1541-
DiagID id, DiagGroupID groupID,
1542-
PrintDiagnosticNamesMode printDiagnosticNamesMode);
1533+
/// Get a localized format string for the given `DiagID`. If no localization
1534+
/// is available, returns the default string.
1535+
llvm::StringRef getFormatStringForDiagnostic(DiagID id);
1536+
1537+
/// Get a localized format string for the given diagnostic. If no
1538+
/// localization is available, returns the default string.
1539+
///
1540+
/// \param includeDiagnosticName Whether to at all consider embedding the
1541+
/// name of the diagnostic identifier or group, per the setting.
1542+
llvm::StringRef getFormatStringForDiagnostic(const Diagnostic &diagnostic,
1543+
bool includeDiagnosticName);
15431544

15441545
static llvm::StringRef diagnosticIDStringFor(const DiagID id);
15451546

lib/AST/DiagnosticEngine.cpp

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,17 +1429,11 @@ DiagnosticEngine::diagnosticInfoForDiagnostic(const Diagnostic &diagnostic,
14291429
}
14301430
}
14311431

1432-
llvm::StringRef format;
1433-
if (includeDiagnosticName) {
1434-
format =
1435-
diagnosticStringWithNameFor(diagnostic.getID(), groupID,
1436-
getPrintDiagnosticNamesMode());
1437-
} else {
1438-
format = diagnosticStringFor(diagnostic.getID());
1439-
}
1432+
auto formatString =
1433+
getFormatStringForDiagnostic(diagnostic, includeDiagnosticName);
14401434

14411435
return DiagnosticInfo(diagnostic.getID(), loc, toDiagnosticKind(behavior),
1442-
format, diagnostic.getArgs(), Category,
1436+
formatString, diagnostic.getArgs(), Category,
14431437
getDefaultDiagnosticLoc(),
14441438
/*child note info*/ {}, diagnostic.getRanges(), fixIts,
14451439
diagnostic.isChildNote());
@@ -1622,18 +1616,24 @@ DiagnosticKind DiagnosticEngine::declaredDiagnosticKindFor(const DiagID id) {
16221616
return storedDiagnosticInfos[(unsigned)id].kind;
16231617
}
16241618

1625-
llvm::StringRef DiagnosticEngine::diagnosticStringFor(DiagID id) {
1619+
llvm::StringRef DiagnosticEngine::getFormatStringForDiagnostic(DiagID id) {
16261620
llvm::StringRef message = diagnosticStrings[(unsigned)id];
16271621
if (auto localizationProducer = localization.get()) {
16281622
message = localizationProducer->getMessageOr(id, message);
16291623
}
16301624
return message;
16311625
}
16321626

1633-
llvm::StringRef DiagnosticEngine::diagnosticStringWithNameFor(
1634-
DiagID id, DiagGroupID groupID,
1635-
PrintDiagnosticNamesMode printDiagnosticNamesMode) {
1636-
auto message = diagnosticStringFor(id);
1627+
llvm::StringRef
1628+
DiagnosticEngine::getFormatStringForDiagnostic(const Diagnostic &diagnostic,
1629+
bool includeDiagnosticName) {
1630+
auto diagID = diagnostic.getID();
1631+
auto message = getFormatStringForDiagnostic(diagID);
1632+
1633+
if (!includeDiagnosticName) {
1634+
return message;
1635+
}
1636+
16371637
auto formatMessageWithName = [&](StringRef message, StringRef name) {
16381638
const int additionalCharsLength = 3; // ' ', '[', ']'
16391639
std::string messageWithName;
@@ -1648,16 +1648,28 @@ llvm::StringRef DiagnosticEngine::diagnosticStringWithNameFor(
16481648
switch (printDiagnosticNamesMode) {
16491649
case PrintDiagnosticNamesMode::None:
16501650
break;
1651-
case PrintDiagnosticNamesMode::Identifier:
1652-
message = formatMessageWithName(message, diagnosticIDStringFor(id));
1651+
case PrintDiagnosticNamesMode::Identifier: {
1652+
// If this diagnostic is a wrapper for another diagnostic, use the ID of
1653+
// the wrapped diagnostic.
1654+
for (const auto &arg : diagnostic.getArgs()) {
1655+
if (arg.getKind() == DiagnosticArgumentKind::Diagnostic) {
1656+
diagID = arg.getAsDiagnostic()->ID;
1657+
break;
1658+
}
1659+
}
1660+
1661+
message = formatMessageWithName(message, diagnosticIDStringFor(diagID));
16531662
break;
1663+
}
16541664
case PrintDiagnosticNamesMode::Group:
1665+
auto groupID = diagnostic.getGroupID();
16551666
if (groupID != DiagGroupID::no_group) {
16561667
message =
16571668
formatMessageWithName(message, getDiagGroupInfoByID(groupID).name);
16581669
}
16591670
break;
16601671
}
1672+
16611673
return message;
16621674
}
16631675

lib/IDE/CodeCompletionDiagnostics.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ bool CodeCompletionDiagnostics::getDiagnostics(
7474
typename swift::detail::PassArgument<ArgTypes>::type... VArgs) {
7575
DiagID id = ID.ID;
7676
std::vector<DiagnosticArgument> DiagArgs{std::move(VArgs)...};
77-
auto format = Engine.diagnosticStringFor(id);
77+
auto format = Engine.getFormatStringForDiagnostic(id);
7878
DiagnosticEngine::formatDiagnosticText(Out, format, DiagArgs);
7979
severity = getSeverity(Engine.declaredDiagnosticKindFor(id));
8080

lib/PrintAsClang/ModuleContentsWriter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1086,7 +1086,7 @@ class ModuleWriter {
10861086
// Emit a specific unavailable message when we know why a decl can't be
10871087
// exposed, or a generic message otherwise.
10881088
auto diagString =
1089-
M.getASTContext().Diags.diagnosticStringFor(diag.getID());
1089+
M.getASTContext().Diags.getFormatStringForDiagnostic(diag.getID());
10901090
DiagnosticEngine::formatDiagnosticText(os, diagString, diag.getArgs(),
10911091
DiagnosticFormatOptions());
10921092
os << "\");\n";

unittests/AST/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ add_swift_unittest(SwiftASTTests
88
DiagnosticBehaviorTests.cpp
99
DiagnosticConsumerTests.cpp
1010
DiagnosticGroupsTests.cpp
11+
DiagnosticInfoTests.cpp
1112
SourceLocTests.cpp
1213
TestContext.cpp
1314
TypeMatchTests.cpp

unittests/AST/DiagnosticBehaviorTests.cpp

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,12 @@ class TestDiagnosticConsumer : public DiagnosticConsumer {
2929
this->callback(Info);
3030
}
3131
};
32-
} // end anonymous namespace
3332

34-
static void diagnosticBehaviorTestCase(
35-
llvm::function_ref<void(DiagnosticEngine &)> diagnose,
36-
llvm::function_ref<void(DiagnosticEngine &, const DiagnosticInfo &)>
37-
callback,
38-
unsigned expectedNumCallbackCalls) {
33+
static void
34+
testCase(llvm::function_ref<void(DiagnosticEngine &)> diagnose,
35+
llvm::function_ref<void(DiagnosticEngine &, const DiagnosticInfo &)>
36+
callback,
37+
unsigned expectedNumCallbackCalls) {
3938
SourceManager sourceMgr;
4039
DiagnosticEngine diags(sourceMgr);
4140

@@ -55,7 +54,7 @@ static void diagnosticBehaviorTestCase(
5554
}
5655

5756
TEST(DiagnosticBehavior, WarnUntilSwiftLangMode) {
58-
diagnosticBehaviorTestCase(
57+
testCase(
5958
[](DiagnosticEngine &diags) {
6059
diags.setLanguageVersion(version::Version({5}));
6160
diags.diagnose(SourceLoc(), diag::error_immediate_mode_missing_stdlib)
@@ -64,30 +63,30 @@ TEST(DiagnosticBehavior, WarnUntilSwiftLangMode) {
6463
[](DiagnosticEngine &diags, const DiagnosticInfo &info) {
6564
EXPECT_EQ(info.Kind, DiagnosticKind::Error);
6665
EXPECT_EQ(info.FormatString,
67-
diags.diagnosticStringFor(
66+
diags.getFormatStringForDiagnostic(
6867
diag::error_immediate_mode_missing_stdlib.ID));
6968
},
7069
/*expectedNumCallbackCalls=*/1);
7170

72-
diagnosticBehaviorTestCase(
71+
testCase(
7372
[](DiagnosticEngine &diags) {
7473
diags.setLanguageVersion(version::Version({4}));
7574
diags.diagnose(SourceLoc(), diag::error_immediate_mode_missing_stdlib)
7675
.warnUntilSwiftVersion(5);
7776
},
7877
[](DiagnosticEngine &diags, const DiagnosticInfo &info) {
7978
EXPECT_EQ(info.Kind, DiagnosticKind::Warning);
80-
EXPECT_EQ(info.FormatString,
81-
diags.diagnosticStringFor(diag::error_in_swift_lang_mode.ID));
79+
EXPECT_EQ(info.FormatString, diags.getFormatStringForDiagnostic(
80+
diag::error_in_swift_lang_mode.ID));
8281

8382
auto wrappedDiagInfo = info.FormatArgs.front().getAsDiagnostic();
8483
EXPECT_EQ(wrappedDiagInfo->FormatString,
85-
diags.diagnosticStringFor(
84+
diags.getFormatStringForDiagnostic(
8685
diag::error_immediate_mode_missing_stdlib.ID));
8786
},
8887
/*expectedNumCallbackCalls=*/1);
8988

90-
diagnosticBehaviorTestCase(
89+
testCase(
9190
[](DiagnosticEngine &diags) {
9291
diags.setLanguageVersion(version::Version({4}));
9392
diags.diagnose(SourceLoc(), diag::error_immediate_mode_missing_stdlib)
@@ -96,13 +95,15 @@ TEST(DiagnosticBehavior, WarnUntilSwiftLangMode) {
9695
[](DiagnosticEngine &diags, const DiagnosticInfo &info) {
9796
EXPECT_EQ(info.Kind, DiagnosticKind::Warning);
9897
EXPECT_EQ(info.FormatString,
99-
diags.diagnosticStringFor(
98+
diags.getFormatStringForDiagnostic(
10099
diag::error_in_a_future_swift_lang_mode.ID));
101100

102101
auto wrappedDiagInfo = info.FormatArgs.front().getAsDiagnostic();
103102
EXPECT_EQ(wrappedDiagInfo->FormatString,
104-
diags.diagnosticStringFor(
103+
diags.getFormatStringForDiagnostic(
105104
diag::error_immediate_mode_missing_stdlib.ID));
106105
},
107106
/*expectedNumCallbackCalls=*/1);
108107
}
108+
109+
} // end anonymous namespace

unittests/AST/DiagnosticGroupsTests.cpp

Lines changed: 11 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,10 @@ class TestDiagnosticConsumer : public DiagnosticConsumer {
3939
struct TestDiagnostic : public Diagnostic {
4040
TestDiagnostic(DiagID ID, DiagGroupID GroupID) : Diagnostic(ID, GroupID) {}
4141
};
42-
} // end anonymous namespace
4342

44-
static void diagnosticGroupsTestCase(
45-
llvm::function_ref<void(DiagnosticEngine &)> diagnose,
46-
llvm::function_ref<void(const DiagnosticInfo &)> callback,
47-
unsigned expectedNumCallbackCalls) {
43+
static void testCase(llvm::function_ref<void(DiagnosticEngine &)> diagnose,
44+
llvm::function_ref<void(const DiagnosticInfo &)> callback,
45+
unsigned expectedNumCallbackCalls) {
4846
SourceManager sourceMgr;
4947
DiagnosticEngine diags(sourceMgr);
5048

@@ -63,58 +61,10 @@ static void diagnosticGroupsTestCase(
6361
EXPECT_EQ(count, expectedNumCallbackCalls);
6462
}
6563

66-
TEST(DiagnosticGroups, PrintDiagnosticGroups) {
67-
// Test that we append the correct group in the format string.
68-
diagnosticGroupsTestCase(
69-
[](DiagnosticEngine &diags) {
70-
diags.setPrintDiagnosticNamesMode(PrintDiagnosticNamesMode::Group);
71-
72-
TestDiagnostic diagnostic(diag::error_immediate_mode_missing_stdlib.ID,
73-
DiagGroupID::DeprecatedDeclaration);
74-
diags.diagnose(SourceLoc(), diagnostic);
75-
},
76-
[](const DiagnosticInfo &info) {
77-
EXPECT_TRUE(info.FormatString.ends_with(" [DeprecatedDeclaration]"));
78-
},
79-
/*expectedNumCallbackCalls=*/1);
80-
81-
diagnosticGroupsTestCase(
82-
[](DiagnosticEngine &diags) {
83-
diags.setPrintDiagnosticNamesMode(PrintDiagnosticNamesMode::Group);
84-
85-
TestDiagnostic diagnostic(diag::error_immediate_mode_missing_stdlib.ID,
86-
DiagGroupID::no_group);
87-
diags.diagnose(SourceLoc(), diagnostic);
88-
},
89-
[](const DiagnosticInfo &info) {
90-
EXPECT_FALSE(info.FormatString.ends_with("]"));
91-
},
92-
/*expectedNumCallbackCalls=*/1);
93-
}
94-
95-
TEST(DiagnosticGroups, DiagnosticsWrappersInheritGroups) {
96-
// Test that we don't loose the group of a diagnostic when it gets wrapped in
97-
// another one.
98-
diagnosticGroupsTestCase(
99-
[](DiagnosticEngine &diags) {
100-
diags.setPrintDiagnosticNamesMode(PrintDiagnosticNamesMode::Group);
101-
102-
TestDiagnostic diagnostic(diag::error_immediate_mode_missing_stdlib.ID,
103-
DiagGroupID::DeprecatedDeclaration);
104-
diags.diagnose(SourceLoc(), diagnostic)
105-
.limitBehaviorUntilSwiftVersion(DiagnosticBehavior::Warning, 99);
106-
},
107-
[](const DiagnosticInfo &info) {
108-
EXPECT_EQ(info.ID, diag::error_in_a_future_swift_lang_mode.ID);
109-
EXPECT_TRUE(info.FormatString.ends_with(" [DeprecatedDeclaration]"));
110-
},
111-
/*expectedNumCallbackCalls=*/1);
112-
}
113-
11464
TEST(DiagnosticGroups, TargetAll) {
11565
// Test that uncategorized diagnostics are escalated when escalating all
11666
// warnings.
117-
diagnosticGroupsTestCase(
67+
testCase(
11868
[](DiagnosticEngine &diags) {
11969
const std::vector rules = {
12070
WarningAsErrorRule(WarningAsErrorRule::Action::Enable)};
@@ -139,16 +89,16 @@ TEST(DiagnosticGroups, OverrideBehaviorLimitations) {
13989
DiagGroupID::DeprecatedDeclaration);
14090

14191
// Make sure ID actually is an error by default.
142-
diagnosticGroupsTestCase(
92+
testCase(
14393
[&diagnostic](DiagnosticEngine &diags) {
14494
diags.diagnose(SourceLoc(), diagnostic);
14595
},
14696
[](const DiagnosticInfo &info) {
147-
EXPECT_TRUE(info.Kind == DiagnosticKind::Error);
97+
EXPECT_EQ(info.Kind, DiagnosticKind::Error);
14898
},
14999
/*expectedNumCallbackCalls=*/1);
150100

151-
diagnosticGroupsTestCase(
101+
testCase(
152102
[&diagnostic](DiagnosticEngine &diags) {
153103
const std::vector rules = {WarningAsErrorRule(
154104
WarningAsErrorRule::Action::Enable, "DeprecatedDeclaration")};
@@ -172,7 +122,7 @@ TEST(DiagnosticGroups, OverrideBehaviorLimitations) {
172122
DiagGroupID::DeprecatedDeclaration);
173123

174124
// Make sure ID actually is a warning by default.
175-
diagnosticGroupsTestCase(
125+
testCase(
176126
[&diagnostic](DiagnosticEngine &diags) {
177127
diags.diagnose(SourceLoc(), diagnostic);
178128
},
@@ -181,7 +131,7 @@ TEST(DiagnosticGroups, OverrideBehaviorLimitations) {
181131
},
182132
/*expectedNumCallbackCalls=*/1);
183133

184-
diagnosticGroupsTestCase(
134+
testCase(
185135
[&diagnostic](DiagnosticEngine &diags) {
186136
const std::vector rules = {WarningAsErrorRule(
187137
WarningAsErrorRule::Action::Enable, "DeprecatedDeclaration")};
@@ -196,3 +146,5 @@ TEST(DiagnosticGroups, OverrideBehaviorLimitations) {
196146
/*expectedNumCallbackCalls=*/1);
197147
}
198148
}
149+
150+
} // end anonymous namespace

0 commit comments

Comments
 (0)