Skip to content

Commit aebee69

Browse files
spavlofftru
authored andcommitted
[clang] Do not clear FP pragma stack when instantiating functions (#70646)
When instantiation function, a call to Sema::resetFPOption was used to set the FP options associated with AST node. However this function also cleared FP pragma stack, and it is incorrect. Template instantiation takes place on AST representation and semantic information like the FP pragma stack should not affect it. This was a reason for miscompilation in some cases. To make the Sema interface more consistent, now `resetFPOptions` does not clear FP pragma stack anymore. It is cleared in `FpPragmaStackSaveRAII`, which is used in parsing only. This change must fix #69717 (Problems with float_control pragma stack in Clang 17.x). (cherry picked from commit f6f625f)
1 parent 529aa6e commit aebee69

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

clang/include/clang/Sema/Sema.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -710,9 +710,13 @@ class Sema final {
710710
return result;
711711
}
712712

713+
// Saves the current floating-point pragma stack and clear it in this Sema.
713714
class FpPragmaStackSaveRAII {
714715
public:
715-
FpPragmaStackSaveRAII(Sema &S) : S(S), SavedStack(S.FpPragmaStack) {}
716+
FpPragmaStackSaveRAII(Sema &S)
717+
: S(S), SavedStack(std::move(S.FpPragmaStack)) {
718+
S.FpPragmaStack.Stack.clear();
719+
}
716720
~FpPragmaStackSaveRAII() { S.FpPragmaStack = std::move(SavedStack); }
717721

718722
private:
@@ -722,7 +726,6 @@ class Sema final {
722726

723727
void resetFPOptions(FPOptions FPO) {
724728
CurFPFeatures = FPO;
725-
FpPragmaStack.Stack.clear();
726729
FpPragmaStack.CurrentValue = FPO.getChangesFrom(FPOptions(LangOpts));
727730
}
728731

clang/test/Sema/PR69717.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// RUN: %clang_cc1 -verify -fsyntax-only %s
2+
// expected-no-diagnostics
3+
4+
// Testcase for https://github.com/llvm/llvm-project/issues/69717
5+
6+
#pragma float_control(precise, on, push)
7+
8+
template<typename T>
9+
constexpr T multi(T x, T y) {
10+
return x * y;
11+
}
12+
13+
int multi_i(int x, int y) {
14+
return multi<int>(x, y);
15+
}
16+
17+
#pragma float_control(pop)

0 commit comments

Comments
 (0)