Skip to content

ModuleTrace: address code review comments. NFC #77230

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 2 commits into from
Oct 26, 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
70 changes: 39 additions & 31 deletions lib/FrontendTool/LoadedModuleTrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@
#include "swift/Frontend/FrontendOptions.h"
#include "swift/IDE/SourceEntityWalker.h"

#include "clang/Basic/Module.h"
#include "clang/AST/DeclObjC.h"
#include "clang/Basic/Module.h"

#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/Support/JSON.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/YAMLTraits.h"
#include "llvm/Support/FileUtilities.h"
Expand Down Expand Up @@ -814,49 +815,56 @@ 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 {
std::set<const clang::ObjCMethodDecl*> results;
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);
}
class ObjcMethodReferenceCollector: public SourceEntityWalker {
llvm::DenseSet<const clang::ObjCMethodDecl*> results;
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);
}
public:
void dump(llvm::raw_ostream &OS) {
OS << "[\n";
return true;
}
public:
void serializeAsJson(llvm::raw_ostream &OS) {
llvm::json::OStream out(OS, /*IndentSize=*/4);
out.array([&] {
for (const clang::ObjCMethodDecl* clangD: results) {
auto &SM = clangD->getASTContext().getSourceManager();
clang::SourceLocation Loc = clangD->getLocation();
if (!Loc.isValid()) {
continue;
}
OS << "\t{\n";
OS << "\t\t\"method_name\": \"" << clangD->getNameAsString() << "\",\n";
OS << "\t\t\"location\": \"" << Loc.printToString(SM) << "\"\n";
OS << "\t}";
OS << ",\n";
out.object([&] {
if (auto *parent = dyn_cast_or_null<clang::NamedDecl>(clangD
->getParent())) {
auto pName = parent->getName();
if (!pName.empty())
out.attribute("type", pName);
}
out.attribute("method", clangD->getNameAsString());
out.attribute("location", Loc.printToString(SM));
});
}
OS << "{} ]\n";
}
};
});
}
};

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.");

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");
llvm::sys::path::append(tracePath, ".SWIFT_OBJC_MESSAGE_TRACE");
if (!llvm::sys::fs::exists(tracePath)) {
if (llvm::sys::fs::create_directory(tracePath))
return false;
Expand All @@ -868,13 +876,13 @@ bool swift::emitObjCMessageSendTraceIfNeeded(ModuleDecl *mainModule,
}
// Write the contents of the buffer.
llvm::raw_fd_ostream out(tmpFD, /*shouldClose=*/true);
ObjcMethodRefereceCollector collector;
ObjcMethodReferenceCollector collector;
for (auto *FU : mainModule->getFiles()) {
if (auto *SF = dyn_cast<SourceFile>(FU)) {
collector.walk(*SF);
}
}
collector.dump(out);
collector.serializeAsJson(out);
return true;
});
return false;
Expand Down
15 changes: 0 additions & 15 deletions test/IDE/Inputs/print_first_file_under_directory.py

This file was deleted.

12 changes: 5 additions & 7 deletions test/IDE/objc_send_collector.swift
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
// 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
// RUN: cat %t/.SWIFT_OBJC_MESSAGE_TRACE/* | %FileCheck %s

// REQUIRES: objc_interop

// EXPECTED OUTPUT STARTS BELOW THIS LINE.


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
// CHECK-DAG: fooBaseInstanceFunc0
// CHECK-DAG: fooBaseInstanceFunc1
// CHECK-DAG: "type": "FooClassBase"
// CHECK-DAG: SOURCE_DIR/test/IDE/Inputs/mock-sdk/Foo.framework/Headers/Foo.h