|
| 1 | +//===--- DiagnoseUnnecessaryPreconcurrencyImports.cpp ---------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 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 | +/// \file |
| 14 | +/// |
| 15 | +/// This is run after TransferNonSendable and uses Sema infrastructure to |
| 16 | +/// determine if in Sema or TransferNonSendable any of the preconcurrency import |
| 17 | +/// statements were not used. |
| 18 | +/// |
| 19 | +/// This only runs when RegionIsolation is enabled. If RegionIsolation is |
| 20 | +/// disabled, we emit the unnecessary preconcurrency imports earlier during Sema |
| 21 | +/// since no later diagnostics will be emitted. |
| 22 | +/// |
| 23 | +/// NOTE: This needs to be a module pass and run after TransferNonSendable so we |
| 24 | +/// can guarantee that we have run TransferNonSendable on all functions in our |
| 25 | +/// module before this runs. |
| 26 | +/// |
| 27 | +//===----------------------------------------------------------------------===// |
| 28 | + |
| 29 | +#include "swift/AST/SourceFile.h" |
| 30 | +#include "swift/SILOptimizer/PassManager/Transforms.h" |
| 31 | +#include "swift/Sema/Concurrency.h" |
| 32 | + |
| 33 | +using namespace swift; |
| 34 | + |
| 35 | +//===----------------------------------------------------------------------===// |
| 36 | +// MARK: Top Level Entrypoint |
| 37 | +//===----------------------------------------------------------------------===// |
| 38 | + |
| 39 | +namespace { |
| 40 | + |
| 41 | +class DiagnoseUnnecessaryPreconcurrencyImports : public SILModuleTransform { |
| 42 | + void run() override { |
| 43 | + // If region isolation is not enabled... return early. |
| 44 | + if (!getModule()->getASTContext().LangOpts.hasFeature( |
| 45 | + Feature::RegionBasedIsolation)) |
| 46 | + return; |
| 47 | + |
| 48 | + std::vector<SourceFile *> data; |
| 49 | + for (auto &fn : *getModule()) { |
| 50 | + auto *sf = fn.getSourceFile(); |
| 51 | + if (!sf) { |
| 52 | + continue; |
| 53 | + } |
| 54 | + |
| 55 | + data.push_back(sf); |
| 56 | + assert(sf->getBufferID() != -1 && "Must have a buffer id"); |
| 57 | + } |
| 58 | + |
| 59 | + // Sort unique by filename so our diagnostics are deterministic. |
| 60 | + // |
| 61 | + // TODO: If we cannot rely upon this, just sort by pointer address. Non |
| 62 | + // determinism emission of diagnostics isn't great but it isn't fatal. |
| 63 | + sortUnique(data, [](SourceFile *lhs, SourceFile *rhs) -> bool { |
| 64 | + return lhs->getBufferID() < rhs->getBufferID(); |
| 65 | + }); |
| 66 | + |
| 67 | + // At this point, we know that we have our sorted unique list of source |
| 68 | + // files. |
| 69 | + for (auto *sf : data) { |
| 70 | + diagnoseUnnecessaryPreconcurrencyImports(*sf); |
| 71 | + } |
| 72 | + } |
| 73 | +}; |
| 74 | + |
| 75 | +} // namespace |
| 76 | + |
| 77 | +SILTransform *swift::createDiagnoseUnnecessaryPreconcurrencyImports() { |
| 78 | + return new DiagnoseUnnecessaryPreconcurrencyImports(); |
| 79 | +} |
0 commit comments