From 6d60a4ec1955d633c12eeea1422e3c505064c511 Mon Sep 17 00:00:00 2001 From: Ted Kremenek Date: Mon, 6 Oct 2008 22:17:16 +0000 Subject: [PATCH] Modified DeclGroupRef to always load/store the internal pointer value as Decl*. This hopefully will obviate any concerns with violating strict type-aliasing issues. llvm-svn: 57213 --- clang/include/clang/AST/DeclGroup.h | 32 ++++++++++++++++------------- clang/lib/AST/DeclGroup.cpp | 13 ++++++------ 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/clang/include/clang/AST/DeclGroup.h b/clang/include/clang/AST/DeclGroup.h index 4ad2dfcd2258..4818fc219a1f 100644 --- a/clang/include/clang/AST/DeclGroup.h +++ b/clang/include/clang/AST/DeclGroup.h @@ -42,31 +42,35 @@ public: } }; + class DeclGroupRef { protected: - enum Kind { DeclKind=0x0, DeclGroupKind=0x1, Mask=0x1 }; - union { Decl* D; uintptr_t Raw; }; - Kind getKind() const { return (Kind) (Raw & Mask); } + enum Kind { DeclKind=0x0, DeclGroupKind=0x1, Mask=0x1 }; + Decl* D; + + Kind getKind() const { + return (Kind) (reinterpret_cast(D) & Mask); + } public: - DeclGroupRef() : Raw(0) {} + DeclGroupRef() : D(0) {} explicit DeclGroupRef(Decl* d) : D(d) {} explicit DeclGroupRef(DeclGroup* dg) - : Raw(reinterpret_cast(dg) | DeclGroupKind) {} + : D((Decl*) (reinterpret_cast(D) | DeclGroupKind)) {} typedef Decl** iterator; iterator begin() { - if (getKind() == DeclKind) return Raw ? &D : 0; - DeclGroup* G = reinterpret_cast(Raw & ~Mask); - return &(*G)[0]; + if (getKind() == DeclKind) return D ? &D : 0; + DeclGroup& G = *((DeclGroup*) (reinterpret_cast(D) & ~Mask)); + return &G[0]; } iterator end() { - if (getKind() == DeclKind) return Raw ? &D + 1 : 0; - DeclGroup* G = reinterpret_cast(Raw & ~Mask); - return &(*G)[0] + G->size(); + if (getKind() == DeclKind) return D ? &D + 1 : 0; + DeclGroup& G = *((DeclGroup*) (reinterpret_cast(D) & ~Mask)); + return &G[0] + G.size(); } }; @@ -76,11 +80,11 @@ public: void Destroy(ASTContext& C); explicit DeclGroupOwningRef(DeclGroupOwningRef& R) - : DeclGroupRef(R) { R.Raw = 0; } + : DeclGroupRef(R) { R.D = 0; } DeclGroupOwningRef& operator=(DeclGroupOwningRef& R) { - Raw = R.Raw; - R.Raw = 0; + D = R.D; + R.D = 0; return *this; } }; diff --git a/clang/lib/AST/DeclGroup.cpp b/clang/lib/AST/DeclGroup.cpp index 6ec0e5beafa4..34b37dada5c7 100644 --- a/clang/lib/AST/DeclGroup.cpp +++ b/clang/lib/AST/DeclGroup.cpp @@ -43,17 +43,18 @@ void DeclGroup::Destroy(ASTContext& C) { } DeclGroupOwningRef::~DeclGroupOwningRef() { - assert (Raw == 0 && "Destroy method not called."); + assert (D == 0 && "Destroy method not called."); } void DeclGroupOwningRef::Destroy(ASTContext& C) { - if (!Raw) + if (!D) return; if (getKind() == DeclKind) - reinterpret_cast(Raw)->Destroy(C); - else - reinterpret_cast(Raw & ~Mask)->Destroy(C); + D->Destroy(C); + else + reinterpret_cast(reinterpret_cast(D) & + ~Mask)->Destroy(C); - Raw = 0; + D = 0; }