[clang] 626c789 - Fix compile error in UnresolvedSetTest.cpp, hopefully the last one

John Brawn via cfe-commits cfe-commits at lists.llvm.org
Thu Jul 6 03:55:19 PDT 2023


Author: John Brawn
Date: 2023-07-06T11:54:45+01:00
New Revision: 626c789d92bc917e7db30478e9b6d4c9b567ee17

URL: https://github.com/llvm/llvm-project/commit/626c789d92bc917e7db30478e9b6d4c9b567ee17
DIFF: https://github.com/llvm/llvm-project/commit/626c789d92bc917e7db30478e9b6d4c9b567ee17.diff

LOG: Fix compile error in UnresolvedSetTest.cpp, hopefully the last one

This test is failing to compile when LLVM_ENABLE_MODULES=ON due to
NamedDecl being multiply defined. Fix this by avoiding declaring our
own NamedDecl in the test and instead cast a struct of appropriate
size and alignment to NamedDecl.

Added: 
    

Modified: 
    clang/unittests/AST/UnresolvedSetTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/unittests/AST/UnresolvedSetTest.cpp b/clang/unittests/AST/UnresolvedSetTest.cpp
index ada857e0e38260..0d01eebe07e2f5 100644
--- a/clang/unittests/AST/UnresolvedSetTest.cpp
+++ b/clang/unittests/AST/UnresolvedSetTest.cpp
@@ -1,56 +1,56 @@
 #include "clang/AST/UnresolvedSet.h"
+#include "clang/AST/Decl.h"
 #include "gtest/gtest.h"
 
