Skip to content

Frontend: when emitting loaded module trace file, also emit a JSON file tracking Objc method calls issued from the source code #77217

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/FrontendTool/Dependencies.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ bool emitLoadedModuleTraceIfNeeded(ModuleDecl *mainModule,
const FrontendOptions &opts,
const InputFile &input);

bool emitObjCMessageSendTraceIfNeeded(ModuleDecl *mainModule,
const FrontendOptions &opts);

} // end namespace swift

#endif
1 change: 1 addition & 0 deletions lib/FrontendTool/FrontendTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,7 @@ static void performEndOfPipelineActions(CompilerInstance &Instance) {
Instance.getMainModule(), Instance.getDependencyTracker(), opts);

dumpAPIIfNeeded(Instance);
swift::emitObjCMessageSendTraceIfNeeded(Instance.getMainModule(), opts);
}

// Contains the hadError checks internally, we still want to output the
Expand Down
69 changes: 69 additions & 0 deletions lib/FrontendTool/LoadedModuleTrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@
#include "swift/AST/Module.h"
#include "swift/AST/ModuleLoader.h"
#include "swift/AST/PluginRegistry.h"
#include "swift/AST/SourceFile.h"
#include "swift/Basic/Assertions.h"
#include "swift/Basic/FileTypes.h"
#include "swift/Basic/JSONSerialization.h"
#include "swift/Frontend/FrontendOptions.h"
#include "swift/IDE/SourceEntityWalker.h"

#include "clang/Basic/Module.h"
#include "clang/AST/DeclObjC.h"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-format?


#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallString.h"
Expand Down Expand Up @@ -810,3 +813,69 @@ bool swift::emitLoadedModuleTraceIfNeeded(ModuleDecl *mainModule,
}
return false;
}


bool swift::emitObjCMessageSendTraceIfNeeded(ModuleDecl *mainModule,
const FrontendOptions &opts) {
ASTContext &ctxt = mainModule->getASTContext();
assert(!ctxt.hadError() &&
"We should've already exited earlier if there was an error.");
class ObjcMethodRefereceCollector: public SourceEntityWalker {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd pull out of this function personally

std::set<const clang::ObjCMethodDecl*> results;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DenseSet/SmallPtrSet?

bool visitDeclReference(ValueDecl *D, CharSourceRange Range,
TypeDecl *CtorTyRef, ExtensionDecl *ExtTyRef,
Type T, ReferenceMetaData Data) override {
if (!Range.isValid())
return true;
if (auto *clangD = dyn_cast_or_null<clang::ObjCMethodDecl>(D->getClangDecl())) {
results.insert(clangD);
}
return true;
}
public:
void dump(llvm::raw_ostream &OS) {
OS << "[\n";
for (const clang::ObjCMethodDecl* clangD: results) {
auto &SM = clangD->getASTContext().getSourceManager();
clang::SourceLocation Loc = clangD->getLocation();
if (!Loc.isValid()) {
continue;
}
OS << "\t{\n";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For small JSON output like this, it would be nice to use llvm::json::OStream. e.g. how ConstExtract.cpp's writeValue does.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Will update.

OS << "\t\t\"method_name\": \"" << clangD->getNameAsString() << "\",\n";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is just the method name really all that's needed? Is the expectation that the location will unique them?

OS << "\t\t\"location\": \"" << Loc.printToString(SM) << "\"\n";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure exactly what you're after here, but note this will be both the spelling + expansion + line + column. If you only need offset, you could make this a fair bit simpler.

OS << "\t}";
OS << ",\n";
}
OS << "{} ]\n";
}
};
opts.InputsAndOutputs.forEachInput([&](const InputFile &input) {
auto loadedModuleTracePath = input.getLoadedModuleTracePath();
if (loadedModuleTracePath.empty())
return false;
llvm::SmallString<128> tracePath {loadedModuleTracePath};
llvm::sys::path::remove_filename(tracePath);
llvm::sys::path::append(tracePath, "SWIFT_OBJC_MESSAGE_TRACE");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caps is a little weird but module trace does the same so 🤷. Maybe .SWIFT_OBJC_MESSAGE_TRACE then though?

if (!llvm::sys::fs::exists(tracePath)) {
if (llvm::sys::fs::create_directory(tracePath))
return false;
}
llvm::sys::path::append(tracePath, "%%%%-%%%%-%%%%.json");
int tmpFD;
if (llvm::sys::fs::createUniqueFile(tracePath.str(), tmpFD, tracePath)) {
return false;
}
// Write the contents of the buffer.
llvm::raw_fd_ostream out(tmpFD, /*shouldClose=*/true);
ObjcMethodRefereceCollector collector;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ObjcMethodRefereceCollector -> ObjcMethodReferenceCollector

for (auto *FU : mainModule->getFiles()) {
if (auto *SF = dyn_cast<SourceFile>(FU)) {
collector.walk(*SF);
}
}
collector.dump(out);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I see dump I think of debugging. serializeAsJson or something similar?

return true;
});
return false;
}
15 changes: 15 additions & 0 deletions test/IDE/Inputs/print_first_file_under_directory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env python3
#
# Usage: print_first_file_under_director.py /dir/

import os
import sys

directory = sys.argv[1]

for p in os.listdir(directory):
with open(directory + '/' + p, 'r') as f:
print(f.read())
exit(0)

exit(1)
20 changes: 20 additions & 0 deletions test/IDE/objc_send_collector.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// RUN: %empty-directory(%t)

// RUN: %target-swift-frontend -I %t/lib/swift -typecheck %s -module-name main -swift-version 5 -F %S/Inputs/mock-sdk -emit-loaded-module-trace-path %t/.MODULE_TRACE
// RUN: %{python} %S/Inputs/print_first_file_under_directory.py %t/SWIFT_OBJC_MESSAGE_TRACE | %FileCheck %s
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just RUN: cat %t/SWIFT_OBJC_MESSAGE_TRACE/*?


// REQUIRES: objc_interop

// EXPECTED OUTPUT STARTS BELOW THIS LINE.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't really the output 🤔. Also does this test really belong under IDE?



import Foo

public func testProperties(_ x: FooClassBase) {
_ = x.fooBaseInstanceFunc0()
x.fooBaseInstanceFunc1(1.2)
}

// CHECK: fooBaseInstanceFunc0
// CHECK: fooBaseInstanceFunc1
// CHECK: SOURCE_DIR/test/IDE/Inputs/mock-sdk/Foo.framework/Headers/Foo.h