Skip to content

Commit d3214de

Browse files
authored
Merge pull request #84685 from hnrklssn/verify-all
This adds the -verify-ignore-unrelated flag. When -verify is used without -verify-ignore-unrelated, diagnostics emitted in buffers other than the main file and those passed with -verify-additional-file (except diagnostics emitted at <unknown>:0) will now result in an error. They were previously ignored. The old behaviour is still available as opt-in using -verify-ignore-unrelated, but by being strict by default it should make it harder to accidentally miss diagnostics. To avoid unnecessary performance overhead, -verify-additional-file is still required to parse the expected-* directives in files other than the main file.
2 parents fbad380 + ce8f967 commit d3214de

File tree

430 files changed

+679
-618
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

430 files changed

+679
-618
lines changed

include/swift/Basic/DiagnosticOptions.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ class DiagnosticOptions {
4141
/// \c VerifyMode is not \c NoVerify.
4242
bool VerifyIgnoreUnknown = false;
4343

44+
/// Indicates whether to allow diagnostics for locations outside files parsed
45+
/// for 'expected' diagnostics if \c VerifyMode is not \c NoVerify. Does not
46+
/// allow diagnostics at <unknown>, that is controlled by VerifyIgnoreUnknown.
47+
bool VerifyIgnoreUnrelated = false;
48+
4449
/// Indicates whether diagnostic passes should be skipped.
4550
bool SkipDiagnosticPasses = false;
4651

include/swift/Frontend/DiagnosticVerifier.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,18 +99,19 @@ class DiagnosticVerifier : public DiagnosticConsumer {
9999
ArrayRef<std::string> AdditionalFilePaths;
100100
bool AutoApplyFixes;
101101
bool IgnoreUnknown;
102+
bool IgnoreUnrelated;
102103
bool UseColor;
103104
ArrayRef<std::string> AdditionalExpectedPrefixes;
104105

105106
public:
106107
explicit DiagnosticVerifier(SourceManager &SM, ArrayRef<unsigned> BufferIDs,
107108
ArrayRef<std::string> AdditionalFilePaths,
108109
bool AutoApplyFixes, bool IgnoreUnknown,
109-
bool UseColor,
110+
bool IgnoreUnrelated, bool UseColor,
110111
ArrayRef<std::string> AdditionalExpectedPrefixes)
111112
: SM(SM), BufferIDs(BufferIDs), AdditionalFilePaths(AdditionalFilePaths),
112113
AutoApplyFixes(AutoApplyFixes), IgnoreUnknown(IgnoreUnknown),
113-
UseColor(UseColor),
114+
IgnoreUnrelated(IgnoreUnrelated), UseColor(UseColor),
114115
AdditionalExpectedPrefixes(AdditionalExpectedPrefixes) {}
115116

116117
virtual void handleDiagnostic(SourceManager &SM,
@@ -131,6 +132,11 @@ class DiagnosticVerifier : public DiagnosticConsumer {
131132

132133
void printDiagnostic(const llvm::SMDiagnostic &Diag) const;
133134

135+
/// Check whether there were any diagnostics in files without expected
136+
/// diagnostics
137+
bool verifyUnrelated(
138+
std::vector<CapturedDiagnosticInfo> &CapturedDiagnostics) const;
139+
134140
bool
135141
verifyUnknown(std::vector<CapturedDiagnosticInfo> &CapturedDiagnostics) const;
136142

include/swift/Option/FrontendOptions.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ def verify_apply_fixes : Flag<["-"], "verify-apply-fixes">,
165165
HelpText<"Like -verify, but updates the original source file">;
166166
def verify_ignore_unknown: Flag<["-"], "verify-ignore-unknown">,
167167
HelpText<"Allow diagnostics for '<unknown>' location in verify mode">;
168+
def verify_ignore_unrelated: Flag<["-"], "verify-ignore-unrelated">,
169+
HelpText<"Allow diagnostics in files outside those with expected diagnostics in verify mode">;
168170
def verify_generic_signatures : Separate<["-"], "verify-generic-signatures">,
169171
MetaVarName<"<module-name>">,
170172
HelpText<"Verify the generic signatures in the given module">;

lib/Frontend/CompilerInvocation.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2588,6 +2588,7 @@ static bool ParseDiagnosticArgs(DiagnosticOptions &Opts, ArgList &Args,
25882588
if (Args.hasArg(OPT_verify_apply_fixes))
25892589
Opts.VerifyMode = DiagnosticOptions::VerifyAndApplyFixes;
25902590
Opts.VerifyIgnoreUnknown |= Args.hasArg(OPT_verify_ignore_unknown);
2591+
Opts.VerifyIgnoreUnrelated |= Args.hasArg(OPT_verify_ignore_unrelated);
25912592
Opts.SkipDiagnosticPasses |= Args.hasArg(OPT_disable_diagnostic_passes);
25922593
Opts.ShowDiagnosticsAfterFatalError |=
25932594
Args.hasArg(OPT_show_diagnostics_after_fatal);

lib/Frontend/DiagnosticVerifier.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,48 @@ bool DiagnosticVerifier::verifyUnknown(
400400
auto diag = SM.GetMessage({}, llvm::SourceMgr::DK_Error, Message, {}, {});
401401
printDiagnostic(diag);
402402
}
403+
404+
if (HadError) {
405+
auto NoteMessage = "use '-verify-ignore-unknown' to "
406+
"ignore diagnostics at this location";
407+
auto noteDiag =
408+
SM.GetMessage({}, llvm::SourceMgr::DK_Note, NoteMessage, {}, {});
409+
printDiagnostic(noteDiag);
410+
}
411+
return HadError;
412+
}
413+
414+
bool DiagnosticVerifier::verifyUnrelated(
415+
std::vector<CapturedDiagnosticInfo> &CapturedDiagnostics) const {
416+
bool HadError = false;
417+
for (unsigned i = 0, e = CapturedDiagnostics.size(); i != e; ++i) {
418+
SourceLoc Loc = CapturedDiagnostics[i].Loc;
419+
if (!Loc.isValid())
420+
// checked by verifyUnknown
421+
continue;
422+
423+
HadError = true;
424+
std::string Message =
425+
("unexpected " +
426+
getDiagKindString(CapturedDiagnostics[i].Classification) +
427+
" produced: " + CapturedDiagnostics[i].Message)
428+
.str();
429+
430+
auto diag = SM.GetMessage(Loc, llvm::SourceMgr::DK_Error, Message, {}, {});
431+
printDiagnostic(diag);
432+
433+
auto FileName = SM.getIdentifierForBuffer(SM.findBufferContainingLoc(Loc));
434+
auto NoteMessage = ("file '" + FileName +
435+
"' is not parsed for 'expected' statements. Use "
436+
"'-verify-additional-file " +
437+
FileName +
438+
"' to enable, or '-verify-ignore-unrelated' to "
439+
"ignore diagnostics in this file");
440+
auto noteDiag =
441+
SM.GetMessage(Loc, llvm::SourceMgr::DK_Note, NoteMessage, {}, {});
442+
printDiagnostic(noteDiag);
443+
}
444+
403445
return HadError;
404446
}
405447

@@ -1519,6 +1561,11 @@ bool DiagnosticVerifier::finishProcessing() {
15191561
// For <unknown>, all errors are unexpected.
15201562
Result.HadUnexpectedDiag |= HadError;
15211563
}
1564+
if (!IgnoreUnrelated) {
1565+
bool HadError = verifyUnrelated(CapturedDiagnostics);
1566+
Result.HadError |= HadError;
1567+
Result.HadUnexpectedDiag |= HadError;
1568+
}
15221569

15231570
if (Result.HadUnexpectedDiag)
15241571
printRemainingDiagnostics();

lib/Frontend/Frontend.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -434,8 +434,8 @@ bool CompilerInstance::setupDiagnosticVerifierIfNeeded() {
434434
DiagVerifier = std::make_unique<DiagnosticVerifier>(
435435
SourceMgr, InputSourceCodeBufferIDs, diagOpts.AdditionalVerifierFiles,
436436
diagOpts.VerifyMode == DiagnosticOptions::VerifyAndApplyFixes,
437-
diagOpts.VerifyIgnoreUnknown, diagOpts.UseColor,
438-
diagOpts.AdditionalDiagnosticVerifierPrefixes);
437+
diagOpts.VerifyIgnoreUnknown, diagOpts.VerifyIgnoreUnrelated,
438+
diagOpts.UseColor, diagOpts.AdditionalDiagnosticVerifierPrefixes);
439439

440440
addDiagnosticConsumer(DiagVerifier.get());
441441
}

test/APINotes/basic.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-typecheck-verify-swift -I %S/Inputs/custom-modules -F %S/Inputs/custom-frameworks
1+
// RUN: %target-typecheck-verify-swift -verify-ignore-unrelated -I %S/Inputs/custom-modules -F %S/Inputs/custom-frameworks
22
import APINotesTest
33
import APINotesFrameworkTest
44

test/APINotes/broken-swift-name.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-typecheck-verify-swift -I %S/Inputs/broken-modules
1+
// RUN: %target-typecheck-verify-swift -verify-ignore-unrelated -I %S/Inputs/broken-modules
22
import BrokenAPINotes
33

44
func testBrokenSwiftName(x: inout ZXSpectrum) {

test/APINotes/obsoleted.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// RUN: not %target-swift-frontend -typecheck -verify -I %S/Inputs/custom-modules -F %S/Inputs/custom-frameworks -swift-version 4 %s
2-
// RUN: %target-swift-frontend -typecheck -verify -I %S/Inputs/custom-modules -F %S/Inputs/custom-frameworks -swift-version 4.2 %s
3-
// RUN: %target-swift-frontend -typecheck -verify -I %S/Inputs/custom-modules -F %S/Inputs/custom-frameworks -swift-version 5 %s
1+
// RUN: not %target-swift-frontend -typecheck -verify -verify-ignore-unrelated -I %S/Inputs/custom-modules -F %S/Inputs/custom-frameworks -swift-version 4 %s
2+
// RUN: %target-swift-frontend -typecheck -verify -verify-ignore-unrelated -I %S/Inputs/custom-modules -F %S/Inputs/custom-frameworks -swift-version 4.2 %s
3+
// RUN: %target-swift-frontend -typecheck -verify -verify-ignore-unrelated -I %S/Inputs/custom-modules -F %S/Inputs/custom-frameworks -swift-version 5 %s
44
// REQUIRES: objc_interop
55
import ObsoletedAPINotesTest
66

test/AssociatedTypeInference/missing_conformance.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-typecheck-verify-swift
1+
// RUN: %target-typecheck-verify-swift -verify-ignore-unrelated
22

33
// Test candidates for witnesses that are missing conformances
44
// in various ways.

0 commit comments

Comments
 (0)