|
2 | 2 | //
|
3 | 3 | // This source file is part of the Swift.org open source project
|
4 | 4 | //
|
5 |
| -// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors |
| 5 | +// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors |
6 | 6 | // Licensed under Apache License v2.0 with Runtime Library Exception
|
7 | 7 | //
|
8 | 8 | // See https://swift.org/LICENSE.txt for license information
|
|
18 | 18 | #ifndef SWIFT_FRONTEND_DIAGNOSTIC_VERIFIER_H
|
19 | 19 | #define SWIFT_FRONTEND_DIAGNOSTIC_VERIFIER_H
|
20 | 20 |
|
| 21 | +#include "swift/AST/DiagnosticConsumer.h" |
21 | 22 | #include "swift/Basic/LLVM.h"
|
22 | 23 |
|
23 | 24 | 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 | +}; |
45 | 102 | }
|
46 | 103 |
|
47 | 104 | #endif
|
0 commit comments