From f4f7cb814f8537606754b9eb8848094196e73214 Mon Sep 17 00:00:00 2001 From: Peter Collingbourne Date: Fri, 11 Mar 2011 19:24:59 +0000 Subject: [PATCH] OpenCL: if double precision floating point constant encountered without cl_khr_fp64, warn and cast to single precision git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@127476 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Basic/DiagnosticSemaKinds.td | 2 ++ lib/Sema/SemaExpr.cpp | 11 ++++++++--- test/SemaOpenCL/extension-fp64.cl | 8 +++++--- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index 2331132507..bac7050ae0 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -30,6 +30,8 @@ def warn_float_overflow : Warning< def warn_float_underflow : Warning< "magnitude of floating-point constant too small for type %0; minimum is %1">, InGroup; +def warn_double_const_requires_fp64 : Warning< + "double precision constant requires cl_khr_fp64, casting to single precision">; // C99 variable-length arrays def ext_vla : Extension< diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index 7dee50257d..8a532f0363 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -2601,9 +2601,14 @@ ExprResult Sema::ActOnNumericConstant(const Token &Tok) { bool isExact = (result == APFloat::opOK); Res = FloatingLiteral::Create(Context, Val, isExact, Ty, Tok.getLocation()); - if (getLangOptions().SinglePrecisionConstants && Ty == Context.DoubleTy) - ImpCastExprToType(Res, Context.FloatTy, CK_FloatingCast); - + if (Ty == Context.DoubleTy) { + if (getLangOptions().SinglePrecisionConstants) { + ImpCastExprToType(Res, Context.FloatTy, CK_FloatingCast); + } else if (getLangOptions().OpenCL && !getOpenCLOptions().cl_khr_fp64) { + Diag(Tok.getLocation(), diag::warn_double_const_requires_fp64); + ImpCastExprToType(Res, Context.FloatTy, CK_FloatingCast); + } + } } else if (!Literal.isIntegerLiteral()) { return ExprError(); } else { diff --git a/test/SemaOpenCL/extension-fp64.cl b/test/SemaOpenCL/extension-fp64.cl index eaf2509502..e0c2b1ea4b 100644 --- a/test/SemaOpenCL/extension-fp64.cl +++ b/test/SemaOpenCL/extension-fp64.cl @@ -1,17 +1,19 @@ // RUN: %clang_cc1 %s -verify -pedantic -fsyntax-only -void f1(double da) { // expected-error {{requires cl_khr_fp64 extension}} - double d; // expected-error {{requires cl_khr_fp64 extension}} +void f1(double da) { // expected-error {{type 'double' requires cl_khr_fp64 extension}} + double d; // expected-error {{type 'double' requires cl_khr_fp64 extension}} + (void) 1.0; // expected-warning {{double precision constant requires cl_khr_fp64}} } #pragma OPENCL EXTENSION cl_khr_fp64 : enable void f2(void) { double d; + (void) 1.0; } #pragma OPENCL EXTENSION cl_khr_fp64 : disable void f3(void) { - double d; // expected-error {{requires cl_khr_fp64 extension}} + double d; // expected-error {{type 'double' requires cl_khr_fp64 extension}} }