|
47 | 47 | #include "llvm/Support/CommandLine.h"
|
48 | 48 | #include "llvm/Support/FormattedStream.h"
|
49 | 49 | #include "llvm/Support/FileSystem.h"
|
| 50 | +#include <set> |
50 | 51 |
|
51 | 52 |
|
52 | 53 | using namespace swift;
|
@@ -2750,6 +2751,57 @@ printSILCoverageMaps(SILPrintContext &Ctx,
|
2750 | 2751 | M->print(Ctx);
|
2751 | 2752 | }
|
2752 | 2753 |
|
| 2754 | +using MagicFileStringMap = |
| 2755 | + llvm::StringMap<std::pair<std::string, /*isWinner=*/bool>>; |
| 2756 | + |
| 2757 | +static void |
| 2758 | +printMagicFileStringMapEntry(SILPrintContext &Ctx, |
| 2759 | + const MagicFileStringMap::MapEntryTy &entry) { |
| 2760 | + auto &OS = Ctx.OS(); |
| 2761 | + OS << "// '" << std::get<0>(entry.second) |
| 2762 | + << "' => '" << entry.first() << "'"; |
| 2763 | + |
| 2764 | + if (!std::get<1>(entry.second)) |
| 2765 | + OS << " (alternate)"; |
| 2766 | + |
| 2767 | + OS << "\n"; |
| 2768 | +} |
| 2769 | + |
| 2770 | +static void printMagicFileStringMap(SILPrintContext &Ctx, |
| 2771 | + const MagicFileStringMap map) { |
| 2772 | + if (map.empty()) |
| 2773 | + return; |
| 2774 | + |
| 2775 | + Ctx.OS() << "\n\n// Mappings from '#file' to '#filePath':\n"; |
| 2776 | + |
| 2777 | + if (Ctx.sortSIL()) { |
| 2778 | + llvm::SmallVector<llvm::StringRef, 16> keys; |
| 2779 | + llvm::copy(map.keys(), std::back_inserter(keys)); |
| 2780 | + |
| 2781 | + llvm::sort(keys, [&](StringRef leftKey, StringRef rightKey) -> bool { |
| 2782 | + const auto &leftValue = map.find(leftKey)->second; |
| 2783 | + const auto &rightValue = map.find(rightKey)->second; |
| 2784 | + |
| 2785 | + // Lexicographically earlier #file strings sort earlier. |
| 2786 | + if (std::get<0>(leftValue) != std::get<0>(rightValue)) |
| 2787 | + return std::get<0>(leftValue) < std::get<0>(rightValue); |
| 2788 | + |
| 2789 | + // Conflict winners sort before losers. |
| 2790 | + if (std::get<1>(leftValue) != std::get<1>(rightValue)) |
| 2791 | + return std::get<1>(leftValue); |
| 2792 | + |
| 2793 | + // Finally, lexicographically earlier #filePath strings sort earlier. |
| 2794 | + return leftKey < rightKey; |
| 2795 | + }); |
| 2796 | + |
| 2797 | + for (auto key : keys) |
| 2798 | + printMagicFileStringMapEntry(Ctx, *map.find(key)); |
| 2799 | + } else { |
| 2800 | + for (const auto &entry : map) |
| 2801 | + printMagicFileStringMapEntry(Ctx, entry); |
| 2802 | + } |
| 2803 | +} |
| 2804 | + |
2753 | 2805 | void SILProperty::print(SILPrintContext &Ctx) const {
|
2754 | 2806 | PrintOptions Options = PrintOptions::printSIL();
|
2755 | 2807 |
|
@@ -2864,6 +2916,10 @@ void SILModule::print(SILPrintContext &PrintCtx, ModuleDecl *M,
|
2864 | 2916 | printSILProperties(PrintCtx, getPropertyList());
|
2865 | 2917 | printExternallyVisibleDecls(PrintCtx, externallyVisible.getArrayRef());
|
2866 | 2918 |
|
| 2919 | + if (M) |
| 2920 | + printMagicFileStringMap( |
| 2921 | + PrintCtx, M->computeMagicFileStringMap()); |
| 2922 | + |
2867 | 2923 | OS << "\n\n";
|
2868 | 2924 | }
|
2869 | 2925 |
|
|
0 commit comments