[cfe-commits] [PATCH] Allow __builtin_bswap32/64 in constant expressions like gcc

Tijl Coosemans tijl at coosemans.org
Fri Sep 28 02:40:24 PDT 2012


On 26-09-2012 21:58, Richard Smith wrote:
> The patch looks fine, but please add a testcase.

Something like this?
-------------- next part --------------
Index: lib/AST/ExprConstant.cpp
===================================================================
--- lib/AST/ExprConstant.cpp	(revision 164813)
+++ lib/AST/ExprConstant.cpp	(working copy)
@@ -4296,6 +4296,15 @@ bool IntExprEvaluator::VisitCallExpr(const CallExp
     return Error(E);
   }
 
+  case Builtin::BI__builtin_bswap32:
+  case Builtin::BI__builtin_bswap64: {
+    APSInt Val;
+    if (!EvaluateInteger(E->getArg(0), Val, Info))
+      return false;
+
+    return Success(Val.byteSwap(), E);
+  }
+
   case Builtin::BI__builtin_classify_type:
     return Success(EvaluateBuiltinClassifyType(E), E);
 
Index: test/Sema/constant-builtins.c
===================================================================
--- test/Sema/constant-builtins.c	(revision 164813)
+++ test/Sema/constant-builtins.c	(working copy)
@@ -16,6 +16,8 @@ extern int f();
 int h0 = __builtin_types_compatible_p(int,float);
 //int h1 = __builtin_choose_expr(1, 10, f());
 //int h2 = __builtin_expect(0, 0);
+int h3 = __builtin_bswap32(0x1234) == 0x34120000 ? 1 : f();
+int h4 = __builtin_bswap64(0x1234) == 0x3412000000000000 ? 1 : f();
 
 short somefunc();
 
Index: test/Sema/constant-builtins-2.c
===================================================================
--- test/Sema/constant-builtins-2.c	(revision 164813)
+++ test/Sema/constant-builtins-2.c	(working copy)
@@ -48,6 +48,8 @@ extern int f();
 int h0 = __builtin_types_compatible_p(int, float);
 //int h1 = __builtin_choose_expr(1, 10, f());
 //int h2 = __builtin_expect(0, 0);
+int h3 = __builtin_bswap32(0x1234) == 0x34120000 ? 1 : f();
+int h4 = __builtin_bswap64(0x1234) == 0x3412000000000000 ? 1 : f();
 extern long int bi0;
 extern __typeof__(__builtin_expect(0, 0)) bi0;
 


More information about the cfe-commits mailing list