Skip to content

Commit 2a2731a

Browse files
authored
migrator: add a flag to print incoming usrs to the API diff data store to facilitate testing. NFC (#8969)
1 parent 53745e6 commit 2a2731a

File tree

8 files changed

+32
-1
lines changed

8 files changed

+32
-1
lines changed

include/swift/IDE/APIDigesterData.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ struct APIDiffItemStore {
254254
~APIDiffItemStore();
255255
ArrayRef<APIDiffItem*> getDiffItems(StringRef Key) const;
256256
ArrayRef<APIDiffItem*> getAllDiffItems() const;
257+
void printIncomingUsr(bool print = true);
257258

258259
/// Add a path of a JSON file dumped from swift-api-digester that contains
259260
/// API changes we care about. Calling this can be heavy since the procedure

include/swift/Migrator/MigratorOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ struct MigratorOptions {
2828
/// compiler and applies them. This is generally for debugging.
2929
bool EnableMigratorFixits = true;
3030

31+
/// Whether to print each USR we query the api change data store about.
32+
bool DumpUsr = false;
33+
3134
/// If non-empty, print the last MigrationState's output text to the given
3235
/// file path.
3336
std::string EmitMigratedFilePath = "";

include/swift/Option/Options.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,9 @@ def api_diff_data_file: Separate<["-"], "api-diff-data-file">,
434434
HelpText<"API migration data is from <path>">,
435435
MetaVarName<"<path>">;
436436

437+
def dump_usr: Flag<["-"], "dump-usr">,
438+
Flags<[FrontendOption, NoInteractiveOption]>,
439+
HelpText<"Dump USR for each declaration reference">;
437440
// File types
438441

439442
def parse_as_library : Flag<["-"], "parse-as-library">,

lib/Driver/ToolChains.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,9 @@ ToolChain::constructInvocation(const CompileJobAction &job,
300300
Arguments.push_back("-api-diff-data-file");
301301
Arguments.push_back(DataPath->getValue());
302302
}
303+
if (context.Args.hasArg(options::OPT_dump_usr)) {
304+
Arguments.push_back("-dump-usr");
305+
}
303306
}
304307
}
305308
break;

lib/Frontend/CompilerInvocation.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1592,6 +1592,7 @@ bool ParseMigratorArgs(MigratorOptions &Opts, llvm::Triple &Triple,
15921592
using namespace options;
15931593

15941594
Opts.AddObjC |= Args.hasArg(OPT_warn_swift3_objc_inference);
1595+
Opts.DumpUsr = Args.hasArg(OPT_dump_usr);
15951596

15961597
if (Args.hasArg(OPT_disable_migrator_fixits)) {
15971598
Opts.EnableMigratorFixits = false;

lib/IDE/APIDigesterData.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ struct swift::ide::api::APIDiffItemStore::Implementation {
371371
llvm::BumpPtrAllocator Allocator;
372372
public:
373373
llvm::StringMap<std::vector<APIDiffItem*>> Data;
374+
bool PrintUsr;
374375
std::vector<APIDiffItem*> AllItems;
375376
void addStorePath(StringRef FileName) {
376377
llvm::MemoryBuffer *pMemBuffer = nullptr;
@@ -400,7 +401,11 @@ struct swift::ide::api::APIDiffItemStore::Implementation {
400401
};
401402

402403
ArrayRef<APIDiffItem*> swift::ide::api::APIDiffItemStore::
403-
getDiffItems(StringRef Key) const { return Impl.Data[Key]; }
404+
getDiffItems(StringRef Key) const {
405+
if (Impl.PrintUsr)
406+
llvm::outs() << Key;
407+
return Impl.Data[Key];
408+
}
404409

405410
ArrayRef<APIDiffItem*> swift::ide::api::APIDiffItemStore::
406411
getAllDiffItems() const { return Impl.AllItems; }
@@ -413,3 +418,7 @@ swift::ide::api::APIDiffItemStore::~APIDiffItemStore() { delete &Impl; }
413418
void swift::ide::api::APIDiffItemStore::addStorePath(StringRef Path) {
414419
Impl.addStorePath(Path);
415420
}
421+
422+
void swift::ide::api::APIDiffItemStore::printIncomingUsr(bool print) {
423+
Impl.PrintUsr = print;
424+
}

lib/Migrator/SyntacticMigratorPass.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ struct SyntacticMigratorPass::Implementation : public SourceEntityWalker {
7676
if (Opts.APIDigesterDataStorePath.empty())
7777
return;
7878
DiffStore.addStorePath(Opts.APIDigesterDataStorePath);
79+
DiffStore.printIncomingUsr(Opts.DumpUsr);
7980
walk(SF);
8081
}
8182

test/migrator/dump_usr.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// REQUIRES: objc_interop
2+
// RUN: rm -rf %t && mkdir -p %t && %swift -update-code -primary-file %s -F %S/mock-sdk -api-diff-data-file %S/API.json -o %t/member.swift.remap -dump-usr | %FileCheck %s -check-prefix=USR
3+
4+
import Bar
5+
6+
func foo(_ b: BarForwardDeclaredClass) -> Int32 {
7+
return barGlobalVariable
8+
}
9+
10+
// USR: c:objc(cs)BarForwardDeclaredClasss:s5Int32Vc:@barGlobalVariable

0 commit comments

Comments
 (0)