|
10 | 10 | // |
11 | 11 | //===----------------------------------------------------------------------===// |
12 | 12 |
|
13 | | -#include "SwiftASTManager.h" |
14 | | -#include "SwiftLangSupport.h" |
15 | 13 | #include "SourceKit/Support/FileSystemProvider.h" |
16 | 14 | #include "SourceKit/Support/ImmutableTextBuffer.h" |
17 | 15 | #include "SourceKit/Support/Logging.h" |
18 | 16 | #include "SourceKit/Support/UIdent.h" |
| 17 | +#include "SwiftASTManager.h" |
| 18 | +#include "SwiftEditorDiagConsumer.h" |
| 19 | +#include "SwiftLangSupport.h" |
19 | 20 |
|
20 | 21 | #include "swift/AST/ASTDemangler.h" |
21 | 22 | #include "swift/AST/ASTPrinter.h" |
@@ -1588,6 +1589,36 @@ static void resolveCursor( |
1588 | 1589 | CancellationToken, fileSystem); |
1589 | 1590 | } |
1590 | 1591 |
|
| 1592 | +static void computeDiagnostics( |
| 1593 | + SwiftLangSupport &Lang, StringRef InputFile, SwiftInvocationRef Invok, |
| 1594 | + llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FileSystem, |
| 1595 | + SourceKitCancellationToken CancellationToken, |
| 1596 | + std::function<void(const RequestResult<DiagnosticsResult> &)> Receiver) { |
| 1597 | + |
| 1598 | + class DiagnosticsConsumer : public SwiftASTConsumer { |
| 1599 | + std::function<void(const RequestResult<DiagnosticsResult> &)> Receiver; |
| 1600 | + |
| 1601 | + public: |
| 1602 | + DiagnosticsConsumer( |
| 1603 | + std::function<void(const RequestResult<DiagnosticsResult> &)> Receiver) |
| 1604 | + : Receiver(Receiver) {} |
| 1605 | + |
| 1606 | + void handlePrimaryAST(ASTUnitRef AstUnit) override { |
| 1607 | + unsigned BufferID = |
| 1608 | + AstUnit->getPrimarySourceFile().getBufferID().getValue(); |
| 1609 | + auto &DiagConsumer = AstUnit->getEditorDiagConsumer(); |
| 1610 | + auto Diagnostics = DiagConsumer.getDiagnosticsForBuffer(BufferID); |
| 1611 | + Receiver(RequestResult<DiagnosticsResult>::fromResult(Diagnostics)); |
| 1612 | + } |
| 1613 | + }; |
| 1614 | + |
| 1615 | + auto Consumer = std::make_shared<DiagnosticsConsumer>(std::move(Receiver)); |
| 1616 | + |
| 1617 | + Lang.getASTManager()->processASTAsync(Invok, std::move(Consumer), |
| 1618 | + /*OncePerASTToken=*/nullptr, |
| 1619 | + CancellationToken, FileSystem); |
| 1620 | +} |
| 1621 | + |
1591 | 1622 | static void resolveName( |
1592 | 1623 | SwiftLangSupport &Lang, StringRef InputFile, unsigned Offset, |
1593 | 1624 | SwiftInvocationRef Invok, bool TryExistingAST, NameTranslatingInfo &Input, |
@@ -1846,6 +1877,33 @@ void SwiftLangSupport::getCursorInfo( |
1846 | 1877 | fileSystem, CancellationToken, Receiver); |
1847 | 1878 | } |
1848 | 1879 |
|
| 1880 | +void SwiftLangSupport::getDiagnostics( |
| 1881 | + StringRef InputFile, ArrayRef<const char *> Args, |
| 1882 | + Optional<VFSOptions> VfsOptions, |
| 1883 | + SourceKitCancellationToken CancellationToken, |
| 1884 | + std::function<void(const RequestResult<DiagnosticsResult> &)> Receiver) { |
| 1885 | + std::string FileSystemError; |
| 1886 | + auto FileSystem = getFileSystem(VfsOptions, InputFile, FileSystemError); |
| 1887 | + if (!FileSystem) { |
| 1888 | + Receiver(RequestResult<DiagnosticsResult>::fromError(FileSystemError)); |
| 1889 | + return; |
| 1890 | + } |
| 1891 | + |
| 1892 | + std::string InvocationError; |
| 1893 | + SwiftInvocationRef Invok = |
| 1894 | + ASTMgr->getInvocation(Args, InputFile, FileSystem, InvocationError); |
| 1895 | + if (!InvocationError.empty()) { |
| 1896 | + LOG_WARN_FUNC("error creating ASTInvocation: " << InvocationError); |
| 1897 | + } |
| 1898 | + if (!Invok) { |
| 1899 | + Receiver(RequestResult<DiagnosticsResult>::fromError(InvocationError)); |
| 1900 | + return; |
| 1901 | + } |
| 1902 | + |
| 1903 | + computeDiagnostics(*this, InputFile, Invok, FileSystem, CancellationToken, |
| 1904 | + Receiver); |
| 1905 | +} |
| 1906 | + |
1849 | 1907 | void SwiftLangSupport::getRangeInfo( |
1850 | 1908 | StringRef InputFile, unsigned Offset, unsigned Length, |
1851 | 1909 | bool CancelOnSubsequentRequest, ArrayRef<const char *> Args, |
|
0 commit comments