Skip to content

Commit 4937282

Browse files
authored
Merge pull request #77337 from swiftlang/multi-file-objc-message
ModuleObjcMessageTrace: report ObjC method calls from all source files under compilation
2 parents 2518691 + 6e42dfb commit 4937282

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

lib/FrontendTool/LoadedModuleTrace.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "swift/Basic/JSONSerialization.h"
2424
#include "swift/Basic/SourceManager.h"
2525
#include "swift/Frontend/FrontendOptions.h"
26+
#include "swift/Frontend/ModuleInterfaceSupport.h"
2627
#include "swift/IDE/SourceEntityWalker.h"
2728

2829
#include "clang/AST/DeclObjC.h"
@@ -819,18 +820,19 @@ bool swift::emitLoadedModuleTraceIfNeeded(ModuleDecl *mainModule,
819820
const static unsigned OBJC_METHOD_TRACE_FILE_FORMAT_VERSION = 1;
820821

821822
class ObjcMethodReferenceCollector: public SourceEntityWalker {
823+
std::string compilerVer;
822824
std::string target;
823825
std::string targetVariant;
824826
SmallVector<StringRef, 32> FilePaths;
825827
unsigned CurrentFileID;
826-
llvm::DenseSet<const clang::ObjCMethodDecl*> results;
828+
llvm::DenseMap<const clang::ObjCMethodDecl*, unsigned> results;
827829
bool visitDeclReference(ValueDecl *D, CharSourceRange Range,
828830
TypeDecl *CtorTyRef, ExtensionDecl *ExtTyRef,
829831
Type T, ReferenceMetaData Data) override {
830832
if (!Range.isValid())
831833
return true;
832834
if (auto *clangD = dyn_cast_or_null<clang::ObjCMethodDecl>(D->getClangDecl())) {
833-
results.insert(clangD);
835+
results[clangD] = CurrentFileID;
834836
}
835837
return true;
836838
}
@@ -855,6 +857,8 @@ class ObjcMethodReferenceCollector: public SourceEntityWalker {
855857
}
856858
public:
857859
ObjcMethodReferenceCollector(ModuleDecl *MD) {
860+
compilerVer =
861+
getSwiftInterfaceCompilerVersionForCurrentCompiler(MD->getASTContext());
858862
auto &Opts = MD->getASTContext().LangOpts;
859863
target = Opts.Target.str();
860864
targetVariant = Opts.TargetVariant.has_value() ?
@@ -868,27 +872,29 @@ class ObjcMethodReferenceCollector: public SourceEntityWalker {
868872
void serializeAsJson(llvm::raw_ostream &OS) {
869873
llvm::json::OStream out(OS, /*IndentSize=*/4);
870874
out.object([&] {
875+
out.attribute("swift-compiler-version", compilerVer);
871876
out.attribute("format-vesion", OBJC_METHOD_TRACE_FILE_FORMAT_VERSION);
872877
out.attribute("target", target);
873878
if (!targetVariant.empty())
874879
out.attribute("target-variant", targetVariant);
875880
out.attributeArray("references", [&] {
876-
for (const clang::ObjCMethodDecl* clangD: results) {
881+
for (auto pair: results) {
882+
auto *clangD = pair.first;
877883
auto &SM = clangD->getASTContext().getSourceManager();
878884
clang::SourceLocation Loc = clangD->getLocation();
879885
if (!Loc.isValid()) {
880886
continue;
881887
}
882888
out.object([&] {
883889
if (auto *parent = dyn_cast_or_null<clang::NamedDecl>(clangD
884-
->getParent())) {
890+
->getParent())) {
885891
auto pName = parent->getName();
886892
if (!pName.empty())
887893
out.attribute(selectMethodOwnerKey(parent), pName);
888894
}
889895
out.attribute(selectMethodKey(clangD), clangD->getNameAsString());
890896
out.attribute("declared_at", Loc.printToString(SM));
891-
out.attribute("referenced_at_file_id", CurrentFileID);
897+
out.attribute("referenced_at_file_id", pair.second);
892898
});
893899
}
894900
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import Foo
2+
3+
public func testProperties2(_ x: FooClassBase, _ y: FooProtocolBase) {
4+
y.fooProtoFunc()
5+
}

test/IDE/objc_send_collector.swift renamed to test/IDE/objc_send_collector_1.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// RUN: %empty-directory(%t)
22

3-
// 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
3+
// 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
44
// RUN: cat %t/.SWIFT_FINE_DEPENDENCY_TRACE/* | %FileCheck %s
55

66
// REQUIRES: objc_interop
@@ -21,5 +21,8 @@ public func testProperties(_ x: FooClassBase, _ y: FooProtocolBase) {
2121
// CHECK-DAG: "protocol_type": "FooProtocolBase"
2222
// CHECK-DAG: "declared_at": "SOURCE_DIR/test/IDE/Inputs/mock-sdk/Foo.framework/Headers/Foo.h
2323
// CHECK-DAG: "referenced_at_file_id": 1
24+
// CHECK-DAG: "referenced_at_file_id": 2
2425
// CHECK-DAG: "file_id": 1,
25-
// CHECK-DAG: "file_path": "SOURCE_DIR/test/IDE/objc_send_collector.swift"
26+
// CHECK-DAG: "file_path": "SOURCE_DIR/test/IDE/objc_send_collector_1.swift"
27+
// CHECK-DAG: "file_path": "SOURCE_DIR/test/IDE/Inputs/objc_send_collector_2.swift"
28+
// CHECK-DAG: "swift-compiler-version":

0 commit comments

Comments
 (0)