xref: /llvm-project/clang/test/AST/ast-dump-fpfeatures.cpp (revision 0140ba031c9b6a4028b36adb292fb37eb62f3dc0)
1 // Test without serialization:
2 // RUN: %clang_cc1 -triple x86_64-pc-linux -std=c++11 -fcxx-exceptions -ast-dump %s \
3 // RUN: | FileCheck --strict-whitespace %s
4 
5 // Test with serialization:
6 // RUN: %clang_cc1 -triple x86_64-pc-linux -emit-pch -fcxx-exceptions -o %t %s
7 // RUN: %clang_cc1 -x c++ -triple x86_64-pc-linux -include-pch %t -fcxx-exceptions -ast-dump-all /dev/null \
8 // RUN: | sed -e "s/ <undeserialized declarations>//" -e "s/ imported//" \
9 // RUN: | FileCheck --strict-whitespace %s
10 
11 float func_01(float x);
12 
13 template <typename T>
func_02(T x)14 T func_02(T x) {
15 #pragma STDC FP_CONTRACT ON
16   return func_01(x);
17 }
18 
func_03(float x)19 float func_03(float x) {
20 #pragma STDC FP_CONTRACT OFF
21   return func_02(x);
22 }
23 
24 // CHECK:      FunctionTemplateDecl {{.*}} func_02
25 // CHECK:        FunctionDecl {{.*}} func_02 'float (float)'
26 // CHECK-NEXT:     TemplateArgument type 'float'
27 // CHECK-NEXT:       BuiltinType {{.*}} 'float'
28 // CHECK-NEXT:     ParmVarDecl {{.*}} x 'float'
29 // CHECK-NEXT:     CompoundStmt
30 // CHECK-NEXT:       ReturnStmt
31 // CHECK-NEXT:         CallExpr {{.*}} FPContractMode=1
32 
33 // CHECK:      FunctionDecl {{.*}} func_03 'float (float)'
34 // CHECK-NEXT:   ParmVarDecl {{.*}} x 'float'
35 // CHECK-NEXT:     CompoundStmt
36 // CHECK-NEXT:       ReturnStmt
37 // CHECK-NEXT:         CallExpr {{.*}} FPContractMode=0
38 
func_04(float x)39 int func_04(float x) {
40 #pragma STDC FP_CONTRACT ON
41   return x;
42 }
43 
44 // CHECK:      FunctionDecl {{.*}} func_04 'int (float)'
45 // CHECK-NEXT:   ParmVarDecl {{.*}} x 'float'
46 // CHECK-NEXT:   CompoundStmt
47 // CHECK-NEXT:     ReturnStmt
48 // CHECK-NEXT:       ImplicitCastExpr {{.*}} 'int' <FloatingToIntegral> FPContractMode=1
49 
func_05(double x)50 float func_05(double x) {
51 #pragma STDC FP_CONTRACT ON
52   return (float)x;
53 }
54 
55 // CHECK:      FunctionDecl {{.*}} func_05 'float (double)'
56 // CHECK-NEXT:   ParmVarDecl {{.*}} x 'double'
57 // CHECK-NEXT:   CompoundStmt
58 // CHECK-NEXT:     ReturnStmt
59 // CHECK-NEXT:       CStyleCastExpr {{.*}} FPContractMode=1
60 
func_06(double x)61 float func_06(double x) {
62 #pragma STDC FP_CONTRACT ON
63   return float(x);
64 }
65 
66 // CHECK:      FunctionDecl {{.*}} func_06 'float (double)'
67 // CHECK-NEXT:   ParmVarDecl {{.*}} x 'double'
68 // CHECK-NEXT:   CompoundStmt
69 // CHECK-NEXT:     ReturnStmt
70 // CHECK-NEXT:       CXXFunctionalCastExpr {{.*}} FPContractMode=1
71 
func_07(double x)72 float func_07(double x) {
73 #pragma STDC FP_CONTRACT ON
74   return static_cast<float>(x);
75 }
76 
77 // CHECK:      FunctionDecl {{.*}} func_07 'float (double)'
78 // CHECK-NEXT:   ParmVarDecl {{.*}} x 'double'
79 // CHECK-NEXT:   CompoundStmt
80 // CHECK-NEXT:     ReturnStmt
81 // CHECK-NEXT:       CXXStaticCastExpr {{.*}} FPContractMode=1
82 
83 #pragma STDC FENV_ROUND FE_DOWNWARD
84 
func_10(float x,float y)85 float func_10(float x, float y) {
86   return x + y;
87 }
88 
89 // CHECK-LABEL: FunctionDecl {{.*}} func_10 'float (float, float)'
90 // CHECK:         BinaryOperator {{.*}} 'float' '+' ConstRoundingMode=downward
91 
func_11(float x,float y)92 float func_11(float x, float y) {
93   if (x < 0) {
94     #pragma STDC FENV_ROUND FE_UPWARD
95     return x + y;
96   }
97   return x - y;
98 }
99 
100 // CHECK-LABEL: FunctionDecl {{.*}} func_11 'float (float, float)'
101 // CHECK:         BinaryOperator {{.*}} 'float' '+' ConstRoundingMode=upward
102 // CHECK:         BinaryOperator {{.*}} 'float' '-' ConstRoundingMode=downward
103 
104 
105 #pragma STDC FENV_ROUND FE_DYNAMIC
106 
func_12(float x,float y)107 float func_12(float x, float y) {
108   return x + y;
109 }
110 
111 // CHECK-LABEL: FunctionDecl {{.*}} func_12 'float (float, float)'
112 // CHECK:         BinaryOperator {{.*}} 'float' '+' ConstRoundingMode=dynamic
113 
114 #pragma STDC FENV_ROUND FE_TONEAREST
115 
func_13(float x,float y)116 float func_13(float x, float y) {
117   return x + y;
118 }
119 
120 // CHECK-LABEL: FunctionDecl {{.*}} func_13 'float (float, float)'
121 // CHECK:         BinaryOperator {{.*}} 'float' '+' ConstRoundingMode=tonearest
122 
123 
124 template <typename T>
func_14(T x,T y)125 T func_14(T x, T y) {
126 #pragma STDC FENV_ROUND FE_TOWARDZERO
127   return x + y;
128 }
129 
func_15(float x,float y)130 float func_15(float x, float y) {
131 #pragma STDC FENV_ROUND FE_DOWNWARD
132   return func_14(x, y);
133 }
134 
135 // CHECK-LABEL: FunctionTemplateDecl {{.*}} func_14
136 // CHECK:         FunctionDecl {{.*}} func_14 'T (T, T)'
137 // CHECK:           CompoundStmt
138 // CHECK-NEXT:        ReturnStmt
139 // CHECK-NEXT:          BinaryOperator {{.*}} '+' ConstRoundingMode=towardzero
140 // CHECK:         FunctionDecl {{.*}} func_14 'float (float, float)'
141 // CHECK:           CompoundStmt
142 // CHECK-NEXT:        ReturnStmt
143 // CHECK-NEXT:          BinaryOperator {{.*}} 'float' '+' ConstRoundingMode=towardzero
144 
func_16(float x,float y)145 float func_16(float x, float y) {
146 #pragma STDC FENV_ROUND FE_TOWARDZERO
147   if (x < 0) {
148 #pragma STDC FENV_ROUND FE_UPWARD
149     return x - y;
150   }
151   return x + y;
152 }
153 
154 // CHECK-LABEL: FunctionDecl {{.*}} func_16 'float (float, float)'
155 // CHECK:         CompoundStmt {{.*}} ConstRoundingMode=towardzero
156 // CHECK:           IfStmt
157 // CHECK:             CompoundStmt {{.*}} ConstRoundingMode=upward
158 // CHECK:               ReturnStmt
159 // CHECK:                 BinaryOperator {{.*}} ConstRoundingMode=upward
160 // CHECK:           ReturnStmt
161 // CHECK:             BinaryOperator {{.*}} ConstRoundingMode=towardzero
162 
func_17(float x,float y)163 float func_17(float x, float y) {
164 #pragma STDC FENV_ROUND FE_TOWARDZERO
165   if (x < 0) {
166 #pragma STDC FENV_ROUND FE_TOWARDZERO
167     return x - y;
168   }
169   return x + y;
170 }
171 
172 // CHECK-LABEL: FunctionDecl {{.*}} func_17 'float (float, float)'
173 // CHECK:         CompoundStmt {{.*}} ConstRoundingMode=towardzero
174 // CHECK:           IfStmt
175 // CHECK:             CompoundStmt {{.*}}
176 // CHECK:               ReturnStmt
177 // CHECK:                 BinaryOperator {{.*}} ConstRoundingMode=towardzero
178 // CHECK:           ReturnStmt
179 // CHECK:             BinaryOperator {{.*}} ConstRoundingMode=towardzero
180 
181 #pragma STDC FENV_ROUND FE_DOWNWARD
func_18(float x,float y)182 float func_18(float x, float y) {
183   return x + y;
184 }
185 
186 // CHECK-LABEL: FunctionDecl {{.*}} func_18 'float (float, float)'
187 // CHECK:         CompoundStmt {{.*}} ConstRoundingMode=downward
188 // CHECK:           ReturnStmt
189 // CHECK:             BinaryOperator {{.*}} ConstRoundingMode=downward
190 
191 #pragma float_control(precise, off)
192 
193 __attribute__((optnone))
func_19(float x,float y)194 float func_19(float x, float y) {
195   return x + y;
196 }
197 
198 // CHECK-LABEL: FunctionDecl {{.*}} func_19 'float (float, float)'
199 // CHECK:         CompoundStmt {{.*}} MathErrno=1
200 // CHECK:           ReturnStmt
201 // CHECK:             BinaryOperator {{.*}} 'float' '+' FPContractMode=1 ConstRoundingMode=downward MathErrno=1
202 
203 __attribute__((optnone))
func_20(float x,float y)204 float func_20(float x, float y) try {
205   return x + y;
206 } catch (...) {
207   return 1.0;
208 }
209 
210 // CHECK-LABEL: FunctionDecl {{.*}} func_20 'float (float, float)'
211 // CHECK:         CompoundStmt {{.*}} ConstRoundingMode=downward MathErrno=1
212 // CHECK:           ReturnStmt
213 // CHECK:             BinaryOperator {{.*}} 'float' '+' FPContractMode=1 ConstRoundingMode=downward MathErrno=1
214 
215 struct C21 {
216   C21(float x, float y);
a_methodC21217   __attribute__((optnone)) float a_method(float x, float y) {
218     return x * y;
219   }
220   float member;
221 };
222 
223 // CHECK-LABEL: CXXMethodDecl {{.*}} a_method 'float (float, float)'
224 // CHECK:         CompoundStmt {{.*}} FPContractMode=1 ConstRoundingMode=downward MathErrno=1
225 // CHECK:           ReturnStmt
226 // CHECK:             BinaryOperator {{.*}} 'float' '*' FPContractMode=1 ConstRoundingMode=downward MathErrno=1
227 
C21(float x,float y)228 __attribute__((optnone)) C21::C21(float x, float y) : member(x + y) {}
229 
230 // CHECK-LABEL: CXXConstructorDecl {{.*}} C21 'void (float, float)'
231 // CHECK:         CXXCtorInitializer {{.*}} 'member' 'float'
232 // CHECK:           BinaryOperator {{.*}} 'float' '+' FPContractMode=1 ConstRoundingMode=downward MathErrno=1
233 
234 template <typename T>
func_22(T x,T y)235 __attribute__((optnone)) T func_22(T x, T y) {
236   return x + y;
237 }
238 
239 // CHECK-LABEL: FunctionTemplateDecl {{.*}} func_22
240 // CHECK:         FunctionDecl {{.*}} func_22 'T (T, T)'
241 // CHECK:           CompoundStmt {{.*}} FPContractMode=1 ConstRoundingMode=downward MathErrno=1
242 // CHECK:             ReturnStmt
243 // CHECK:               BinaryOperator {{.*}} '+' FPContractMode=1 ConstRoundingMode=downward MathErrno=1
244 // CHECK:         FunctionDecl {{.*}} func_22 'float (float, float)'
245 // CHECK:           CompoundStmt {{.*}} FPContractMode=1 ConstRoundingMode=downward MathErrno=1
246 // CHECK:             ReturnStmt
247 // CHECK:               BinaryOperator {{.*}} 'float' '+' FPContractMode=1 ConstRoundingMode=downward MathErrno=1
248 
func_23(float x,float y)249 float func_23(float x, float y) {
250   return func_22(x, y);
251 }