|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2024 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include "NullEditorConsumer.h" |
| 14 | +#include "SourceKit/Core/Context.h" |
| 15 | +#include "SourceKit/Core/LangSupport.h" |
| 16 | +#include "SourceKit/Core/NotificationCenter.h" |
| 17 | +#include "SourceKit/Support/Concurrency.h" |
| 18 | +#include "SourceKit/SwiftLang/Factory.h" |
| 19 | +#include "swift/Basic/LLVMInitialize.h" |
| 20 | +#include "gtest/gtest.h" |
| 21 | + |
| 22 | +#include <chrono> |
| 23 | +#include <condition_variable> |
| 24 | +#include <mutex> |
| 25 | +#include <thread> |
| 26 | + |
| 27 | +using namespace SourceKit; |
| 28 | +using namespace llvm; |
| 29 | + |
| 30 | +static StringRef getRuntimeLibPath() { |
| 31 | + return sys::path::parent_path(SWIFTLIB_DIR); |
| 32 | +} |
| 33 | + |
| 34 | +static SmallString<128> getSwiftExecutablePath() { |
| 35 | + SmallString<128> path = sys::path::parent_path(getRuntimeLibPath()); |
| 36 | + sys::path::append(path, "bin", "swift-frontend"); |
| 37 | + return path; |
| 38 | +} |
| 39 | + |
| 40 | +namespace { |
| 41 | +class CompileTrackingConsumer final : public trace::TraceConsumer { |
| 42 | + std::mutex Mtx; |
| 43 | + std::condition_variable CV; |
| 44 | + bool HasStarted = false; |
| 45 | + |
| 46 | +public: |
| 47 | + CompileTrackingConsumer() {} |
| 48 | + CompileTrackingConsumer(const CompileTrackingConsumer &) = delete; |
| 49 | + |
| 50 | + void operationStarted(uint64_t OpId, trace::OperationKind OpKind, |
| 51 | + const trace::SwiftInvocation &Inv, |
| 52 | + const trace::StringPairs &OpArgs) override { |
| 53 | + std::unique_lock<std::mutex> lk(Mtx); |
| 54 | + HasStarted = true; |
| 55 | + CV.notify_all(); |
| 56 | + } |
| 57 | + |
| 58 | + void waitForBuildToStart() { |
| 59 | + std::unique_lock<std::mutex> lk(Mtx); |
| 60 | + auto secondsToWait = std::chrono::seconds(20); |
| 61 | + auto when = std::chrono::system_clock::now() + secondsToWait; |
| 62 | + CV.wait_until(lk, when, [&]() { return HasStarted; }); |
| 63 | + HasStarted = false; |
| 64 | + } |
| 65 | + |
| 66 | + void operationFinished(uint64_t OpId, trace::OperationKind OpKind, |
| 67 | + ArrayRef<DiagnosticEntryInfo> Diagnostics) override {} |
| 68 | + |
| 69 | + swift::OptionSet<trace::OperationKind> desiredOperations() override { |
| 70 | + return trace::OperationKind::PerformSema; |
| 71 | + } |
| 72 | +}; |
| 73 | + |
| 74 | +class CloseTest : public ::testing::Test { |
| 75 | + std::shared_ptr<SourceKit::Context> Ctx; |
| 76 | + std::shared_ptr<CompileTrackingConsumer> CompileTracker; |
| 77 | + |
| 78 | + NullEditorConsumer Consumer; |
| 79 | + |
| 80 | +public: |
| 81 | + CloseTest() { |
| 82 | + INITIALIZE_LLVM(); |
| 83 | + Ctx = std::make_shared<SourceKit::Context>( |
| 84 | + getSwiftExecutablePath(), getRuntimeLibPath(), |
| 85 | + /*diagnosticDocumentationPath*/ "", SourceKit::createSwiftLangSupport, |
| 86 | + /*dispatchOnMain=*/false); |
| 87 | + } |
| 88 | + |
| 89 | + CompileTrackingConsumer &getCompileTracker() const { return *CompileTracker; } |
| 90 | + LangSupport &getLang() { return Ctx->getSwiftLangSupport(); } |
| 91 | + |
| 92 | + void SetUp() override { |
| 93 | + CompileTracker = std::make_shared<CompileTrackingConsumer>(); |
| 94 | + trace::registerConsumer(CompileTracker.get()); |
| 95 | + } |
| 96 | + |
| 97 | + void TearDown() override { |
| 98 | + trace::unregisterConsumer(CompileTracker.get()); |
| 99 | + CompileTracker = nullptr; |
| 100 | + } |
| 101 | + |
| 102 | + void open(const char *DocName, StringRef Text, ArrayRef<const char *> CArgs) { |
| 103 | + auto Args = makeArgs(DocName, CArgs); |
| 104 | + auto Buf = MemoryBuffer::getMemBufferCopy(Text, DocName); |
| 105 | + getLang().editorOpen(DocName, Buf.get(), Consumer, Args, std::nullopt); |
| 106 | + } |
| 107 | + |
| 108 | + void close(const char *DocName, bool removeCache) { |
| 109 | + getLang().editorClose(DocName, removeCache); |
| 110 | + } |
| 111 | + |
| 112 | + void getDiagnosticsAsync( |
| 113 | + const char *DocName, ArrayRef<const char *> CArgs, |
| 114 | + llvm::function_ref<void(const RequestResult<DiagnosticsResult> &)> |
| 115 | + callback) { |
| 116 | + auto Args = makeArgs(DocName, CArgs); |
| 117 | + getLang().getDiagnostics(DocName, Args, /*VFSOpts*/ std::nullopt, |
| 118 | + /*CancelToken*/ {}, callback); |
| 119 | + } |
| 120 | + |
| 121 | +private: |
| 122 | + std::vector<const char *> makeArgs(const char *DocName, |
| 123 | + ArrayRef<const char *> CArgs) { |
| 124 | + std::vector<const char *> Args = CArgs; |
| 125 | + Args.push_back(DocName); |
| 126 | + return Args; |
| 127 | + } |
| 128 | +}; |
| 129 | + |
| 130 | +} // end anonymous namespace |
| 131 | + |
| 132 | +static const char *getComplexSourceText() { |
| 133 | + // best of luck, type-checker |
| 134 | + return |
| 135 | + "struct A: ExpressibleByIntegerLiteral { init(integerLiteral value: Int) {} }\n" |
| 136 | + "struct B: ExpressibleByIntegerLiteral { init(integerLiteral value: Int) {} }\n" |
| 137 | + "struct C: ExpressibleByIntegerLiteral { init(integerLiteral value: Int) {} }\n" |
| 138 | + |
| 139 | + "func + (lhs: A, rhs: B) -> A { fatalError() }\n" |
| 140 | + "func + (lhs: B, rhs: C) -> A { fatalError() }\n" |
| 141 | + "func + (lhs: C, rhs: A) -> A { fatalError() }\n" |
| 142 | + |
| 143 | + "func + (lhs: B, rhs: A) -> B { fatalError() }\n" |
| 144 | + "func + (lhs: C, rhs: B) -> B { fatalError() }\n" |
| 145 | + "func + (lhs: A, rhs: C) -> B { fatalError() }\n" |
| 146 | + |
| 147 | + "func + (lhs: C, rhs: B) -> C { fatalError() }\n" |
| 148 | + "func + (lhs: B, rhs: C) -> C { fatalError() }\n" |
| 149 | + "func + (lhs: A, rhs: A) -> C { fatalError() }\n" |
| 150 | + |
| 151 | + "let x: C = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8\n"; |
| 152 | +} |
| 153 | + |
| 154 | +TEST_F(CloseTest, Cancel) { |
| 155 | + const char *DocName = "test.swift"; |
| 156 | + auto *Contents = getComplexSourceText(); |
| 157 | + const char *Args[] = {"-parse-as-library"}; |
| 158 | + |
| 159 | + // Test twice with RemoveCache = false to test both the prior state of |
| 160 | + // the ASTProducer being cached and not cached. |
| 161 | + for (auto RemoveCache : {true, false, false}) { |
| 162 | + open(DocName, Contents, Args); |
| 163 | + |
| 164 | + Semaphore BuildResultSema(0); |
| 165 | + |
| 166 | + getDiagnosticsAsync(DocName, Args, |
| 167 | + [&](const RequestResult<DiagnosticsResult> &Result) { |
| 168 | + EXPECT_TRUE(Result.isCancelled()); |
| 169 | + BuildResultSema.signal(); |
| 170 | + }); |
| 171 | + |
| 172 | + getCompileTracker().waitForBuildToStart(); |
| 173 | + |
| 174 | + close(DocName, RemoveCache); |
| 175 | + |
| 176 | + bool Expired = BuildResultSema.wait(30 * 1000); |
| 177 | + if (Expired) |
| 178 | + llvm::report_fatal_error("Did not receive a response for the request"); |
| 179 | + } |
| 180 | +} |
0 commit comments