ModuleObjcMessageTrace: report ObjC method calls from all source files under compilation

This commit is contained in:
Xi Ge
2024-10-31 14:50:14 -07:00
parent 108ad6f115
commit 95f2523ef3
3 changed files with 15 additions and 7 deletions

View File

@@ -823,14 +823,14 @@ class ObjcMethodReferenceCollector: public SourceEntityWalker {
std::string targetVariant; std::string targetVariant;
SmallVector<StringRef, 32> FilePaths; SmallVector<StringRef, 32> FilePaths;
unsigned CurrentFileID; unsigned CurrentFileID;
llvm::DenseSet<const clang::ObjCMethodDecl*> results; llvm::DenseMap<const clang::ObjCMethodDecl*, unsigned> results;
bool visitDeclReference(ValueDecl *D, CharSourceRange Range, bool visitDeclReference(ValueDecl *D, CharSourceRange Range,
TypeDecl *CtorTyRef, ExtensionDecl *ExtTyRef, TypeDecl *CtorTyRef, ExtensionDecl *ExtTyRef,
Type T, ReferenceMetaData Data) override { Type T, ReferenceMetaData Data) override {
if (!Range.isValid()) if (!Range.isValid())
return true; return true;
if (auto *clangD = dyn_cast_or_null<clang::ObjCMethodDecl>(D->getClangDecl())) { if (auto *clangD = dyn_cast_or_null<clang::ObjCMethodDecl>(D->getClangDecl())) {
results.insert(clangD); results[clangD] = CurrentFileID;
} }
return true; return true;
} }
@@ -873,7 +873,8 @@ public:
if (!targetVariant.empty()) if (!targetVariant.empty())
out.attribute("target-variant", targetVariant); out.attribute("target-variant", targetVariant);
out.attributeArray("references", [&] { out.attributeArray("references", [&] {
for (const clang::ObjCMethodDecl* clangD: results) { for (auto pair: results) {
auto *clangD = pair.first;
auto &SM = clangD->getASTContext().getSourceManager(); auto &SM = clangD->getASTContext().getSourceManager();
clang::SourceLocation Loc = clangD->getLocation(); clang::SourceLocation Loc = clangD->getLocation();
if (!Loc.isValid()) { if (!Loc.isValid()) {
@@ -881,14 +882,14 @@ public:
} }
out.object([&] { out.object([&] {
if (auto *parent = dyn_cast_or_null<clang::NamedDecl>(clangD if (auto *parent = dyn_cast_or_null<clang::NamedDecl>(clangD
->getParent())) { ->getParent())) {
auto pName = parent->getName(); auto pName = parent->getName();
if (!pName.empty()) if (!pName.empty())
out.attribute(selectMethodOwnerKey(parent), pName); out.attribute(selectMethodOwnerKey(parent), pName);
} }
out.attribute(selectMethodKey(clangD), clangD->getNameAsString()); out.attribute(selectMethodKey(clangD), clangD->getNameAsString());
out.attribute("declared_at", Loc.printToString(SM)); out.attribute("declared_at", Loc.printToString(SM));
out.attribute("referenced_at_file_id", CurrentFileID); out.attribute("referenced_at_file_id", pair.second);
}); });
} }
}); });

View File

@@ -0,0 +1,5 @@
import Foo
public func testProperties2(_ x: FooClassBase, _ y: FooProtocolBase) {
y.fooProtoFunc()
}

View File

@@ -1,6 +1,6 @@
// RUN: %empty-directory(%t) // 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: %target-swift-frontend -I %t/lib/swift -typecheck %s %S/Inputs/objc_send_collector_2.swift -module-name main -swift-version 5 -F %S/Inputs/mock-sdk -emit-loaded-module-trace-path %t/.MODULE_TRACE
// RUN: cat %t/.SWIFT_FINE_DEPENDENCY_TRACE/* | %FileCheck %s // RUN: cat %t/.SWIFT_FINE_DEPENDENCY_TRACE/* | %FileCheck %s
// REQUIRES: objc_interop // REQUIRES: objc_interop
@@ -21,5 +21,7 @@ public func testProperties(_ x: FooClassBase, _ y: FooProtocolBase) {
// CHECK-DAG: "protocol_type": "FooProtocolBase" // CHECK-DAG: "protocol_type": "FooProtocolBase"
// CHECK-DAG: "declared_at": "SOURCE_DIR/test/IDE/Inputs/mock-sdk/Foo.framework/Headers/Foo.h // CHECK-DAG: "declared_at": "SOURCE_DIR/test/IDE/Inputs/mock-sdk/Foo.framework/Headers/Foo.h
// CHECK-DAG: "referenced_at_file_id": 1 // CHECK-DAG: "referenced_at_file_id": 1
// CHECK-DAG: "referenced_at_file_id": 2
// CHECK-DAG: "file_id": 1, // CHECK-DAG: "file_id": 1,
// CHECK-DAG: "file_path": "SOURCE_DIR/test/IDE/objc_send_collector.swift" // CHECK-DAG: "file_path": "SOURCE_DIR/test/IDE/objc_send_collector_1.swift"
// CHECK-DAG: "file_path": "SOURCE_DIR/test/IDE/Inputs/objc_send_collector_2.swift"