[cfe-commits] r57213 - in /cfe/trunk: include/clang/AST/DeclGroup.h lib/AST/DeclGroup.cpp

Ted Kremenek kremenek at apple.com
Mon Oct 6 15:36:45 PDT 2008


BTW, I want to thank Mike Stump for being patient enough to explain  
more of the C++ strict-aliasing rules to me.

On Oct 6, 2008, at 3:17 PM, Ted Kremenek wrote:

> Author: kremenek
> Date: Mon Oct  6 17:17:16 2008
> New Revision: 57213
>
> URL: http://llvm.org/viewvc/llvm-project?rev=57213&view=rev
> Log:
> Modified DeclGroupRef to always load/store the internal pointer  
> value as Decl*.  This hopefully will obviate any concerns with  
> violating strict type-aliasing issues.
>
> Modified:
>    cfe/trunk/include/clang/AST/DeclGroup.h
>    cfe/trunk/lib/AST/DeclGroup.cpp
>
> Modified: cfe/trunk/include/clang/AST/DeclGroup.h
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/DeclGroup.h?rev=57213&r1=57212&r2=57213&view=diff
>
> = 
> = 
> = 
> = 
> = 
> = 
> = 
> = 
> ======================================================================
> --- cfe/trunk/include/clang/AST/DeclGroup.h (original)
> +++ cfe/trunk/include/clang/AST/DeclGroup.h Mon Oct  6 17:17:16 2008
> @@ -42,31 +42,35 @@
>   }
> };
>
> +
> 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<uintptr_t>(D) & Mask);
> +  }
>
> public:
> -  DeclGroupRef() : Raw(0) {}
> +  DeclGroupRef() : D(0) {}
>
>   explicit DeclGroupRef(Decl* d) : D(d) {}
>   explicit DeclGroupRef(DeclGroup* dg)
> -    : Raw(reinterpret_cast<uintptr_t>(dg) | DeclGroupKind) {}
> +    : D((Decl*) (reinterpret_cast<uintptr_t>(D) | DeclGroupKind)) {}
>
>   typedef Decl** iterator;
>
>   iterator begin() {
> -    if (getKind() == DeclKind) return Raw ? &D : 0;
> -    DeclGroup* G = reinterpret_cast<DeclGroup*>(Raw & ~Mask);
> -    return &(*G)[0];
> +    if (getKind() == DeclKind) return D ? &D : 0;
> +    DeclGroup& G = *((DeclGroup*) (reinterpret_cast<uintptr_t>(D) &  
> ~Mask));
> +    return &G[0];
>   }
>
>   iterator end() {
> -    if (getKind() == DeclKind) return Raw ? &D + 1 : 0;
> -    DeclGroup* G = reinterpret_cast<DeclGroup*>(Raw & ~Mask);
> -    return &(*G)[0] + G->size();
> +    if (getKind() == DeclKind) return D ? &D + 1 : 0;
> +    DeclGroup& G = *((DeclGroup*) (reinterpret_cast<uintptr_t>(D) &  
> ~Mask));
> +    return &G[0] + G.size();
>   }
> };
>
> @@ -76,11 +80,11 @@
>   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;
>   }
> };
>
> Modified: cfe/trunk/lib/AST/DeclGroup.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/DeclGroup.cpp?rev=57213&r1=57212&r2=57213&view=diff
>
> = 
> = 
> = 
> = 
> = 
> = 
> = 
> = 
> ======================================================================
> --- cfe/trunk/lib/AST/DeclGroup.cpp (original)
> +++ cfe/trunk/lib/AST/DeclGroup.cpp Mon Oct  6 17:17:16 2008
> @@ -43,17 +43,18 @@
> }
>
> 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<Decl*>(Raw)->Destroy(C);
> -  else
> -    reinterpret_cast<DeclGroup*>(Raw & ~Mask)->Destroy(C);
> +    D->Destroy(C);
> +  else
> +    reinterpret_cast<DeclGroup*>(reinterpret_cast<uintptr_t>(D) &
> +                                 ~Mask)->Destroy(C);
>
> -  Raw = 0;
> +  D = 0;
> }
>
>
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits




More information about the cfe-commits mailing list