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

Conversation

nkcsgexi
Copy link
Contributor

No description provided.

…le tracking Objc method calls issued from the source code.
@nkcsgexi
Copy link
Contributor Author

@swift-ci please smoke test

@nkcsgexi nkcsgexi enabled auto-merge October 25, 2024 02:43
@nkcsgexi nkcsgexi merged commit 4679187 into main Oct 25, 2024
3 checks passed
@nkcsgexi nkcsgexi deleted the objc_trace_build branch October 25, 2024 04:22
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

assert(!ctxt.hadError() &&
"We should've already exited earlier if there was an error.");
class ObjcMethodRefereceCollector: public SourceEntityWalker {
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?

}
OS << "\t{\n";
OS << "\t\t\"method_name\": \"" << clangD->getNameAsString() << "\",\n";
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.

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?

}
// 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

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?


// 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?

continue;
}
OS << "\t{\n";
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?

// 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/*?


#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?

@nkcsgexi
Copy link
Contributor Author

Thank you for the comments, Ben! Will address via a follow-up PR. Regarding the exact content/details of this tracing file, we are still discussing with clients to settle the details.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants