Skip to content

Commit 58e63f7

Browse files
authored
Merge pull request #28313 from owenv/verifier-consumer
[DiagnosticVerifier] Make DiagnosticVerifier a DiagnosticConsumer
2 parents ad09f43 + 465bab0 commit 58e63f7

File tree

10 files changed

+480
-331
lines changed

10 files changed

+480
-331
lines changed

include/swift/Frontend/DiagnosticVerifier.h

Lines changed: 79 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -18,30 +18,87 @@
1818
#ifndef SWIFT_FRONTEND_DIAGNOSTIC_VERIFIER_H
1919
#define SWIFT_FRONTEND_DIAGNOSTIC_VERIFIER_H
2020

21+
#include "swift/AST/DiagnosticConsumer.h"
2122
#include "swift/Basic/LLVM.h"
2223

2324
namespace swift {
24-
class DependencyTracker;
25-
class FileUnit;
26-
class SourceManager;
27-
class SourceFile;
28-
29-
/// Set up the specified source manager so that diagnostics are captured
30-
/// instead of being printed.
31-
void enableDiagnosticVerifier(SourceManager &SM);
32-
33-
/// Verify that captured diagnostics meet with the expectations of the source
34-
/// files corresponding to the specified \p BufferIDs and tear down our
35-
/// support for capturing and verifying diagnostics.
36-
///
37-
/// This returns true if there are any mismatches found.
38-
bool verifyDiagnostics(SourceManager &SM, ArrayRef<unsigned> BufferIDs,
39-
bool autoApplyFixes, bool ignoreUnknown);
40-
41-
bool verifyDependencies(SourceManager &SM, const DependencyTracker &DT,
42-
ArrayRef<FileUnit *> SFs);
43-
bool verifyDependencies(SourceManager &SM, const DependencyTracker &DT,
44-
ArrayRef<SourceFile *> SFs);
25+
class DependencyTracker;
26+
class FileUnit;
27+
class SourceManager;
28+
class SourceFile;
29+
30+
// MARK: - DependencyVerifier
31+
bool verifyDependencies(SourceManager &SM, const DependencyTracker &DT,
32+
ArrayRef<FileUnit *> SFs);
33+
bool verifyDependencies(SourceManager &SM, const DependencyTracker &DT,
34+
ArrayRef<SourceFile *> SFs);
35+
36+
// MARK: - DiagnosticVerifier
37+
struct ExpectedFixIt;
38+
39+
struct CapturedDiagnosticInfo {
40+
llvm::SmallString<128> Message;
41+
llvm::SmallString<32> FileName;
42+
DiagnosticKind Classification;
43+
SourceLoc Loc;
44+
unsigned Line;
45+
unsigned Column;
46+
SmallVector<DiagnosticInfo::FixIt, 2> FixIts;
47+
48+
CapturedDiagnosticInfo(llvm::SmallString<128> Message,
49+
llvm::SmallString<32> FileName,
50+
DiagnosticKind Classification, SourceLoc Loc,
51+
unsigned Line, unsigned Column,
52+
SmallVector<DiagnosticInfo::FixIt, 2> FixIts)
53+
: Message(Message), FileName(FileName), Classification(Classification),
54+
Loc(Loc), Line(Line), Column(Column), FixIts(FixIts) {}
55+
};
56+
/// This class implements support for -verify mode in the compiler. It
57+
/// buffers up diagnostics produced during compilation, then checks them
58+
/// against expected-error markers in the source file.
59+
class DiagnosticVerifier : public DiagnosticConsumer {
60+
SourceManager &SM;
61+
std::vector<CapturedDiagnosticInfo> CapturedDiagnostics;
62+
ArrayRef<unsigned> BufferIDs;
63+
bool AutoApplyFixes;
64+
bool IgnoreUnknown;
65+
66+
public:
67+
explicit DiagnosticVerifier(SourceManager &SM, ArrayRef<unsigned> BufferIDs,
68+
bool AutoApplyFixes, bool IgnoreUnknown)
69+
: SM(SM), BufferIDs(BufferIDs), AutoApplyFixes(AutoApplyFixes),
70+
IgnoreUnknown(IgnoreUnknown) {}
71+
72+
virtual void handleDiagnostic(SourceManager &SM,
73+
const DiagnosticInfo &Info) override;
74+
75+
virtual bool finishProcessing() override;
76+
77+
private:
78+
/// Result of verifying a file.
79+
struct Result {
80+
/// Were there any errors? All of the following are considered errors:
81+
/// - Expected diagnostics that were not present
82+
/// - Unexpected diagnostics that were present
83+
/// - Errors in the definition of expected diagnostics
84+
bool HadError;
85+
bool HadUnexpectedDiag;
86+
};
87+
88+
/// verifyFile - After the file has been processed, check to see if we
89+
/// got all of the expected diagnostics and check to see if there were any
90+
/// unexpected ones.
91+
Result verifyFile(unsigned BufferID);
92+
93+
bool checkForFixIt(const ExpectedFixIt &Expected,
94+
const CapturedDiagnosticInfo &D, StringRef buffer);
95+
96+
// Render the verifier syntax for a given set of fix-its.
97+
std::string renderFixits(ArrayRef<DiagnosticInfo::FixIt> fixits,
98+
StringRef InputFile);
99+
100+
void printRemainingDiagnostics() const;
101+
};
45102
}
46103

47104
#endif

include/swift/Frontend/Frontend.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "swift/Basic/SourceManager.h"
3232
#include "swift/ClangImporter/ClangImporter.h"
3333
#include "swift/ClangImporter/ClangImporterOptions.h"
34+
#include "swift/Frontend/DiagnosticVerifier.h"
3435
#include "swift/Frontend/FrontendOptions.h"
3536
#include "swift/Frontend/ModuleInterfaceSupport.h"
3637
#include "swift/Migrator/MigratorOptions.h"
@@ -405,6 +406,7 @@ class CompilerInstance {
405406
std::unique_ptr<ASTContext> Context;
406407
std::unique_ptr<Lowering::TypeConverter> TheSILTypes;
407408
std::unique_ptr<SILModule> TheSILModule;
409+
std::unique_ptr<DiagnosticVerifier> DiagVerifier;
408410

409411
/// Null if no tracker.
410412
std::unique_ptr<DependencyTracker> DepTracker;
@@ -585,6 +587,7 @@ class CompilerInstance {
585587
bool setUpInputs();
586588
bool setUpASTContextIfNeeded();
587589
void setupStatsReporter();
590+
void setupDiagnosticVerifierIfNeeded();
588591
Optional<unsigned> setUpCodeCompletionBuffer();
589592

590593
/// Set up all state in the CompilerInstance to process the given input file.

include/swift/Frontend/PrintingDiagnosticConsumer.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class PrintingDiagnosticConsumer : public DiagnosticConsumer {
4141
// Educational notes which are buffered until the consumer is finished
4242
// constructing a snippet.
4343
SmallVector<std::string, 1> BufferedEducationalNotes;
44+
bool SuppressOutput = false;
4445

4546
public:
4647
PrintingDiagnosticConsumer(llvm::raw_ostream &stream = llvm::errs());
@@ -70,6 +71,10 @@ class PrintingDiagnosticConsumer : public DiagnosticConsumer {
7071
return DidErrorOccur;
7172
}
7273

74+
void setSuppressOutput(bool suppressOutput) {
75+
SuppressOutput = suppressOutput;
76+
}
77+
7378
private:
7479
void printDiagnostic(SourceManager &SM, const DiagnosticInfo &Info);
7580
};

0 commit comments

Comments
 (0)