Skip to content

Commit 0d903b6

Browse files
authored
[clang][ASTImporter] Import AlignValueAttr correctly. (#75308)
Expression of attribute `align_value` was not imported. Import of the attribute is corrected, a test for it is added, other related tests with FIXME are updated. Fixes #75054.
1 parent 17858ce commit 0d903b6

File tree

2 files changed

+31
-46
lines changed

2 files changed

+31
-46
lines changed

clang/lib/AST/ASTImporter.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9103,6 +9103,12 @@ Expected<Attr *> ASTImporter::Import(const Attr *FromAttr) {
91039103
break;
91049104
}
91059105

9106+
case attr::AlignValue: {
9107+
auto *From = cast<AlignValueAttr>(FromAttr);
9108+
AI.importAttr(From, AI.importArg(From->getAlignment()).value());
9109+
break;
9110+
}
9111+
91069112
case attr::Format: {
91079113
const auto *From = cast<FormatAttr>(FromAttr);
91089114
AI.importAttr(From, Import(From->getType()), From->getFormatIdx(),

clang/unittests/AST/ASTImporterTest.cpp

Lines changed: 25 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7445,67 +7445,46 @@ void ImportAttributes::checkImported<Decl>(const Decl *From, const Decl *To) {
74457445
ToAST->getASTContext().getTranslationUnitDecl());
74467446
}
74477447

7448-
// FIXME: Use ImportAttributes for this test.
7449-
TEST_P(ASTImporterOptionSpecificTestBase, ImportExprOfAlignmentAttr) {
7450-
// Test if import of these packed and aligned attributes does not trigger an
7451-
// error situation where source location from 'From' context is referenced in
7452-
// 'To' context through evaluation of the alignof attribute.
7453-
// This happens if the 'alignof(A)' expression is not imported correctly.
7454-
Decl *FromTU = getTuDecl(
7448+
TEST_P(ImportAttributes, ImportAligned) {
7449+
AlignedAttr *FromAttr, *ToAttr;
7450+
importAttr<RecordDecl>(
74557451
R"(
74567452
struct __attribute__((packed)) A { int __attribute__((aligned(8))) X; };
7457-
struct alignas(alignof(A)) S {};
7453+
struct alignas(alignof(A)) test {};
74587454
)",
7459-
Lang_CXX11, "input.cc");
7460-
auto *FromD = FirstDeclMatcher<CXXRecordDecl>().match(
7461-
FromTU, cxxRecordDecl(hasName("S"), unless(isImplicit())));
7462-
ASSERT_TRUE(FromD);
7463-
7464-
auto *ToD = Import(FromD, Lang_CXX11);
7465-
ASSERT_TRUE(ToD);
7466-
7467-
auto *FromAttr = FromD->getAttr<AlignedAttr>();
7468-
auto *ToAttr = ToD->getAttr<AlignedAttr>();
7469-
EXPECT_EQ(FromAttr->isInherited(), ToAttr->isInherited());
7470-
EXPECT_EQ(FromAttr->isPackExpansion(), ToAttr->isPackExpansion());
7471-
EXPECT_EQ(FromAttr->isImplicit(), ToAttr->isImplicit());
7472-
EXPECT_EQ(FromAttr->getSyntax(), ToAttr->getSyntax());
7473-
EXPECT_EQ(FromAttr->getSemanticSpelling(), ToAttr->getSemanticSpelling());
7474-
EXPECT_TRUE(ToAttr->getAlignmentExpr());
7455+
FromAttr, ToAttr);
7456+
checkImported(FromAttr->getAlignmentExpr(), ToAttr->getAlignmentExpr());
74757457

74767458
auto *ToA = FirstDeclMatcher<CXXRecordDecl>().match(
7477-
ToD->getTranslationUnitDecl(),
7459+
ToAST->getASTContext().getTranslationUnitDecl(),
74787460
cxxRecordDecl(hasName("A"), unless(isImplicit())));
74797461
// Ensure that 'struct A' was imported (through reference from attribute of
7480-
// 'S').
7462+
// struct 'test').
74817463
EXPECT_TRUE(ToA);
74827464
}
74837465

7484-
// FIXME: Use ImportAttributes for this test.
7485-
TEST_P(ASTImporterOptionSpecificTestBase, ImportFormatAttr) {
7486-
Decl *FromTU = getTuDecl(
7466+
TEST_P(ImportAttributes, ImportAlignValue) {
7467+
AlignValueAttr *FromAttr, *ToAttr;
7468+
importAttr<VarDecl>(
7469+
R"(
7470+
void *test __attribute__((align_value(64)));
7471+
)",
7472+
FromAttr, ToAttr);
7473+
checkImported(FromAttr->getAlignment(), ToAttr->getAlignment());
7474+
}
7475+
7476+
TEST_P(ImportAttributes, ImportFormat) {
7477+
FormatAttr *FromAttr, *ToAttr;
7478+
importAttr<FunctionDecl>(
74877479
R"(
7488-
int foo(const char * fmt, ...)
7480+
int test(const char * fmt, ...)
74897481
__attribute__ ((__format__ (__scanf__, 1, 2)));
74907482
)",
7491-
Lang_CXX03, "input.cc");
7492-
auto *FromD = FirstDeclMatcher<FunctionDecl>().match(
7493-
FromTU, functionDecl(hasName("foo")));
7494-
ASSERT_TRUE(FromD);
7483+
FromAttr, ToAttr);
74957484

7496-
auto *ToD = Import(FromD, Lang_CXX03);
7497-
ASSERT_TRUE(ToD);
7498-
ToD->dump(); // Should not crash!
7499-
7500-
auto *FromAttr = FromD->getAttr<FormatAttr>();
7501-
auto *ToAttr = ToD->getAttr<FormatAttr>();
7502-
EXPECT_EQ(FromAttr->isInherited(), ToAttr->isInherited());
7503-
EXPECT_EQ(FromAttr->isPackExpansion(), ToAttr->isPackExpansion());
7504-
EXPECT_EQ(FromAttr->isImplicit(), ToAttr->isImplicit());
7505-
EXPECT_EQ(FromAttr->getSyntax(), ToAttr->getSyntax());
7506-
EXPECT_EQ(FromAttr->getAttributeSpellingListIndex(),
7507-
ToAttr->getAttributeSpellingListIndex());
75087485
EXPECT_EQ(FromAttr->getType()->getName(), ToAttr->getType()->getName());
7486+
EXPECT_EQ(FromAttr->getFirstArg(), ToAttr->getFirstArg());
7487+
EXPECT_EQ(FromAttr->getFormatIdx(), ToAttr->getFormatIdx());
75097488
}
75107489

75117490
TEST_P(ImportAttributes, ImportEnableIf) {

0 commit comments

Comments
 (0)