Skip to content

Commit 54f5967

Browse files
committed
Include #file -> #filePath table in printed SIL
A proof of concept for tools providing this information in more useful forms.
1 parent 8e5ca8a commit 54f5967

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

lib/SIL/SILPrinter.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#include "llvm/Support/CommandLine.h"
4848
#include "llvm/Support/FormattedStream.h"
4949
#include "llvm/Support/FileSystem.h"
50+
#include <set>
5051

5152

5253
using namespace swift;
@@ -2750,6 +2751,57 @@ printSILCoverageMaps(SILPrintContext &Ctx,
27502751
M->print(Ctx);
27512752
}
27522753

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+
27532805
void SILProperty::print(SILPrintContext &Ctx) const {
27542806
PrintOptions Options = PrintOptions::printSIL();
27552807

@@ -2864,6 +2916,10 @@ void SILModule::print(SILPrintContext &PrintCtx, ModuleDecl *M,
28642916
printSILProperties(PrintCtx, getPropertyList());
28652917
printExternallyVisibleDecls(PrintCtx, externallyVisible.getArrayRef());
28662918

2919+
if (M)
2920+
printMagicFileStringMap(
2921+
PrintCtx, M->computeMagicFileStringMap());
2922+
28672923
OS << "\n\n";
28682924
}
28692925

test/SILGen/magic_identifier_file.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,7 @@ func forceTry(_ fn: () throws -> ()) {
3131
// ABSOLUTE: string_literal utf8 "SOURCE_DIR/test/SILGen/magic_identifier_file.swift"
3232
// CONCISE: string_literal utf8 "Foo/magic_identifier_file.swift"
3333
}
34+
35+
// CONCISE-LABEL: // Mappings from '#file' to '#filePath':
36+
// CONCISE: // 'Foo/magic_identifier_file.swift' => 'SOURCE_DIR/test/SILGen/magic_identifier_file.swift'
37+

0 commit comments

Comments
 (0)