This is an archive of the discontinued LLVM Phabricator instance.

[modules] Merge ObjC interface ivars with anonymous types.
ClosedPublic

Authored by vsapsai on Jan 28 2022, 7:53 PM.

Details

Summary

Without the fix ivars with anonymous types can trigger errors like

error: 'TestClass::structIvar' from module 'Target' is not present in definition of 'TestClass' provided earlier
[...]
note: declaration of 'structIvar' does not match

It happens because types of ivars from different modules are considered
to be different. And it is caused by not merging anonymous TagDecl
from different modules.

To fix that I've changed serialization::needsAnonymousDeclarationNumber
to handle anonymous TagDecl inside ObjCInterfaceDecl. But that's not
sufficient as C code inside ObjCInterfaceDecl doesn't use interface
decl as a decl context but switches to its parent (TranslationUnit in
most cases). I'm changing that to make ObjCContainerDecl the lexical
decl context but keeping the semantic decl context intact.

Test "check-dup-decls-inside-objc.m" doesn't reflect a change in
functionality but captures the existing behavior to prevent regressions.

rdar://85563013

Diff Detail

Repository
rC Clang

Event Timeline

vsapsai created this revision.Jan 28 2022, 7:53 PM
vsapsai requested review of this revision.Jan 28 2022, 7:53 PM
vsapsai added a subscriber: akyrtzi.

@akyrtzi I believe you were working on the code responsible for decl contexts at some point. I'm not sure you'll be able to review the change. Can you recommend someone else knowledgeable with this code?

vsapsai updated this revision to Diff 406977.Feb 8 2022, 2:31 PM

Rebase and address clang-format comment.

Herald added a project: Restricted Project. · View Herald TranscriptFeb 8 2022, 2:31 PM

This is one of those patches that's difficult to review because it's so hard to foresee the consequences of changing the concepts. That said, I think the basic idea here seems reasonable.

clang/test/Modules/merge-anon-record-definition-in-objc.m
6

Do you want to test this in Objective-C++ mode as well?

vsapsai added inline comments.Feb 28 2022, 11:43 AM
clang/test/Modules/merge-anon-record-definition-in-objc.m
6

That's a good idea! Let me add that.

The modules side of this looks good to me, and logically changing the lexical decl context in an interface to be that interface makes a lot of sense, but I agree with @rjmccall that it's hard to predict what the consequences of that change might be. Can you also test this against some body of real-world code?

vsapsai planned changes to this revision.Mar 7 2022, 1:43 PM
vsapsai added inline comments.
clang/test/Modules/merge-anon-record-definition-in-objc.m
6

It turned out to be a great idea! Looks like Objective-C++ mode has exposed an actual issue. After skipping a bunch of "cannot be defined in a parameter type" I'm hitting "error: use of undeclared identifier 'kX'". And in non-modular case

// clang -fsyntax-only test.mm
@interface NSObject
@end
@interface TestSubject: NSObject {
@public
    enum { kEnumConstant = 0, } enumIvar;
}
@end

int test() {
    return kEnumConstant;
}

does not trigger any errors. So I believe there should be no errors in my test case too.

Herald added a project: Restricted Project. · View Herald TranscriptMar 7 2022, 1:43 PM
vsapsai updated this revision to Diff 417468.Mar 22 2022, 7:13 PM

Fix bug with anonymous enum constant lookup in C++.

Turns out the lookup was broken by using ObjCInterfaceDecl as a semantic decl
context for anonymous enums. Fixing that fixed the lookup.

Also added in tests checking scoped enums in C++. Scoped enums cannot be
anonymous, so test only named enum definitions inside Objective-C code.

vsapsai edited the summary of this revision. (Show Details)Mar 28 2022, 4:47 PM
vsapsai added a subscriber: benlangmuir.

I've run clang with this change on a bunch of code (especially Objective-C code) and there were no regressions.

Also adding @benlangmuir to reviewers as people mentioned he was doing some work in this area earlier.

Thanks everyone for the feedback! Especially valuable is opinion on making ObjCContainerDecl a lexical decl context. As discussed earlier, not many people can provide feedback on Objective-C-related code and I'm still responsible for any problems caused by this change. So I'll commit the change and please let me know if it breaks anything, I'll be glad to address it.

This revision was not accepted when it landed; it landed in state Needs Review.Apr 4 2022, 6:49 PM
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.