-namespace clang {
-class NamedDecl {
-  // DeclAccessPair assumes that NamedDecl is at least 4-byte aligned, so we
-  // we need to have a dummy value to make this dummy NamedDecl also be aligned.
-  [[maybe_unused]] int dummy;
-
-public:
-  NamedDecl() {}
-};
-} // namespace clang
-
 using namespace clang;
 
 class UnresolvedSetTest : public ::testing::Test {
 protected:
-  NamedDecl n0, n1, n2, n3;
+  // For this test we don't care about the contents of the NamedDecl,
+  // so just use a dummy struct of appropriate size and alignment.
+  struct alignas(NamedDecl) DummyDecl {
+    char contents[sizeof(NamedDecl)];
+  };
+  DummyDecl d0, d1, d2, d3;
+  NamedDecl *n0, *n1, *n2, *n3;
   UnresolvedSet<2> set;
 
   void SetUp() override {
-    set.addDecl(&n0);
-    set.addDecl(&n1);
-    set.addDecl(&n2);
-    set.addDecl(&n3);
+    n0 = reinterpret_cast<NamedDecl*>(&d0);
+    n1 = reinterpret_cast<NamedDecl*>(&d1);
+    n2 = reinterpret_cast<NamedDecl*>(&d2);
+    n3 = reinterpret_cast<NamedDecl*>(&d3);
+    set.addDecl(n0);
+    set.addDecl(n1);
+    set.addDecl(n2);
+    set.addDecl(n3);
   }
 };
 
 TEST_F(UnresolvedSetTest, Size) { EXPECT_EQ(set.size(), 4u); }
 
 TEST_F(UnresolvedSetTest, ArrayOperator) {
-  EXPECT_EQ(set[0].getDecl(), &n0);
-  EXPECT_EQ(set[1].getDecl(), &n1);
-  EXPECT_EQ(set[2].getDecl(), &n2);
-  EXPECT_EQ(set[3].getDecl(), &n3);
+  EXPECT_EQ(set[0].getDecl(), n0);
+  EXPECT_EQ(set[1].getDecl(), n1);
+  EXPECT_EQ(set[2].getDecl(), n2);
+  EXPECT_EQ(set[3].getDecl(), n3);
 }
 
 TEST_F(UnresolvedSetTest, EraseIntegerFromStart) {
   set.erase(0);
   EXPECT_EQ(set.size(), 3u);
-  EXPECT_EQ(set[0].getDecl(), &n3);
-  EXPECT_EQ(set[1].getDecl(), &n1);
-  EXPECT_EQ(set[2].getDecl(), &n2);
+  EXPECT_EQ(set[0].getDecl(), n3);
+  EXPECT_EQ(set[1].getDecl(), n1);
+  EXPECT_EQ(set[2].getDecl(), n2);
 
   set.erase(0);
   EXPECT_EQ(set.size(), 2u);
-  EXPECT_EQ(set[0].getDecl(), &n2);
-  EXPECT_EQ(set[1].getDecl(), &n1);
+  EXPECT_EQ(set[0].getDecl(), n2);
+  EXPECT_EQ(set[1].getDecl(), n1);
 
   set.erase(0);
   EXPECT_EQ(set.size(), 1u);
-  EXPECT_EQ(set[0].getDecl(), &n1);
+  EXPECT_EQ(set[0].getDecl(), n1);
 
   set.erase(0);
   EXPECT_EQ(set.size(), 0u);
@@ -59,18 +59,18 @@ TEST_F(UnresolvedSetTest, EraseIntegerFromStart) {
 TEST_F(UnresolvedSetTest, EraseIntegerFromEnd) {
   set.erase(3);
   EXPECT_EQ(set.size(), 3u);
-  EXPECT_EQ(set[0].getDecl(), &n0);
-  EXPECT_EQ(set[1].getDecl(), &n1);
-  EXPECT_EQ(set[2].getDecl(), &n2);
+  EXPECT_EQ(set[0].getDecl(), n0);
+  EXPECT_EQ(set[1].getDecl(), n1);
+  EXPECT_EQ(set[2].getDecl(), n2);
 
   set.erase(2);
   EXPECT_EQ(set.size(), 2u);
-  EXPECT_EQ(set[0].getDecl(), &n0);
-  EXPECT_EQ(set[1].getDecl(), &n1);
+  EXPECT_EQ(set[0].getDecl(), n0);
+  EXPECT_EQ(set[1].getDecl(), n1);
 
   set.erase(1);
   EXPECT_EQ(set.size(), 1u);
-  EXPECT_EQ(set[0].getDecl(), &n0);
+  EXPECT_EQ(set[0].getDecl(), n0);
 
   set.erase(0);
   EXPECT_EQ(set.size(), 0u);
@@ -79,18 +79,18 @@ TEST_F(UnresolvedSetTest, EraseIntegerFromEnd) {
 TEST_F(UnresolvedSetTest, EraseIteratorFromStart) {
   set.erase(set.begin());
   EXPECT_EQ(set.size(), 3u);
-  EXPECT_EQ(set[0].getDecl(), &n3);
-  EXPECT_EQ(set[1].getDecl(), &n1);
-  EXPECT_EQ(set[2].getDecl(), &n2);
+  EXPECT_EQ(set[0].getDecl(), n3);
+  EXPECT_EQ(set[1].getDecl(), n1);
+  EXPECT_EQ(set[2].getDecl(), n2);
 
   set.erase(set.begin());
   EXPECT_EQ(set.size(), 2u);
-  EXPECT_EQ(set[0].getDecl(), &n2);
-  EXPECT_EQ(set[1].getDecl(), &n1);
+  EXPECT_EQ(set[0].getDecl(), n2);
+  EXPECT_EQ(set[1].getDecl(), n1);
 
   set.erase(set.begin());
   EXPECT_EQ(set.size(), 1u);
-  EXPECT_EQ(set[0].getDecl(), &n1);
+  EXPECT_EQ(set[0].getDecl(), n1);
 
   set.erase(set.begin());
   EXPECT_EQ(set.size(), 0u);
@@ -99,18 +99,18 @@ TEST_F(UnresolvedSetTest, EraseIteratorFromStart) {
 TEST_F(UnresolvedSetTest, EraseIteratorFromEnd) {
   set.erase(--set.end());
   EXPECT_EQ(set.size(), 3u);
-  EXPECT_EQ(set[0].getDecl(), &n0);
-  EXPECT_EQ(set[1].getDecl(), &n1);
-  EXPECT_EQ(set[2].getDecl(), &n2);
+  EXPECT_EQ(set[0].getDecl(), n0);
+  EXPECT_EQ(set[1].getDecl(), n1);
+  EXPECT_EQ(set[2].getDecl(), n2);
 
   set.erase(--set.end());
   EXPECT_EQ(set.size(), 2u);
-  EXPECT_EQ(set[0].getDecl(), &n0);
-  EXPECT_EQ(set[1].getDecl(), &n1);
+  EXPECT_EQ(set[0].getDecl(), n0);
+  EXPECT_EQ(set[1].getDecl(), n1);
 
   set.erase(--set.end());
   EXPECT_EQ(set.size(), 1u);
-  EXPECT_EQ(set[0].getDecl(), &n0);
+  EXPECT_EQ(set[0].getDecl(), n0);
 
   set.erase(--set.end());
   EXPECT_EQ(set.size(), 0u);


        


More information about the cfe-commits mailing list