Skip to content

[clang][ASTImporter] IdentifierInfo of Attribute should be set using 'ToASTContext' #73290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 29, 2023

Conversation

jcsxky
Copy link
Contributor

@jcsxky jcsxky commented Nov 24, 2023

No description provided.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Nov 24, 2023
@llvmbot
Copy link
Member

llvmbot commented Nov 24, 2023

@llvm/pr-subscribers-clang

Author: Qizhi Hu (jcsxky)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/73290.diff

3 Files Affected:

  • (modified) clang/include/clang/Basic/AttributeCommonInfo.h (+1)
  • (modified) clang/lib/AST/ASTImporter.cpp (+5-2)
  • (modified) clang/unittests/AST/ASTImporterTest.cpp (+11-2)
diff --git a/clang/include/clang/Basic/AttributeCommonInfo.h b/clang/include/clang/Basic/AttributeCommonInfo.h
index 3140d1a838afcec..f84f51b1cd25022 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 *A) { AttrName = A; }
   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 c4e931e220f69b5..19e3e94c70c82d5 100644
--- a/clang/lib/AST/ASTImporter.cpp
+++ b/clang/lib/AST/ASTImporter.cpp
@@ -9241,9 +9241,12 @@ Error ASTImporter::ImportAttrs(Decl *ToD, Decl *FromD) {
     return Error::success();
   for (const Attr *FromAttr : FromD->getAttrs()) {
     auto ToAttrOrErr = Import(FromAttr);
-    if (ToAttrOrErr)
+    if (ToAttrOrErr) {
+      if (auto *FromAttrNameII = FromAttr->getAttrName())
+        (*ToAttrOrErr)
+            ->setAttrName(&ToContext.Idents.get(FromAttrNameII->getName()));
       ToD->addAttr(*ToAttrOrErr);
-    else
+    } else
       return ToAttrOrErr.takeError();
   }
   return Error::success();
diff --git a/clang/unittests/AST/ASTImporterTest.cpp b/clang/unittests/AST/ASTImporterTest.cpp
index 5f4d8d040772cb1..2df92ad9d5985a4 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"
@@ -7362,11 +7363,12 @@ struct ImportAttributes : public ASTImporterOptionSpecificTestBase {
   }
 
   template <class DT, class AT>
-  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<Attr, AT>::value, "AT should be an Attr");
     static_assert(std::is_base_of<Decl, DT>::value, "DT should be a Decl");
 
-    Decl *FromTU = getTuDecl(Code, Lang_CXX11, "input.cc");
+    Decl *FromTU = getTuDecl(Code, Lang, "input.cc");
     DT *FromD =
         FirstDeclMatcher<DT>().match(FromTU, namedDecl(hasName("test")));
     ASSERT_TRUE(FromD);
@@ -7652,6 +7654,13 @@ TEST_P(ImportAttributes, ImportLocksExcluded) {
   checkImportVariadicArg(FromAttr->args(), ToAttr->args());
 }
 
+TEST_P(ImportAttributes, ImportC99NoThrowAttr) {
+  NoThrowAttr *FromAttr, *ToAttr;
+  importAttr<FunctionDecl>("void test () __attribute__ ((__nothrow__));",
+                           FromAttr, ToAttr, Lang_C99);
+  checkImported(FromAttr->getAttrName(), ToAttr->getAttrName());
+}
+
 template <typename T>
 auto ExtendWithOptions(const T &Values, const std::vector<std::string> &Args) {
   auto Copy = Values;

@jcsxky jcsxky force-pushed the set-identifierinfo-of-attribute branch from cc88383 to 150993c Compare November 24, 2023 06:44
@jcsxky jcsxky requested a review from balazske November 24, 2023 06:47
@jcsxky jcsxky force-pushed the set-identifierinfo-of-attribute branch 2 times, most recently from 52b972f to a29523e Compare November 24, 2023 06:53
Copy link
Collaborator

@balazske balazske left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is better to add the import of AttrName to the attribute import code (function Import(const Attr *FromAttr) and what is called from it). Probably it works to add it to AttrImporter::cloneAttr and do it like const IdentifierInfo *ToAttrName = Importer.Import(FromAttr->getAttrName());.

@jcsxky jcsxky force-pushed the set-identifierinfo-of-attribute branch from a29523e to 20aab60 Compare November 24, 2023 08:56
@jcsxky
Copy link
Contributor Author

jcsxky commented Nov 24, 2023

I think it is better to add the import of AttrName to the attribute import code (function Import(const Attr *FromAttr) and what is called from it). Probably it works to add it to AttrImporter::cloneAttr and do it like const IdentifierInfo *ToAttrName = Importer.Import(FromAttr->getAttrName());.

Fixed according to your guidance.

@jcsxky jcsxky requested a review from balazske November 24, 2023 08:59
@jcsxky jcsxky merged commit 3287ae8 into llvm:main Nov 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants