[cfe-commits] r56983 - in /cfe/trunk: include/clang/AST/Builtins.def include/clang/AST/Builtins.h include/clang/AST/Expr.h lib/AST/Expr.cpp lib/Sema/SemaDecl.cpp test/Sema/constant-builtins.c
Daniel Dunbar
daniel at zuster.org
Thu Oct 2 16:30:31 PDT 2008
Author: ddunbar
Date: Thu Oct 2 18:30:31 2008
New Revision: 56983
URL: http://llvm.org/viewvc/llvm-project?rev=56983&view=rev
Log:
Add Builtins.def attribute for "can be a constant expression".
- Enabled for builtins which are always constant expressions
(__builtin_huge_val*, __builtin_inf*, __builtin_constant_p,
__builtin_classify_type, __builtin___CFStringMakeConstantString).
Added Builtin::Context::isConstantExpr.
- Currently overly simply interface which only works for builtins
whose constantexprness does not depend on their arguments.
CallExpr::isBuiltinConstantExpr now takes an ASTContext argument.
Added:
cfe/trunk/test/Sema/constant-builtins.c
Modified:
cfe/trunk/include/clang/AST/Builtins.def
cfe/trunk/include/clang/AST/Builtins.h
cfe/trunk/include/clang/AST/Expr.h
cfe/trunk/lib/AST/Expr.cpp
cfe/trunk/lib/Sema/SemaDecl.cpp
Modified: cfe/trunk/include/clang/AST/Builtins.def
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Builtins.def?rev=56983&r1=56982&r2=56983&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/Builtins.def (original)
+++ cfe/trunk/include/clang/AST/Builtins.def Thu Oct 2 18:30:31 2008
@@ -52,15 +52,16 @@
// n -> nothrow
// c -> const
// F -> this is a libc/libm function with a '__builtin_' prefix added.
+// C -> this builtin can be used as a "constant expression"
// FIXME: gcc has nonnull
// Standard libc/libm functions:
-BUILTIN(__builtin_huge_val, "d", "nc")
-BUILTIN(__builtin_huge_valf, "f", "nc")
-BUILTIN(__builtin_huge_vall, "Ld", "nc")
-BUILTIN(__builtin_inf , "d" , "nc")
-BUILTIN(__builtin_inff , "f" , "nc")
-BUILTIN(__builtin_infl , "Ld" , "nc")
+BUILTIN(__builtin_huge_val, "d", "ncC")
+BUILTIN(__builtin_huge_valf, "f", "ncC")
+BUILTIN(__builtin_huge_vall, "Ld", "ncC")
+BUILTIN(__builtin_inf , "d" , "ncC")
+BUILTIN(__builtin_inff , "f" , "ncC")
+BUILTIN(__builtin_infl , "Ld" , "ncC")
BUILTIN(__builtin_nan, "dcC*" , "ncF")
BUILTIN(__builtin_nanf, "fcC*" , "ncF")
BUILTIN(__builtin_nanl, "LdcC*", "ncF")
@@ -111,9 +112,9 @@
BUILTIN(__builtin_bswap64, "ULLiULLi", "nc")
// Random GCC builtins
-BUILTIN(__builtin_constant_p, "UsUs", "nc")
-BUILTIN(__builtin_classify_type, "i.", "nc")
-BUILTIN(__builtin___CFStringMakeConstantString, "FC*cC*", "nc")
+BUILTIN(__builtin_constant_p, "UsUs", "ncC")
+BUILTIN(__builtin_classify_type, "i.", "ncC")
+BUILTIN(__builtin___CFStringMakeConstantString, "FC*cC*", "ncC")
BUILTIN(__builtin_va_start, "va&.", "n")
BUILTIN(__builtin_va_end, "va&", "n")
BUILTIN(__builtin_va_copy, "va&a", "n")
Modified: cfe/trunk/include/clang/AST/Builtins.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Builtins.h?rev=56983&r1=56982&r2=56983&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/Builtins.h (original)
+++ cfe/trunk/include/clang/AST/Builtins.h Thu Oct 2 18:30:31 2008
@@ -73,11 +73,17 @@
}
/// isLibFunction - Return true if this is a builtin for a libc/libm function,
- /// with a "__builtin_" prefix (e.g. __builtin_inf).
+ /// with a "__builtin_" prefix (e.g. __builtin_abs).
bool isLibFunction(unsigned ID) const {
return strchr(GetRecord(ID).Attributes, 'F') != 0;
}
+ /// isConstantExpr - Return true if this builtin can be used where a
+ /// constant expression is required.
+ bool isConstantExpr(unsigned ID) const {
+ return strchr(GetRecord(ID).Attributes, 'C') != 0;
+ }
+
/// hasVAListUse - Return true of the specified builtin uses __builtin_va_list
/// as an operand or return type.
bool hasVAListUse(unsigned ID) const {
Modified: cfe/trunk/include/clang/AST/Expr.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Expr.h?rev=56983&r1=56982&r2=56983&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/Expr.h (original)
+++ cfe/trunk/include/clang/AST/Expr.h Thu Oct 2 18:30:31 2008
@@ -704,7 +704,7 @@
bool isBuiltinClassifyType(llvm::APSInt &Result) const;
/// isBuiltinConstantExpr - Return true if this built-in call is constant.
- bool isBuiltinConstantExpr() const;
+ bool isBuiltinConstantExpr(ASTContext &Ctx) const;
SourceLocation getRParenLoc() const { return RParenLoc; }
Modified: cfe/trunk/lib/AST/Expr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Expr.cpp?rev=56983&r1=56982&r2=56983&view=diff
==============================================================================
--- cfe/trunk/lib/AST/Expr.cpp (original)
+++ cfe/trunk/lib/AST/Expr.cpp Thu Oct 2 18:30:31 2008
@@ -139,7 +139,7 @@
this->NumArgs = NumArgs;
}
-bool CallExpr::isBuiltinConstantExpr() const {
+bool CallExpr::isBuiltinConstantExpr(ASTContext &Ctx) const {
// All simple function calls (e.g. func()) are implicitly cast to pointer to
// function. As a result, we try and obtain the DeclRefExpr from the
// ImplicitCastExpr.
@@ -159,10 +159,7 @@
if (!builtinID)
return false;
- // We have a builtin that is a constant expression
- return builtinID == Builtin::BI__builtin___CFStringMakeConstantString ||
- builtinID == Builtin::BI__builtin_classify_type ||
- builtinID == Builtin::BI__builtin_huge_valf;
+ return Ctx.BuiltinInfo.isConstantExpr(builtinID);
}
bool CallExpr::isBuiltinClassifyType(llvm::APSInt &Result) const {
@@ -585,7 +582,7 @@
return true;
case CallExprClass: {
const CallExpr *CE = cast<CallExpr>(this);
- if (CE->isBuiltinConstantExpr())
+ if (CE->isBuiltinConstantExpr(Ctx))
return true;
if (Loc) *Loc = getLocStart();
return false;
Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=56983&r1=56982&r2=56983&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Thu Oct 2 18:30:31 2008
@@ -878,7 +878,7 @@
return false;
case Expr::CallExprClass: {
const CallExpr *CE = cast<CallExpr>(Init);
- if (CE->isBuiltinConstantExpr())
+ if (CE->isBuiltinConstantExpr(Context))
return false;
Diag(Init->getExprLoc(),
diag::err_init_element_not_constant, Init->getSourceRange());
@@ -1077,7 +1077,7 @@
return false;
case Expr::CallExprClass: {
const CallExpr *CE = cast<CallExpr>(Init);
- if (CE->isBuiltinConstantExpr())
+ if (CE->isBuiltinConstantExpr(Context))
return false;
Diag(Init->getExprLoc(),
diag::err_init_element_not_constant, Init->getSourceRange());
Added: cfe/trunk/test/Sema/constant-builtins.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/constant-builtins.c?rev=56983&view=auto
==============================================================================
--- cfe/trunk/test/Sema/constant-builtins.c (added)
+++ cfe/trunk/test/Sema/constant-builtins.c Thu Oct 2 18:30:31 2008
@@ -0,0 +1,18 @@
+// RUN: clang -fsyntax-only %s
+
+// Math stuff
+
+float g0 = __builtin_huge_val();
+double g1 = __builtin_huge_valf();
+long double g2 = __builtin_huge_vall();
+float g3 = __builtin_inf();
+double g4 = __builtin_inff();
+long double g5 = __builtin_infl();
+
+// GCC misc stuff
+
+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);
More information about the cfe-commits
mailing list