]> granicus.if.org Git - clang/commitdiff
Fix some type punning errors in SizeOfAlignOf and Typeid AST nodes. This should satis...
authorSebastian Redl <sebastian.redl@getdesigned.at>
Wed, 3 Dec 2008 23:17:54 +0000 (23:17 +0000)
committerSebastian Redl <sebastian.redl@getdesigned.at>
Wed, 3 Dec 2008 23:17:54 +0000 (23:17 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@60511 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/AST/Expr.h
include/clang/AST/ExprCXX.h
lib/AST/Expr.cpp
lib/AST/ExprCXX.cpp
lib/AST/StmtSerialization.cpp

index 932885eb30dfb3571e17029cd21ab56d547f39a0..02e581dce4bf02fd19dab1597c8acdf1b464a7a0 100644 (file)
@@ -557,15 +557,23 @@ public:
 class SizeOfAlignOfExpr : public Expr {
   bool isSizeof : 1;  // true if sizeof, false if alignof.
   bool isType : 1;    // true if operand is a type, false if an expression
-  void *Argument;
+  union {
+    void *Ty;
+    Stmt *Ex;
+  } Argument;
   SourceLocation OpLoc, RParenLoc;
 public:
   SizeOfAlignOfExpr(bool issizeof, bool istype, void *argument,
                     QualType resultType, SourceLocation op,
                     SourceLocation rp) :
-    Expr(SizeOfAlignOfExprClass, resultType),
-    isSizeof(issizeof), isType(istype), Argument(argument),
-    OpLoc(op), RParenLoc(rp) {}
+      Expr(SizeOfAlignOfExprClass, resultType), isSizeof(issizeof),
+      isType(istype), OpLoc(op), RParenLoc(rp) {
+    if (isType)
+      Argument.Ty = argument;
+    else
+      // argument was an Expr*, so cast it back to that to be safe
+      Argument.Ex = static_cast<Expr*>(argument);
+  }
 
   virtual void Destroy(ASTContext& C);
 
@@ -573,11 +581,11 @@ public:
   bool isArgumentType() const { return isType; }
   QualType getArgumentType() const {
     assert(isArgumentType() && "calling getArgumentType() when arg is expr");
-    return QualType::getFromOpaquePtr(Argument);
+    return QualType::getFromOpaquePtr(Argument.Ty);
   }
   Expr* getArgumentExpr() const {
     assert(!isArgumentType() && "calling getArgumentExpr() when arg is type");
-    return (Expr *)Argument;
+    return static_cast<Expr*>(Argument.Ex);
   }
   /// Gets the argument type, or the type of the argument expression, whichever
   /// is appropriate.
index 2fb55fb4afaa57c113e3ab3e063a0077c6fb0585..1d0a516eaf529870b58306e2897b83b5efe7140c 100644 (file)
@@ -193,21 +193,30 @@ public:
 class CXXTypeidExpr : public Expr {
 private:
   bool isTypeOp : 1;
-  void *Operand;
+  union {
+    void *Ty;
+    Stmt *Ex;
+  } Operand;
   SourceRange Range;
 
 public:
   CXXTypeidExpr(bool isTypeOp, void *op, QualType Ty, const SourceRange r) :
-    Expr(CXXTypeidExprClass, Ty), isTypeOp(isTypeOp), Operand(op), Range(r) {}
+      Expr(CXXTypeidExprClass, Ty), isTypeOp(isTypeOp), Range(r) {
+    if (isTypeOp)
+      Operand.Ty = op;
+    else
+      // op was an Expr*, so cast it back to that to be safe
+      Operand.Ex = static_cast<Stmt*>(op);
+  }
 
   bool isTypeOperand() const { return isTypeOp; }
   QualType getTypeOperand() const {
     assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)");
-    return QualType::getFromOpaquePtr(Operand);
+    return QualType::getFromOpaquePtr(Operand.Ty);
   }
   Expr* getExprOperand() const {
     assert(!isTypeOperand() && "Cannot call getExprOperand for typeid(type)");
-    return static_cast<Expr*>(Operand);
+    return static_cast<Expr*>(Operand.Ex);
   }
 
   virtual SourceRange getSourceRange() const {
index 948dbc241897106bf667003769e76e5b7362ddb0..e4386eceda1830676859e63276844424249554dc 100644 (file)
@@ -1314,12 +1314,12 @@ Stmt::child_iterator SizeOfAlignOfExpr::child_begin() {
       return child_iterator(T);
     return child_iterator();
   }
-  return child_iterator((Stmt**)&Argument);
+  return child_iterator(&Argument.Ex);
 }
 Stmt::child_iterator SizeOfAlignOfExpr::child_end() {
   if (isArgumentType())
     return child_iterator();
-  return child_iterator((Stmt**)&Argument + 1);
+  return child_iterator(&Argument.Ex + 1);
 }
 
 // ArraySubscriptExpr
index 1bf07c4a63e770343e44317f3a229a1a737eb146..2d517196be6c4a6c75c356216fb3afb5157e2a90 100644 (file)
@@ -28,10 +28,10 @@ void CXXConditionDeclExpr::Destroy(ASTContext& C) {
 
 // CXXTypeidExpr - has child iterators if the operand is an expression
 Stmt::child_iterator CXXTypeidExpr::child_begin() {
-  return isTypeOperand() ? child_iterator() : (Stmt**)&Operand;
+  return isTypeOperand() ? child_iterator() : &Operand.Ex;
 }
 Stmt::child_iterator CXXTypeidExpr::child_end() {
-  return isTypeOperand() ? child_iterator() : (Stmt**)&Operand+1;
+  return isTypeOperand() ? child_iterator() : &Operand.Ex+1;
 }
 
 // CXXBoolLiteralExpr
index a8602245f3e99aa0fd3efec97022d84820ab2764..78a2cbcaa7683c1f1e4b5a8cdb50b3b94ac9eaf6 100644 (file)
@@ -1394,7 +1394,7 @@ CXXNamedCastExpr::CreateImpl(Deserializer& D, ASTContext& C, StmtClass SC) {
 
 void CXXTypeidExpr::EmitImpl(llvm::Serializer& S) const {
   S.Emit(getType());
-  S.Emit(isTypeOperand());
+  S.EmitBool(isTypeOperand());
   if (isTypeOperand()) {
     S.Emit(getTypeOperand());
   } else {