Skip to content

Commit 0baf85c

Browse files
committed
[clang] Set FP options in Sema when instantiating CompoundStmt
When an expression is instantiated, TreeTransform skips ImplicitCastExpr nodes, assuming they are recreated when the instantiated expression is built. It breaks functions that use non-default floating-point options, because they are kept in these ImplicitCastExprs. In this case the recreated ImplicitCastExpr takes FP options from the current Sema state and not from AST node. To fix this issue the FP options in Sema object are set when a compound statement is cloned in TreeTransform. This change fixes llvm#64605 ([Regression 16 -> 17] Template instantiation ignores FENV_ACCESS being ON for both definition and instantiation). Differential Revision: https://reviews.llvm.org/D158158
1 parent bf69217 commit 0baf85c

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

clang/lib/Sema/TreeTransform.h

+4
Original file line numberDiff line numberDiff line change
@@ -7491,6 +7491,10 @@ StmtResult
74917491
TreeTransform<Derived>::TransformCompoundStmt(CompoundStmt *S,
74927492
bool IsStmtExpr) {
74937493
Sema::CompoundScopeRAII CompoundScope(getSema());
7494+
Sema::FPFeaturesStateRAII FPSave(getSema());
7495+
if (S->hasStoredFPFeatures())
7496+
getSema().resetFPOptions(
7497+
S->getStoredFPFeatures().applyOverrides(getSema().getLangOpts()));
74947498

74957499
const Stmt *ExprResult = S->getStmtExprResult();
74967500
bool SubStmtInvalid = false;

clang/test/SemaCXX/template-64605.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: %clang_cc1 -ast-dump -ast-dump-filter=b_64605 %s | FileCheck %s
2+
3+
// https://github.com/llvm/llvm-project/issues/64605
4+
5+
#pragma STDC FENV_ACCESS ON
6+
template <typename>
7+
int b_64605() {
8+
int x;
9+
if ((float)0xFFFFFFFF != (float)0x100000000) {
10+
x = 1;
11+
}
12+
return x;
13+
}
14+
int f() { return b_64605<void>(); }
15+
16+
// CHECK: ImplicitCastExpr {{.*}} 'float' <IntegralToFloating> RoundingMath=1 AllowFEnvAccess=1
17+
// CHECK-NEXT: IntegerLiteral {{.*}} 4294967295
18+
19+
// CHECK: FunctionDecl {{.*}} b_64605 'int ()' implicit_instantiation
20+
// CHECK-NEXT: TemplateArgument type 'void'
21+
22+
// CHECK: ImplicitCastExpr {{.*}} 'float' <IntegralToFloating> RoundingMath=1 AllowFEnvAccess=1
23+
// CHECK-NEXT: IntegerLiteral {{.*}} 4294967295

0 commit comments

Comments
 (0)