[clang] [clang][ASTImporter] Import AlignValueAttr correctly. (PR #75308)
Balázs Kéri via cfe-commits
cfe-commits at lists.llvm.org
Thu Dec 21 05:42:35 PST 2023
https://github.com/balazske updated https://github.com/llvm/llvm-project/pull/75308
>From 2e6fe315bdebea705d84b4152a831e5934b659eb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bal=C3=A1zs=20K=C3=A9ri?= <balazs.keri at ericsson.com>
Date: Wed, 13 Dec 2023 10:23:48 +0100
Subject: [PATCH 1/2] [clang][ASTImporter] Import AlignValueAttr correctly.
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.
---
clang/lib/AST/ASTImporter.cpp | 6 +++
clang/unittests/AST/ASTImporterTest.cpp | 69 +++++++++----------------
2 files changed, 30 insertions(+), 45 deletions(-)
diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp
index f1f335118f37a4..cc29f4356ad755 100644
--- a/clang/lib/AST/ASTImporter.cpp
+++ b/clang/lib/AST/ASTImporter.cpp
@@ -9101,6 +9101,12 @@ Expected<Attr *> ASTImporter::Import(const Attr *FromAttr) {
break;
}
+ case attr::AlignValue: {
+ auto *From = cast<AlignValueAttr>(FromAttr);
+ AI.importAttr(From, AI.importArg(From->getAlignment()).value());
+ break;
+ }
+
case attr::Format: {
const auto *From = cast<FormatAttr>(FromAttr);
AI.importAttr(From, Import(From->getType()), From->getFormatIdx(),
diff --git a/clang/unittests/AST/ASTImporterTest.cpp b/clang/unittests/AST/ASTImporterTest.cpp
index 4dd7510bf8ddf8..da47e6b6653095 100644
--- a/clang/unittests/AST/ASTImporterTest.cpp
+++ b/clang/unittests/AST/ASTImporterTest.cpp
@@ -7425,67 +7425,46 @@ void ImportAttributes::checkImported<Decl>(const Decl *From, const Decl *To) {
ToAST->getASTContext().getTranslationUnitDecl());
}
-// FIXME: Use ImportAttributes for this test.
-TEST_P(ASTImporterOptionSpecificTestBase, ImportExprOfAlignmentAttr) {
- // Test if import of these packed and aligned attributes does not trigger an
- // error situation where source location from 'From' context is referenced in
- // 'To' context through evaluation of the alignof attribute.
- // This happens if the 'alignof(A)' expression is not imported correctly.
- Decl *FromTU = getTuDecl(
+TEST_P(ImportAttributes, ImportAligned) {
+ AlignedAttr *FromAttr, *ToAttr;
+ importAttr<RecordDecl>(
R"(
struct __attribute__((packed)) A { int __attribute__((aligned(8))) X; };
- struct alignas(alignof(A)) S {};
+ struct alignas(alignof(A)) test {};
)",
- Lang_CXX11, "input.cc");
- auto *FromD = FirstDeclMatcher<CXXRecordDecl>().match(
- FromTU, cxxRecordDecl(hasName("S"), unless(isImplicit())));
- ASSERT_TRUE(FromD);
-
- auto *ToD = Import(FromD, Lang_CXX11);
- ASSERT_TRUE(ToD);
-
- auto *FromAttr = FromD->getAttr<AlignedAttr>();
- auto *ToAttr = ToD->getAttr<AlignedAttr>();
- EXPECT_EQ(FromAttr->isInherited(), ToAttr->isInherited());
- EXPECT_EQ(FromAttr->isPackExpansion(), ToAttr->isPackExpansion());
- EXPECT_EQ(FromAttr->isImplicit(), ToAttr->isImplicit());
- EXPECT_EQ(FromAttr->getSyntax(), ToAttr->getSyntax());
- EXPECT_EQ(FromAttr->getSemanticSpelling(), ToAttr->getSemanticSpelling());
- EXPECT_TRUE(ToAttr->getAlignmentExpr());
+ FromAttr, ToAttr);
+ checkImported(FromAttr->getAlignmentExpr(), ToAttr->getAlignmentExpr());
auto *ToA = FirstDeclMatcher<CXXRecordDecl>().match(
- ToD->getTranslationUnitDecl(),
+ ToAST->getASTContext().getTranslationUnitDecl(),
cxxRecordDecl(hasName("A"), unless(isImplicit())));
// Ensure that 'struct A' was imported (through reference from attribute of
// 'S').
EXPECT_TRUE(ToA);
}
-// FIXME: Use ImportAttributes for this test.
-TEST_P(ASTImporterOptionSpecificTestBase, ImportFormatAttr) {
- Decl *FromTU = getTuDecl(
+TEST_P(ImportAttributes, ImportAlignValue) {
+ AlignValueAttr *FromAttr, *ToAttr;
+ importAttr<VarDecl>(
+ R"(
+ void *test __attribute__((align_value(64)));
+ )",
+ FromAttr, ToAttr);
+ checkImported(FromAttr->getAlignment(), ToAttr->getAlignment());
+}
+
+TEST_P(ImportAttributes, ImportFormat) {
+ FormatAttr *FromAttr, *ToAttr;
+ importAttr<FunctionDecl>(
R"(
- int foo(const char * fmt, ...)
+ int test(const char * fmt, ...)
__attribute__ ((__format__ (__scanf__, 1, 2)));
)",
- Lang_CXX03, "input.cc");
- auto *FromD = FirstDeclMatcher<FunctionDecl>().match(
- FromTU, functionDecl(hasName("foo")));
- ASSERT_TRUE(FromD);
+ FromAttr, ToAttr);
- auto *ToD = Import(FromD, Lang_CXX03);
- ASSERT_TRUE(ToD);
- ToD->dump(); // Should not crash!
-
- auto *FromAttr = FromD->getAttr<FormatAttr>();
- auto *ToAttr = ToD->getAttr<FormatAttr>();
- EXPECT_EQ(FromAttr->isInherited(), ToAttr->isInherited());
- EXPECT_EQ(FromAttr->isPackExpansion(), ToAttr->isPackExpansion());
- EXPECT_EQ(FromAttr->isImplicit(), ToAttr->isImplicit());
- EXPECT_EQ(FromAttr->getSyntax(), ToAttr->getSyntax());
- EXPECT_EQ(FromAttr->getAttributeSpellingListIndex(),
- ToAttr->getAttributeSpellingListIndex());
EXPECT_EQ(FromAttr->getType()->getName(), ToAttr->getType()->getName());
+ EXPECT_EQ(FromAttr->getFirstArg(), ToAttr->getFirstArg());
+ EXPECT_EQ(FromAttr->getFormatIdx(), ToAttr->getFormatIdx());
}
TEST_P(ImportAttributes, ImportEnableIf) {
>From 616a3e83334ab61487ec079bc264637b1c76b660 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bal=C3=A1zs=20K=C3=A9ri?= <balazs.keri at ericsson.com>
Date: Thu, 21 Dec 2023 14:42:28 +0100
Subject: [PATCH 2/2] changed struct name in comment
Co-authored-by: DonatNagyE <donat.nagy at ericsson.com>
---
clang/unittests/AST/ASTImporterTest.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/unittests/AST/ASTImporterTest.cpp b/clang/unittests/AST/ASTImporterTest.cpp
index da47e6b6653095..bcad34d4572c93 100644
--- a/clang/unittests/AST/ASTImporterTest.cpp
+++ b/clang/unittests/AST/ASTImporterTest.cpp
@@ -7439,7 +7439,7 @@ TEST_P(ImportAttributes, ImportAligned) {
ToAST->getASTContext().getTranslationUnitDecl(),
cxxRecordDecl(hasName("A"), unless(isImplicit())));
// Ensure that 'struct A' was imported (through reference from attribute of
- // 'S').
+ // struct 'test').
EXPECT_TRUE(ToA);
}
More information about the cfe-commits
mailing list