262 lines · cpp
1// Use of NAN macro will trigger a warning "infinity defined in macro" because2// on Windows the NAN macro is defined using INFINITY. See below.3 4// RUN: %clang_cc1 -x c++ -verify=no-inf-no-nan \5// RUN: -triple powerpc64le-unknown-unknown %s -menable-no-infs \6// RUN: -menable-no-nans -std=c++237 8// RUN: %clang_cc1 -x c++ -verify=no-inf-no-nan \9// RUN: -triple powerpc64le-unknown-unknown %s \10// RUN: -menable-no-infs -menable-no-nans -funsafe-math-optimizations \11// RUN: -std=c++2312 13// RUN: %clang_cc1 -x c++ -verify=no-fast -triple powerpc64le-unknown-unknown \14// RUN: %s -std=c++2315 16// RUN: %clang_cc1 -x c++ -verify=no-inf -triple powerpc64le-unknown-unknown %s \17// RUN: -menable-no-infs -std=c++2318 19// RUN: %clang_cc1 -x c++ -verify=no-inf -triple powerpc64le-unknown-unknown %s \20// RUN: -menable-no-infs -funsafe-math-optimizations -std=c++2321 22// RUN: %clang_cc1 -x c++ -verify=no-nan -triple powerpc64le-unknown-unknown %s \23// RUN: -menable-no-nans -std=c++2324 25// RUN: %clang_cc1 -x c++ -verify=no-nan -triple powerpc64le-unknown-unknown %s \26// RUN: -funsafe-math-optimizations -menable-no-nans -std=c++2327 28// RUN: %clang_cc1 -x c++ -verify=no-fast -triple powerpc64le-unknown-unknown \29// RUN: %s -Wno-nan-infinity-disabled -menable-no-infs -std=c++2330 31// RUN: %clang_cc1 -x c++ -verify=no-fast -triple powerpc64le-unknown-unknown \32// RUN: %s -Wno-nan-infinity-disabled -menable-no-nans -std=c++2333 34// no-fast-no-diagnostics35 36int isunorderedf (float x, float y);37extern "C++" {38namespace std __attribute__((__visibility__("default"))) {39 bool40 isinf(float __x);41 bool42 isinf(double __x);43 bool44 isinf(long double __x);45 bool46 isnan(float __x);47 bool48 isnan(double __x);49 bool50 isnan(long double __x);51 bool52 isfinite(float __x);53 bool54 isfinite(double __x);55 bool56 isfinte(long double __x);57 bool58 isunordered(float __x, float __y);59 bool60 isunordered(double __x, double __y);61 bool62 isunordered(long double __x, long double __y);63 64template <class _Ty>65class numeric_limits {66public:67 [[nodiscard]] static constexpr _Ty infinity() noexcept {68 return _Ty();69 }70};71 72} // namespace )73}74 75#define INFINITY ((float)(1e+300 * 1e+300))76#define NAN (-(float)(INFINITY * 0.0F))77 78template <>79class std::numeric_limits<float> {80public:81 [[nodiscard]] static constexpr float infinity() noexcept {82 return __builtin_huge_val();83 }84};85 86template <>87class std::numeric_limits<double> {88public:89 [[nodiscard]] static constexpr double infinity() noexcept {90 return __builtin_huge_val();91 }92};93 94template <class _Ty>95class numeric_limits {96public:97 [[nodiscard]] static constexpr _Ty infinity() noexcept {98 return _Ty();99 }100};101 102template <>103class numeric_limits<float> {104public:105 [[nodiscard]] static constexpr float infinity() noexcept {106 return __builtin_huge_val();107 }108};109 110template <>111class numeric_limits<double> {112public:113 [[nodiscard]] static constexpr double infinity() noexcept {114 return __builtin_huge_val();115 }116};117 118double infinity() { return 0; }119 120int compareit(float a, float b) {121 volatile int i, j, k, l, m, n, o, p;122// no-inf-no-nan-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}}123// no-inf-warning@+1 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}}124 i = a == INFINITY;125 126// no-inf-no-nan-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}}127// no-inf-warning@+1 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}}128 j = INFINITY == a;129 130// no-inf-no-nan-warning@+4 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}}131// no-inf-no-nan-warning@+3 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}}132// no-inf-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}}133// no-nan-warning@+1 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}}134 i = a == NAN;135 136// no-inf-no-nan-warning@+4 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}}137// no-inf-no-nan-warning@+3 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}}138// no-inf-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}}139// no-nan-warning@+1 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}}140 j = NAN == a;141 142// no-inf-no-nan-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}}143// no-inf-warning@+1 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}}144 j = INFINITY <= a;145 146// no-inf-no-nan-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}}147// no-inf-warning@+1 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}}148 j = INFINITY < a;149 150// no-inf-no-nan-warning@+4 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}}151// no-inf-no-nan-warning@+3 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}}152// no-inf-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}}153// no-nan-warning@+1 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}}154 j = a > NAN;155 156// no-inf-no-nan-warning@+4 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}}157// no-inf-no-nan-warning@+3 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}}158// no-inf-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}}159// no-nan-warning@+1 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}}160 j = a >= NAN;161 162// no-inf-no-nan-warning@+2 {{use of infinity is undefined behavior due to the currently enabled floating-point options}}163// no-inf-warning@+1 {{use of infinity is undefined behavior due to the currently enabled floating-point options}}164 k = std::isinf(a);165 166// no-inf-no-nan-warning@+2 {{use of NaN is undefined behavior due to the currently enabled floating-point options}}167// no-nan-warning@+1 {{use of NaN is undefined behavior due to the currently enabled floating-point options}}168 l = std::isnan(a);169 170// no-inf-no-nan-warning@+2 {{use of infinity is undefined behavior due to the currently enabled floating-point options}}171// no-inf-warning@+1 {{use of infinity is undefined behavior due to the currently enabled floating-point options}}172 o = std::isfinite(a);173 174// no-inf-no-nan-warning@+2 {{use of infinity is undefined behavior due to the currently enabled floating-point options}}175// no-inf-warning@+1 {{use of infinity is undefined behavior due to the currently enabled floating-point options}}176 m = __builtin_isinf(a);177 178// no-inf-no-nan-warning@+2 {{use of NaN is undefined behavior due to the currently enabled floating-point options}}179// no-nan-warning@+1 {{use of NaN is undefined behavior due to the currently enabled floating-point options}}180 n = __builtin_isnan(a);181 182// no-inf-no-nan-warning@+2 {{use of infinity is undefined behavior due to the currently enabled floating-point options}}183// no-inf-warning@+1 {{use of infinity is undefined behavior due to the currently enabled floating-point options}}184 p = __builtin_isfinite(a);185 186// These should NOT warn, since they are not using NaN or infinity.187 j = a > 1.1;188 j = b < 1.1;189 j = a >= 1.1;190 j = b <= 1.1;191 j = isunorderedf(a, b);192 193#ifndef INFINITY194 j = a;195#endif196#ifndef NAN197 j = b;198#endif199#ifdef INFINITY200 j = a;201#endif202#ifdef NAN203 j = b;204#endif205#if defined(INFINITY)206 j = a;207#elifndef(INFINITY)208 j = b;209#endif210#if defined(INFINITY)211 j = a;212#elifndef(NAN)213 j = b;214#endif215#if defined(NAN)216 j = a;217#elifndef(INFINITY)218 j = b;219#endif220 221// no-inf-no-nan-warning@+4 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point option}}222// no-inf-no-nan-warning@+3 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}}223// no-inf-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}}224// no-nan-warning@+1 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}}225 j = isunorderedf(a, NAN);226 227// no-inf-no-nan-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}}228// no-inf-warning@+1 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}}229 j = isunorderedf(a, INFINITY);230 231// no-inf-no-nan-warning@+6 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}}232// no-inf-no-nan-warning@+5 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}}233// no-inf-no-nan-warning@+4 {{use of NaN is undefined behavior due to the currently enabled floating-point options}}234// no-inf-warning@+3 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}}235// no-nan-warning@+2 {{use of NaN via a macro is undefined behavior due to the currently enabled floating-point options}}236// no-nan-warning@+1 {{use of NaN is undefined behavior due to the currently enabled floating-point options}}237 i = std::isunordered(a, NAN);238 239// no-inf-no-nan-warning@+4 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}}240// no-inf-no-nan-warning@+3 {{use of NaN is undefined behavior due to the currently enabled floating-point options}}241// no-inf-warning@+2 {{use of infinity via a macro is undefined behavior due to the currently enabled floating-point options}}242// no-nan-warning@+1 {{use of NaN is undefined behavior due to the currently enabled floating-point options}}243 i = std::isunordered(a, INFINITY);244 245// no-inf-no-nan-warning@+2 {{use of infinity is undefined behavior due to the currently enabled floating-point options}}246// no-inf-warning@+1 {{use of infinity is undefined behavior due to the currently enabled floating-point options}}247 double y = i * std::numeric_limits<double>::infinity();248 249 y = i * numeric_limits<double>::infinity(); // expected-no-diagnostics250 251// no-inf-no-nan-warning@+2 {{use of infinity is undefined behavior due to the currently enabled floating-point options}}252// no-inf-warning@+1 {{use of infinity is undefined behavior due to the currently enabled floating-point options}}253 j = std::numeric_limits<float>::infinity();254 255 j = numeric_limits<float>::infinity(); // expected-no-diagnostics256 257 y = infinity(); // expected-no-diagnostics258 259 return 0;260 261}262