[cfe-commits] r85136 - in /cfe/trunk: lib/AST/ExprConstant.cpp test/CodeGen/object-size.c

Mike Stump mrs at apple.com
Mon Oct 26 11:35:08 PDT 2009


Author: mrs
Date: Mon Oct 26 13:35:08 2009
New Revision: 85136

URL: http://llvm.org/viewvc/llvm-project?rev=85136&view=rev
Log:
__builtin_object_size refinements.  WIP.

Added:
    cfe/trunk/test/CodeGen/object-size.c
Modified:
    cfe/trunk/lib/AST/ExprConstant.cpp

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

==============================================================================
--- cfe/trunk/lib/AST/ExprConstant.cpp (original)
+++ cfe/trunk/lib/AST/ExprConstant.cpp Mon Oct 26 13:35:08 2009
@@ -873,6 +873,36 @@
   switch (E->isBuiltinCall(Info.Ctx)) {
   default:
     return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
+
+  case Builtin::BI__builtin_object_size: {
+    llvm::APSInt Result(32);
+    
+    if (!E->getArg(1)->isIntegerConstantExpr(Result, Info.Ctx))
+      assert(0 && "arg2 not ice in __builtin_object_size");
+
+    const Expr *Arg = E->getArg(0)->IgnoreParens();
+    Expr::EvalResult Base;
+    if (Arg->Evaluate(Base, Info.Ctx)
+        && Base.Val.getKind() == APValue::LValue
+        && !Base.HasSideEffects)
+      if (const Expr *LVBase = Base.Val.getLValueBase())
+        if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(LVBase)) {
+          if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
+            uint64_t Size = Info.Ctx.getTypeSize(VD->getType()) / 8;
+            Size -= Base.Val.getLValueOffset();
+            return Success(Size, E);
+          }
+        }
+
+    if (Base.HasSideEffects) {
+      if (Result.getSExtValue() < 2)
+        return Success(-1, E);
+      return Success(0, E);
+    }
+    
+    return Error(E->getLocStart(), diag::note_invalid_subexpr_in_ice, E);
+  }
+
   case Builtin::BI__builtin_classify_type:
     return Success(EvaluateBuiltinClassifyType(E), E);
 

Added: cfe/trunk/test/CodeGen/object-size.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/object-size.c?rev=85136&view=auto

==============================================================================
--- cfe/trunk/test/CodeGen/object-size.c (added)
+++ cfe/trunk/test/CodeGen/object-size.c Mon Oct 26 13:35:08 2009
@@ -0,0 +1,33 @@
+// RUN: clang-cc -triple x86_64-apple-darwin -S -D_FORTIFY_SOURCE=2 %s -o %t.s &&
+// RUN: FileCheck --input-file=%t.s %s
+#include <string.h>
+
+char gbuf[63];
+char *gp;
+
+void test1() {
+  // CHECK:       movabsq $59, %rdx
+  // CHECK-NEXT:  movq    %rax, %rdi
+  // CHECK-NEXT:  movq    %rcx, %rsi
+  // CHECK-NEXT:  call    ___strcpy_chk
+  strcpy(&gbuf[4], "Hi there");
+}
+
+void test2() {
+  // CHECK:       movabsq $63, %rdx
+  // CHECK-NEXT:  movq    %rax, %rdi
+  // CHECK-NEXT:  movq    %rcx, %rsi
+  // CHECK-NEXT:  call    ___strcpy_chk
+  strcpy(gbuf, "Hi there");
+}
+
+void test4() {
+  // CHECK:       call    ___inline_strcpy_chk
+  strcpy(gp, "Hi");
+}
+
+void test3() {
+  int i;
+  // CHECK:       call    ___inline_strcpy_chk
+  strcpy((++i, gbuf), "Hi");
+}





More information about the cfe-commits mailing list