From: Ted Kremenek Date: Mon, 6 Oct 2008 22:17:16 +0000 (+0000) Subject: Modified DeclGroupRef to always load/store the internal pointer value as Decl*. ... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e285a9505c0be75d4b58e67aa32e82de55889235;p=clang Modified DeclGroupRef to always load/store the internal pointer value as Decl*. This hopefully will obviate any concerns with violating strict type-aliasing issues. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@57213 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/AST/DeclGroup.h b/include/clang/AST/DeclGroup.h index 4ad2dfcd22..4818fc219a 100644 --- a/include/clang/AST/DeclGroup.h +++ b/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/lib/AST/DeclGroup.cpp b/lib/AST/DeclGroup.cpp index 6ec0e5beaf..34b37dada5 100644 --- a/lib/AST/DeclGroup.cpp +++ b/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; }