[clang] 7a527f1 - Teach AttachPreviousImpl to inherit MSInheritanceAttr attribute

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Fri Aug 21 09:06:00 PDT 2020


Author: Vaibhav Garg
Date: 2020-08-21T12:04:43-04:00
New Revision: 7a527f17776be78ec44b88e82b39afb65fc148e4

URL: https://github.com/llvm/llvm-project/commit/7a527f17776be78ec44b88e82b39afb65fc148e4
DIFF: https://github.com/llvm/llvm-project/commit/7a527f17776be78ec44b88e82b39afb65fc148e4.diff

LOG: Teach AttachPreviousImpl to inherit MSInheritanceAttr attribute

This commit teaches ASTDeclReader::attachPreviousDecl to successfully merge
two Decl's when one contains an inheritable attribute like the
MSInheritanceAttr. Usually, attributes that are needed to be present along the
redeclaration chain are attached during ASTReading from
ASTDeclReader::attachPreviousDecl, but no such thing is done for inheritable
attributes. Currently, only the logic for merging MSInheritanceAttr is
provided.

Added: 
    clang/test/Modules/Inputs/inherit-attribute/a.h
    clang/test/Modules/Inputs/inherit-attribute/b.h
    clang/test/Modules/Inputs/inherit-attribute/c.h
    clang/test/Modules/Inputs/inherit-attribute/module.modulemap
    clang/test/Modules/inherit-attribute.cpp

Modified: 
    clang/lib/Serialization/ASTReaderDecl.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Serialization/ASTReaderDecl.cpp b/clang/lib/Serialization/ASTReaderDecl.cpp
index d76182847e9d..c7a009d1e50d 100644
--- a/clang/lib/Serialization/ASTReaderDecl.cpp
+++ b/clang/lib/Serialization/ASTReaderDecl.cpp
@@ -281,6 +281,9 @@ namespace clang {
     static Decl *getMostRecentDeclImpl(...);
     static Decl *getMostRecentDecl(Decl *D);
 
+    static void mergeInheritableAttributes(ASTReader &Reader, Decl *D,
+                                           Decl *Previous);
+
     template <typename DeclT>
     static void attachPreviousDeclImpl(ASTReader &Reader,
                                        Redeclarable<DeclT> *D, Decl *Previous,
@@ -3511,6 +3514,19 @@ Decl *ASTReader::getMostRecentExistingDecl(Decl *D) {
   return ASTDeclReader::getMostRecentDecl(D->getCanonicalDecl());
 }
 
+void ASTDeclReader::mergeInheritableAttributes(ASTReader &Reader, Decl *D,
+                                               Decl *Previous) {
+  InheritableAttr *NewAttr = nullptr;
+  ASTContext &Context = Reader.getContext();
+  const auto *IA = Previous->getAttr<MSInheritanceAttr>();
+
+  if (IA && !D->hasAttr<MSInheritanceAttr>()) {
+    NewAttr = cast<InheritableAttr>(IA->clone(Context));
+    NewAttr->setInherited(true);
+    D->addAttr(NewAttr);
+  }
+}
+
 template<typename DeclT>
 void ASTDeclReader::attachPreviousDeclImpl(ASTReader &Reader,
                                            Redeclarable<DeclT> *D,
@@ -3669,6 +3685,12 @@ void ASTDeclReader::attachPreviousDecl(ASTReader &Reader, Decl *D,
   if (auto *TD = dyn_cast<TemplateDecl>(D))
     inheritDefaultTemplateArguments(Reader.getContext(),
                                     cast<TemplateDecl>(Previous), TD);
+
+  // If any of the declaration in the chain contains an Inheritable attribute,
+  // it needs to be added to all the declarations in the redeclarable chain.
+  // FIXME: Only the logic of merging MSInheritableAttr is present, it should
+  // be extended for all inheritable attributes.
+  mergeInheritableAttributes(Reader, D, Previous);
 }
 
 template<typename DeclT>

diff  --git a/clang/test/Modules/Inputs/inherit-attribute/a.h b/clang/test/Modules/Inputs/inherit-attribute/a.h
new file mode 100644
index 000000000000..04aabe9e8c61
--- /dev/null
+++ b/clang/test/Modules/Inputs/inherit-attribute/a.h
@@ -0,0 +1,10 @@
+#ifndef FOO
+#define FOO
+
+class Foo {
+public:
+  void step(int v);
+  Foo();
+};
+
+#endif

diff  --git a/clang/test/Modules/Inputs/inherit-attribute/b.h b/clang/test/Modules/Inputs/inherit-attribute/b.h
new file mode 100644
index 000000000000..9cd1f55b9d33
--- /dev/null
+++ b/clang/test/Modules/Inputs/inherit-attribute/b.h
@@ -0,0 +1,5 @@
+#include "a.h"
+
+void bar() {
+  &Foo::step;
+}

diff  --git a/clang/test/Modules/Inputs/inherit-attribute/c.h b/clang/test/Modules/Inputs/inherit-attribute/c.h
new file mode 100644
index 000000000000..2243de1baf9a
--- /dev/null
+++ b/clang/test/Modules/Inputs/inherit-attribute/c.h
@@ -0,0 +1 @@
+#include "a.h"

diff  --git a/clang/test/Modules/Inputs/inherit-attribute/module.modulemap b/clang/test/Modules/Inputs/inherit-attribute/module.modulemap
new file mode 100644
index 000000000000..16481e2745dd
--- /dev/null
+++ b/clang/test/Modules/Inputs/inherit-attribute/module.modulemap
@@ -0,0 +1,3 @@
+module "b" { header "b.h" }
+
+module "c" { header "c.h" }

diff  --git a/clang/test/Modules/inherit-attribute.cpp b/clang/test/Modules/inherit-attribute.cpp
new file mode 100644
index 000000000000..34aa4670365a
--- /dev/null
+++ b/clang/test/Modules/inherit-attribute.cpp
@@ -0,0 +1,20 @@
+// RUN: rm -rf %t
+// RUN: %clang_cc1 -fmodules -triple x86_64-pc-windows-msvc-unknown -I%S\Inputs\inherit-attribute -fmodules-cache-path=%t \
+// RUN: -fimplicit-module-maps -fmodules-local-submodule-visibility %s -ast-dump-all \
+// RUN: | FileCheck %s
+
+#include "b.h"
+#include "c.h"
+
+class Foo;
+
+Foo f;
+
+// CHECK:   CXXRecordDecl {{.*}} imported in b {{.*}} Foo
+// CHECK:   MSInheritanceAttr {{[^()]*$}}
+
+// CHECK:   CXXRecordDecl {{.*}} prev {{.*}} imported in c {{.*}} Foo
+// CHECK:   MSInheritanceAttr {{.*}} Inherited {{[^()]*$}}
+
+// CHECK:   CXXRecordDecl {{.*}} <line:9:1, col:7> col:7 referenced class Foo
+// CHECK:   MSInheritanceAttr {{.*}} Inherited {{[^()]*$}}


        


More information about the cfe-commits mailing list