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

Chris Lattner sabre at nondot.org
Fri Mar 13 10:28:02 PDT 2009


Author: lattner
Date: Fri Mar 13 12:28:01 2009
New Revision: 66909

URL: http://llvm.org/viewvc/llvm-project?rev=66909&view=rev
Log:
add a helper function to strip noop casts.

Modified:
    cfe/trunk/include/clang/AST/Expr.h
    cfe/trunk/lib/AST/Expr.cpp

Modified: cfe/trunk/include/clang/AST/Expr.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Expr.h?rev=66909&r1=66908&r2=66909&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/Expr.h (original)
+++ cfe/trunk/include/clang/AST/Expr.h Fri Mar 13 12:28:01 2009
@@ -253,13 +253,21 @@
   /// or CastExprs, returning their operand.
   Expr *IgnoreParenCasts();
   
+  /// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
+  /// value (including ptr->int casts of the same size).  Strip off any
+  /// ParenExpr or CastExprs, returning their operand.
+  Expr *IgnoreParenNoopCasts(ASTContext &Ctx);
+  
   const Expr* IgnoreParens() const {
     return const_cast<Expr*>(this)->IgnoreParens();
   }
   const Expr *IgnoreParenCasts() const {
     return const_cast<Expr*>(this)->IgnoreParenCasts();
   }
-
+  const Expr *IgnoreParenNoopCasts(ASTContext &Ctx) const {
+    return const_cast<Expr*>(this)->IgnoreParenNoopCasts(Ctx);
+  }
+  
   static bool hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs);
   static bool hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs);
 

Modified: cfe/trunk/lib/AST/Expr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Expr.cpp?rev=66909&r1=66908&r2=66909&view=diff

==============================================================================
--- cfe/trunk/lib/AST/Expr.cpp (original)
+++ cfe/trunk/lib/AST/Expr.cpp Fri Mar 13 12:28:01 2009
@@ -796,6 +796,40 @@
   }
 }
 
+/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
+/// value (including ptr->int casts of the same size).  Strip off any
+/// ParenExpr or CastExprs, returning their operand.
+Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
+  Expr *E = this;
+  while (true) {
+    if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
+      E = P->getSubExpr();
+      continue;
+    }
+    
+    if (CastExpr *P = dyn_cast<CastExpr>(E)) {
+      // We ignore integer <-> casts that are of the same width, ptr<->ptr and
+      // ptr<->int casts of the same width.  We also ignore all identify casts.
+      Expr *SE = P->getSubExpr();
+      
+      if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
+        E = SE;
+        continue;
+      }
+      
+      if ((E->getType()->isPointerType() || E->getType()->isIntegralType()) &&
+          (SE->getType()->isPointerType() || SE->getType()->isIntegralType()) &&
+          Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
+        E = SE;
+        continue;
+      }
+    }
+    
+    return E;
+  }
+}
+
+
 /// hasAnyTypeDependentArguments - Determines if any of the expressions
 /// in Exprs is type-dependent.
 bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {





More information about the cfe-commits mailing list