]> granicus.if.org Git - clang/commitdiff
__builtin_object_size refinements. WIP.
authorMike Stump <mrs@apple.com>
Mon, 26 Oct 2009 18:35:08 +0000 (18:35 +0000)
committerMike Stump <mrs@apple.com>
Mon, 26 Oct 2009 18:35:08 +0000 (18:35 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@85136 91177308-0d34-0410-b5e6-96231b3b80d8

lib/AST/ExprConstant.cpp
test/CodeGen/object-size.c [new file with mode: 0644]

index 94d22998ebbed043b983d164f7badcdfce9e5edb..9129558421421c7792f79311552e9eef7aa8bb6a 100644 (file)
@@ -873,6 +873,36 @@ bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) {
   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);
 
diff --git a/test/CodeGen/object-size.c b/test/CodeGen/object-size.c
new file mode 100644 (file)
index 0000000..b686116
--- /dev/null
@@ -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");
+}