From 3287ae8f6520ef81570377c1fb4c7147782a13ef Mon Sep 17 00:00:00 2001 From: Qizhi Hu <836744285@qq.com> Date: Wed, 29 Nov 2023 09:33:37 +0800 Subject: [PATCH] [clang][ASTImporter] IdentifierInfo of Attribute should be set using 'ToASTContext' (#73290) Co-authored-by: huqizhi <836744285@qq.com> --- clang/include/clang/Basic/AttributeCommonInfo.h | 1 + clang/lib/AST/ASTImporter.cpp | 1 + clang/unittests/AST/ASTImporterTest.cpp | 13 +++++++++++-- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/clang/include/clang/Basic/AttributeCommonInfo.h b/clang/include/clang/Basic/AttributeCommonInfo.h index 3140d1a838afce..018b92fdc11f55 100644 --- a/clang/include/clang/Basic/AttributeCommonInfo.h +++ b/clang/include/clang/Basic/AttributeCommonInfo.h @@ -177,6 +177,7 @@ class AttributeCommonInfo { IsRegularKeywordAttribute); } const IdentifierInfo *getAttrName() const { return AttrName; } + void setAttrName(const IdentifierInfo *AttrNameII) { AttrName = AttrNameII; } SourceLocation getLoc() const { return AttrRange.getBegin(); } SourceRange getRange() const { return AttrRange; } void setRange(SourceRange R) { AttrRange = R; } diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp index 3dc33c10af11ed..f1f335118f37a4 100644 --- a/clang/lib/AST/ASTImporter.cpp +++ b/clang/lib/AST/ASTImporter.cpp @@ -9073,6 +9073,7 @@ class AttrImporter { ToAttr = FromAttr->clone(Importer.getToContext()); ToAttr->setRange(ToRange); + ToAttr->setAttrName(Importer.Import(FromAttr->getAttrName())); } // Get the result of the previous import attempt (can be used only once). diff --git a/clang/unittests/AST/ASTImporterTest.cpp b/clang/unittests/AST/ASTImporterTest.cpp index 902b740a5106c0..4dd7510bf8ddf8 100644 --- a/clang/unittests/AST/ASTImporterTest.cpp +++ b/clang/unittests/AST/ASTImporterTest.cpp @@ -12,6 +12,7 @@ #include "clang/AST/RecordLayout.h" #include "clang/ASTMatchers/ASTMatchers.h" +#include "clang/Testing/CommandLineArgs.h" #include "llvm/Support/SmallVectorMemoryBuffer.h" #include "clang/AST/DeclContextInternals.h" @@ -7379,11 +7380,12 @@ struct ImportAttributes : public ASTImporterOptionSpecificTestBase { } template - void importAttr(const char *Code, AT *&FromAttr, AT *&ToAttr) { + void importAttr(const char *Code, AT *&FromAttr, AT *&ToAttr, + TestLanguage Lang = Lang_CXX11) { static_assert(std::is_base_of::value, "AT should be an Attr"); static_assert(std::is_base_of::value, "DT should be a Decl"); - Decl *FromTU = getTuDecl(Code, Lang_CXX11, "input.cc"); + Decl *FromTU = getTuDecl(Code, Lang, "input.cc"); DT *FromD = FirstDeclMatcher
().match(FromTU, namedDecl(hasName("test"))); ASSERT_TRUE(FromD); @@ -7669,6 +7671,13 @@ TEST_P(ImportAttributes, ImportLocksExcluded) { checkImportVariadicArg(FromAttr->args(), ToAttr->args()); } +TEST_P(ImportAttributes, ImportC99NoThrowAttr) { + NoThrowAttr *FromAttr, *ToAttr; + importAttr("void test () __attribute__ ((__nothrow__));", + FromAttr, ToAttr, Lang_C99); + checkImported(FromAttr->getAttrName(), ToAttr->getAttrName()); +} + template auto ExtendWithOptions(const T &Values, const std::vector &Args) { auto Copy = Values;