@@ -92,14 +92,26 @@ namespace {
92
92
CapturedDiagnostics.push_back (Diag);
93
93
}
94
94
95
+ // / Result of verifying a file.
96
+ struct Result {
97
+ // / Were there any errors? All of the following are considered errors:
98
+ // / - Expected diagnostics that were not present
99
+ // / - Unexpected diagnostics that were present
100
+ // / - Errors in the definition of expected diagnostics
101
+ bool HadError;
102
+ bool HadUnexpectedDiag;
103
+ };
104
+
95
105
// / verifyFile - After the file has been processed, check to see if we
96
106
// / got all of the expected diagnostics and check to see if there were any
97
107
// / unexpected ones.
98
- bool verifyFile (unsigned BufferID, bool autoApplyFixes);
108
+ Result verifyFile (unsigned BufferID, bool autoApplyFixes);
99
109
100
110
// / diagnostics for '<unknown>:0' should be considered as unexpected.
101
111
bool verifyUnknown ();
102
112
113
+ void printRemainingDiagnostics () const ;
114
+
103
115
// / If there are any -verify errors (e.g. differences between expectations
104
116
// / and actual diagnostics produced), apply fixits to the original source
105
117
// / file and drop it back in place.
@@ -210,8 +222,8 @@ static std::string renderFixits(ArrayRef<llvm::SMFixIt> fixits,
210
222
// / After the file has been processed, check to see if we got all of
211
223
// / the expected diagnostics and check to see if there were any unexpected
212
224
// / ones.
213
- bool DiagnosticVerifier::verifyFile ( unsigned BufferID,
214
- bool shouldAutoApplyFixes) {
225
+ DiagnosticVerifier::Result
226
+ DiagnosticVerifier::verifyFile ( unsigned BufferID, bool shouldAutoApplyFixes) {
215
227
using llvm::SMLoc;
216
228
217
229
const SourceLoc BufferStartLoc = SM.getLocForBufferStart (BufferID);
@@ -632,15 +644,20 @@ bool DiagnosticVerifier::verifyFile(unsigned BufferID,
632
644
}
633
645
634
646
// Verify that there are no diagnostics (in MemoryBuffer) left in the list.
635
- for (unsigned i = 0 , e = CapturedDiagnostics.size (); i != e; ++i) {
636
- if (CapturedDiagnostics[i].getFilename () != BufferName)
647
+ bool HadUnexpectedDiag = false ;
648
+ for (unsigned i = CapturedDiagnostics.size (); i != 0 ; ) {
649
+ --i;
650
+ if (CapturedDiagnostics[i].getFilename () != BufferName) {
637
651
continue ;
652
+ }
638
653
654
+ HadUnexpectedDiag = true ;
639
655
std::string Message =
640
656
" unexpected " +getDiagKindString (CapturedDiagnostics[i].getKind ())+
641
657
" produced: " +CapturedDiagnostics[i].getMessage ().str ();
642
658
addError (CapturedDiagnostics[i].getLoc ().getPointer (),
643
659
Message);
660
+ CapturedDiagnostics.erase (CapturedDiagnostics.begin () + i);
644
661
}
645
662
646
663
// Sort the diagnostics by their address in the memory buffer as the primary
@@ -659,8 +676,8 @@ bool DiagnosticVerifier::verifyFile(unsigned BufferID,
659
676
// If auto-apply fixits is on, rewrite the original source file.
660
677
if (shouldAutoApplyFixes)
661
678
autoApplyFixes (BufferID, Errors);
662
-
663
- return !Errors.empty ();
679
+
680
+ return Result{ !Errors.empty (), HadUnexpectedDiag} ;
664
681
}
665
682
666
683
bool DiagnosticVerifier::verifyUnknown () {
@@ -681,6 +698,15 @@ bool DiagnosticVerifier::verifyUnknown() {
681
698
return HadError;
682
699
}
683
700
701
+ void DiagnosticVerifier::printRemainingDiagnostics () const {
702
+ for (const auto &diag : CapturedDiagnostics) {
703
+ SM.getLLVMSourceMgr ().PrintMessage (
704
+ llvm::errs (), diag.getLoc (), diag.getKind (),
705
+ " diagnostic produced by Clang: " + diag.getMessage (),
706
+ /* Ranges=*/ {}, diag.getFixIts ());
707
+ }
708
+ }
709
+
684
710
// / If there are any -verify errors (e.g. differences between expectations
685
711
// / and actual diagnostics produced), apply fixits to the original source
686
712
// / file and drop it back in place.
@@ -776,15 +802,26 @@ bool swift::verifyDiagnostics(SourceManager &SM, ArrayRef<unsigned> BufferIDs,
776
802
auto *Verifier = (DiagnosticVerifier*)SM.getLLVMSourceMgr ().getDiagContext ();
777
803
SM.getLLVMSourceMgr ().setDiagHandler (nullptr , nullptr );
778
804
779
- bool HadError = false ;
805
+ DiagnosticVerifier::Result Result = { false , false } ;
780
806
781
- for (auto &BufferID : BufferIDs)
782
- HadError |= Verifier->verifyFile (BufferID, autoApplyFixes);
783
- if (!ignoreUnknown)
784
- HadError |= Verifier->verifyUnknown ();
807
+ for (auto &BufferID : BufferIDs) {
808
+ DiagnosticVerifier::Result FileResult =
809
+ Verifier->verifyFile (BufferID, autoApplyFixes);
810
+ Result.HadError |= FileResult.HadError ;
811
+ Result.HadUnexpectedDiag |= FileResult.HadUnexpectedDiag ;
812
+ }
813
+ if (!ignoreUnknown) {
814
+ bool HadError = Verifier->verifyUnknown ();
815
+ Result.HadError |= HadError;
816
+ // For <unknown>, all errors are unexpected.
817
+ Result.HadUnexpectedDiag |= HadError;
818
+ }
819
+
820
+ if (Result.HadUnexpectedDiag )
821
+ Verifier->printRemainingDiagnostics ();
785
822
786
823
delete Verifier;
787
824
788
- return HadError;
825
+ return Result. HadError ;
789
826
}
790
827
0 commit comments