[clang] 0d903b6 - [clang][ASTImporter] Import AlignValueAttr correctly. (#75308)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Dec 22 01:07:43 PST 2023
Author: Balázs Kéri
Date: 2023-12-22T10:07:38+01:00
New Revision: 0d903b689ab984d6e7d8e1919a5b37658ae94518
URL: https://github.com/llvm/llvm-project/commit/0d903b689ab984d6e7d8e1919a5b37658ae94518
DIFF: https://github.com/llvm/llvm-project/commit/0d903b689ab984d6e7d8e1919a5b37658ae94518.diff
LOG: [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.
Added:
Modified:
clang/lib/AST/ASTImporter.cpp
clang/unittests/AST/ASTImporterTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp
index 1cc47de675bf33..88b8c6abb6d5fd 100644
--- a/clang/lib/AST/ASTImporter.cpp
+++ b/clang/lib/AST/ASTImporter.cpp
@@ -9103,6 +9103,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 4c06152d3eb563..9fa7660cde6593 100644
--- a/clang/unittests/AST/ASTImporterTest.cpp
+++ b/clang/unittests/AST/ASTImporterTest.cpp
@@ -7445,67 +7445,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').
+ // struct 'test').
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) {
More information about the cfe-commits
mailing list