Skip to content

Commit 755fad8

Browse files
alx32Alex Borcan
authored andcommitted
[lld-macho] Make ObjC category checker print the source file name of category (llvm#80221)
When printing category conflicts in the ObjC category checker, also print the source file name of the problematic categories. Currently we only print the object file name. This change is mostly useful only for thinLTO builds, where the object file name will be of form 999.arm64.lto.o and thus does not reveal any information about the original source file. --------- Co-authored-by: Alex Borcan <[email protected]>
1 parent 5b41572 commit 755fad8

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

lld/MachO/ObjC.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,12 +237,23 @@ void ObjcCategoryChecker::parseMethods(const ConcatInputSection *methodsIsec,
237237
StringRef newCatName =
238238
getReferentString(*containerIsec->getRelocAt(catLayout.nameOffset));
239239

240+
auto formatObjAndSrcFileName = [](const InputSection *section) {
241+
lld::macho::InputFile *inputFile = section->getFile();
242+
std::string result = toString(inputFile);
243+
244+
auto objFile = dyn_cast_or_null<ObjFile>(inputFile);
245+
if (objFile && objFile->compileUnit)
246+
result += " (" + objFile->sourceFile() + ")";
247+
248+
return result;
249+
};
250+
240251
StringRef containerType = mc.kind == MCK_Category ? "category" : "class";
241252
warn("method '" + methPrefix + methodName.val() +
242253
"' has conflicting definitions:\n>>> defined in category " +
243-
newCatName + " from " + toString(containerIsec->getFile()) +
254+
newCatName + " from " + formatObjAndSrcFileName(containerIsec) +
244255
"\n>>> defined in " + containerType + " " + containerName + " from " +
245-
toString(mc.isec->getFile()));
256+
formatObjAndSrcFileName(mc.isec));
246257
}
247258
}
248259

lld/test/MachO/objc-category-conflicts.s

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos11.0 -I %t %t/cat1.s -o %t/cat1.o
44
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos11.0 -I %t %t/cat2.s -o %t/cat2.o
55
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos11.0 -I %t %t/klass.s -o %t/klass.o
6+
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos11.0 -I %t %t/cat1.s -g -o %t/cat1_w_sym.o
7+
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos11.0 -I %t %t/cat2.s -g -o %t/cat2_w_sym.o
8+
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos11.0 -I %t %t/klass.s -g -o %t/klass_w_sym.o
69
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos11.0 -I %t %t/cat1.s --defsym MAKE_LOAD_METHOD=1 -o %t/cat1-with-load.o
710
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos11.0 -I %t %t/cat2.s --defsym MAKE_LOAD_METHOD=1 -o %t/cat2-with-load.o
811
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos11.0 -I %t %t/klass.s --defsym MAKE_LOAD_METHOD=1 -o %t/klass-with-load.o
@@ -14,6 +17,11 @@
1417
# RUN: %no-fatal-warnings-lld --check-category-conflicts -dylib -lobjc %t/libklass.dylib %t/cat1.o \
1518
# RUN: %t/cat2.o -o /dev/null 2>&1 | FileCheck %s --check-prefix=CATCAT
1619

20+
# RUN: %no-fatal-warnings-lld --check-category-conflicts -dylib -lobjc %t/klass_w_sym.o %t/cat1_w_sym.o %t/cat2_w_sym.o -o \
21+
# RUN: /dev/null 2>&1 | FileCheck %s --check-prefixes=CATCLS_W_SYM,CATCAT_W_SYM
22+
# RUN: %no-fatal-warnings-lld --check-category-conflicts -dylib -lobjc %t/libklass.dylib %t/cat1_w_sym.o \
23+
# RUN: %t/cat2_w_sym.o -o /dev/null 2>&1 | FileCheck %s --check-prefix=CATCAT_W_SYM
24+
1725
## Check that we don't emit spurious warnings around the +load method while
1826
## still emitting the other warnings. Note that we have made separate
1927
## `*-with-load.s` files for ease of comparison with ld64; ld64 will not warn
@@ -45,6 +53,24 @@
4553
# CATCAT-NEXT: >>> defined in category Cat2 from {{.*}}cat2{{.*}}.o
4654
# CATCAT-NEXT: >>> defined in category Cat1 from {{.*}}cat1{{.*}}.o
4755

56+
57+
# CATCLS_W_SYM: warning: method '+s1' has conflicting definitions:
58+
# CATCLS_W_SYM-NEXT: >>> defined in category Cat1 from {{.*}}cat1_w_sym{{.*}}.o ({{.*}}cat1.s{{.*}})
59+
# CATCLS_W_SYM-NEXT: >>> defined in class Foo from {{.*}}klass_w_sym{{.*}}.o ({{.*}}klass.s{{.*}})
60+
61+
# CATCLS_W_SYM: warning: method '-m1' has conflicting definitions:
62+
# CATCLS_W_SYM-NEXT: >>> defined in category Cat1 from {{.*}}cat1_w_sym{{.*}}.o ({{.*}}cat1.s{{.*}})
63+
# CATCLS_W_SYM-NEXT: >>> defined in class Foo from {{.*}}klass_w_sym{{.*}}.o ({{.*}}klass.s{{.*}})
64+
65+
# CATCAT_W_SYM: warning: method '+s2' has conflicting definitions:
66+
# CATCAT_W_SYM-NEXT: >>> defined in category Cat2 from {{.*}}cat2_w_sym{{.*}}.o ({{.*}}cat2.s{{.*}})
67+
# CATCAT_W_SYM-NEXT: >>> defined in category Cat1 from {{.*}}cat1_w_sym{{.*}}.o ({{.*}}cat1.s{{.*}})
68+
69+
# CATCAT_W_SYM: warning: method '-m2' has conflicting definitions:
70+
# CATCAT_W_SYM-NEXT: >>> defined in category Cat2 from {{.*}}cat2_w_sym{{.*}}.o ({{.*}}cat2.s{{.*}})
71+
# CATCAT_W_SYM-NEXT: >>> defined in category Cat1 from {{.*}}cat1_w_sym{{.*}}.o ({{.*}}cat1.s{{.*}})
72+
73+
4874
#--- cat1.s
4975

5076
.include "objc-macros.s"
@@ -55,7 +81,7 @@
5581
## +(void) s1;
5682
## +(void) s2;
5783
## @end
58-
##
84+
##
5985
## @implementation Foo(Cat1)
6086
## -(void) m1 {}
6187
## -(void) m2 {}
@@ -114,7 +140,7 @@ __OBJC_$_CATEGORY_CLASS_METHODS_Foo_$_Cat1:
114140
## -(void) m2;
115141
## +(void) s2;
116142
## @end
117-
##
143+
##
118144
## @implementation Foo(Cat2)
119145
## -(void) m2 {}
120146
## +(void) s2 {}

0 commit comments

Comments
 (0)