This is an archive of the discontinued LLVM Phabricator instance.

[clang][ASTImport] Add support for import of empty records
ClosedPublic

Authored by kpdev42 on Mar 1 2023, 4:43 AM.

Diff Detail

Event Timeline

kpdev42 created this revision.Mar 1 2023, 4:43 AM
Herald added a project: Restricted Project. · View Herald Transcript
Herald added a subscriber: martong. · View Herald Transcript
kpdev42 requested review of this revision.Mar 1 2023, 4:43 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 1 2023, 4:43 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
balazske added inline comments.Mar 2 2023, 7:45 AM
clang/lib/AST/ASTImporter.cpp
3900

I am not familiar with this use case, is there a path where the attributes are read from a FieldDecl before return from VisitFieldDecl? Probably ImportImpl is overridden? Importing the attributes here should work but not totally sure if it does not cause problems. Problematic case is if the attribute has pointer to a Decl or Type that is imported here in a state when the field is already created but not initialized. Another problem is that attributes are added a second time in Import(Decl *). Can it work if the ImportAttrs is made a protected function and called from (overridden) ImportImpl (still there can be a second import in Import(Decl *)?

clang/unittests/AST/ASTImporterTest.cpp
8161
8164
8165

Does this test fail without the changes applied? And does it not fail after (is the "Empty" value copied at import)?

kpdev42 marked an inline comment as done.Mar 6 2023, 10:58 PM
kpdev42 added inline comments.
clang/lib/AST/ASTImporter.cpp
3900

The problem is that field attributes are required when we are adding a copy of a field to a structure in VisitFieldDecl. Attributes themselves are read in CXXRecordDecl::addedMember (see the call to isZeroSize). Adding them after ImportImpl is called is too late: record is already marked as non-empty.

kpdev42 marked 2 inline comments as done.Mar 6 2023, 11:01 PM
kpdev42 added inline comments.
clang/unittests/AST/ASTImporterTest.cpp
8165

Yes the tests will fail due to above mentioned problem with field attributes

balazske added inline comments.Mar 7 2023, 6:39 AM
clang/lib/AST/ASTImporter.cpp
3900

I understand now. But a comment could be added here to explain why the attribute import is needed here.

8991
balazske added inline comments.Mar 7 2023, 6:41 AM
clang/include/clang/AST/DeclCXX.h
1171

This change looks not related.

kpdev42 updated this revision to Diff 504956.Mar 13 2023, 11:47 PM
kpdev42 marked an inline comment as done.

Address review comments

kpdev42 marked an inline comment as done.Mar 13 2023, 11:50 PM
kpdev42 marked an inline comment as done.Mar 13 2023, 11:54 PM
kpdev42 added inline comments.
clang/include/clang/AST/DeclCXX.h
1171

The markEmpty is still needed and is used in corresponding unit tests. It's gonna be used in DWARFASTParserClang to mark records with empty fields as having zero size. Then by copying attributes in ASTImporter we prevent this empty mark to be overwritten

balazske accepted this revision.Mar 14 2023, 3:33 AM

There are some unresolved comments in the test (cast is not needed at Import and EXPECT_TRUE can be used) but otherwise LGTM.
(But I have a feeling that problems may happen with attributes like GuardedByAttr if these are imported before the field is added to the record. Probably it is better to only import the NoUniqueAddressAttr in VisitFieldDecl.)

This revision is now accepted and ready to land.Mar 14 2023, 3:33 AM
This revision was automatically updated to reflect the committed changes.
kpdev42 marked an inline comment as done.