[cfe-commits] r55735 - in /cfe/trunk: include/clang/AST/Builtins.def include/clang/Basic/DiagnosticKinds.def lib/CodeGen/CGBuiltin.cpp lib/Sema/Sema.h lib/Sema/SemaChecking.cpp
Daniel Dunbar
daniel at zuster.org
Wed Sep 3 14:13:56 PDT 2008
Author: ddunbar
Date: Wed Sep 3 16:13:56 2008
New Revision: 55735
URL: http://llvm.org/viewvc/llvm-project?rev=55735&view=rev
Log:
Add __builtin_object_size support.
- Currently CodeGen always returns a conservative value for this (-1
or 0 depending on the context).
Modified:
cfe/trunk/include/clang/AST/Builtins.def
cfe/trunk/include/clang/Basic/DiagnosticKinds.def
cfe/trunk/lib/CodeGen/CGBuiltin.cpp
cfe/trunk/lib/Sema/Sema.h
cfe/trunk/lib/Sema/SemaChecking.cpp
Modified: cfe/trunk/include/clang/AST/Builtins.def
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Builtins.def?rev=55735&r1=55734&r2=55735&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/Builtins.def (original)
+++ cfe/trunk/include/clang/AST/Builtins.def Wed Sep 3 16:13:56 2008
@@ -124,6 +124,7 @@
BUILTIN(__builtin_return_address, "v*Ui", "n")
BUILTIN(__builtin_frame_address, "v*Ui", "n")
// GCC Object size checking builtins
+BUILTIN(__builtin_object_size, "zv*i", "n")
BUILTIN(__builtin___memset_chk, "v*v*izz", "nF")
BUILTIN(__builtin___memcpy_chk, "v*v*vC*zz", "nF")
BUILTIN(__builtin___memmove_chk, "v*v*vC*zz", "nF")
Modified: cfe/trunk/include/clang/Basic/DiagnosticKinds.def
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticKinds.def?rev=55735&r1=55734&r2=55735&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticKinds.def (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticKinds.def Wed Sep 3 16:13:56 2008
@@ -1197,7 +1197,10 @@
DIAG(err_prefetch_invalid_argument, ERROR,
"argument to __builtin_prefetch must be a constant integer")
-DIAG(err_prefetch_invalid_range, ERROR,
+DIAG(err_argument_invalid_range, ERROR,
"argument should be a value from %0 to %1")
+DIAG(err_object_size_invalid_argument, ERROR,
+ "argument to __builtin_object_size must be a constant integer")
+
#undef DIAG
Modified: cfe/trunk/lib/CodeGen/CGBuiltin.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGBuiltin.cpp?rev=55735&r1=55734&r2=55735&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGBuiltin.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGBuiltin.cpp Wed Sep 3 16:13:56 2008
@@ -199,6 +199,16 @@
Value *F = CGM.getIntrinsic(Intrinsic::bswap, &ArgType, 1);
return RValue::get(Builder.CreateCall(F, ArgValue, "tmp"));
}
+ case Builtin::BI__builtin_object_size: {
+ // FIXME: Implement. For now we just always fail and pretend we
+ // don't know the object size.
+ llvm::APSInt TypeArg =
+ E->getArg(1)->getIntegerConstantExprValue(CGM.getContext());
+ const llvm::Type *ResType = ConvertType(E->getType());
+ // bool UseSubObject = TypeArg.getZExtValue() & 1;
+ bool UseMinimum = TypeArg.getZExtValue() & 2;
+ return RValue::get(ConstantInt::get(ResType, UseMinimum ? 0 : -1LL));
+ }
case Builtin::BI__builtin_prefetch: {
Value *Locality, *RW, *Address = EmitScalarExpr(E->getArg(0));
// FIXME: Technically these constants should of type 'int', yes?
Modified: cfe/trunk/lib/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.h?rev=55735&r1=55734&r2=55735&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/Sema.h (original)
+++ cfe/trunk/lib/Sema/Sema.h Wed Sep 3 16:13:56 2008
@@ -952,6 +952,7 @@
bool SemaBuiltinStackAddress(CallExpr *TheCall);
Action::ExprResult SemaBuiltinShuffleVector(CallExpr *TheCall);
bool SemaBuiltinPrefetch(CallExpr *TheCall);
+ bool SemaBuiltinObjectSize(CallExpr *TheCall);
void CheckPrintfArguments(CallExpr *TheCall,
bool HasVAListArg, unsigned format_idx);
void CheckReturnStackAddr(Expr *RetValExp, QualType lhsType,
Modified: cfe/trunk/lib/Sema/SemaChecking.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=55735&r1=55734&r2=55735&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)
+++ cfe/trunk/lib/Sema/SemaChecking.cpp Wed Sep 3 16:13:56 2008
@@ -62,6 +62,9 @@
if (SemaBuiltinPrefetch(TheCall.get()))
return true;
return TheCall.take();
+ case Builtin::BI__builtin_object_size:
+ if (SemaBuiltinObjectSize(TheCall.get()))
+ return true;
}
// Search the KnownFunctionIDs for the identifier.
@@ -300,8 +303,7 @@
QualType RWType = Arg->getType();
const BuiltinType *BT = RWType->getAsBuiltinType();
- // FIXME: 32 is wrong, needs to be proper width of Int
- llvm::APSInt Result(32);
+ llvm::APSInt Result;
if (!BT || BT->getKind() != BuiltinType::Int ||
!Arg->isIntegerConstantExpr(Result, Context)) {
if (Diag(TheCall->getLocStart(), diag::err_prefetch_invalid_argument,
@@ -316,12 +318,12 @@
// is 3.
if (i==1) {
if (Result.getSExtValue() < 0 || Result.getSExtValue() > 1)
- res |= Diag(TheCall->getLocStart(), diag::err_prefetch_invalid_range,
+ res |= Diag(TheCall->getLocStart(), diag::err_argument_invalid_range,
"0", "1",
SourceRange(Arg->getLocStart(), Arg->getLocEnd()));
} else {
if (Result.getSExtValue() < 0 || Result.getSExtValue() > 3)
- res |= Diag(TheCall->getLocStart(), diag::err_prefetch_invalid_range,
+ res |= Diag(TheCall->getLocStart(), diag::err_argument_invalid_range,
"0", "3",
SourceRange(Arg->getLocStart(), Arg->getLocEnd()));
}
@@ -330,6 +332,29 @@
return res;
}
+/// SemaBuiltinObjectSize - Handle __builtin_object_size(void *ptr,
+/// int type). This simply type checks that type is one of the defined
+/// constants (0-3).
+bool Sema::SemaBuiltinObjectSize(CallExpr *TheCall) {
+ Expr *Arg = TheCall->getArg(1);
+ QualType ArgType = Arg->getType();
+ const BuiltinType *BT = ArgType->getAsBuiltinType();
+ llvm::APSInt Result(32);
+ if (!BT || BT->getKind() != BuiltinType::Int ||
+ !Arg->isIntegerConstantExpr(Result, Context)) {
+ return Diag(TheCall->getLocStart(), diag::err_object_size_invalid_argument,
+ SourceRange(Arg->getLocStart(), Arg->getLocEnd()));
+ }
+
+ if (Result.getSExtValue() < 0 || Result.getSExtValue() > 3) {
+ return Diag(TheCall->getLocStart(), diag::err_argument_invalid_range,
+ "0", "3",
+ SourceRange(Arg->getLocStart(), Arg->getLocEnd()));
+ }
+
+ return false;
+}
+
/// CheckPrintfArguments - Check calls to printf (and similar functions) for
/// correct use of format strings.
///
More information about the cfe-commits
mailing list