[PATCH] D158158: [clang] Set FP options in Sema when instantiating CompoundStmt

Serge Pavlov via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 17 01:42:59 PDT 2023


sepavloff created this revision.
sepavloff added reviewers: rjmccall, aaron.ballman, efriedma, zahiraam.
Herald added a project: All.
sepavloff requested review of this revision.
Herald added a project: clang.

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 FP options in Sema object are set when a compound
statement is cloned in TreeTransform.

This change fixes https://github.com/llvm/llvm-project/issues/64605
([Regression 16 -> 17] Template instantiation ignores FENV_ACCESS being
ON for both definition and instantiation).


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D158158

Files:
  clang/lib/Sema/TreeTransform.h
  clang/test/SemaCXX/template-64605.cpp


Index: clang/test/SemaCXX/template-64605.cpp
===================================================================
--- /dev/null
+++ clang/test/SemaCXX/template-64605.cpp
@@ -0,0 +1,23 @@
+// RUN: %clang_cc1 -ast-dump -ast-dump-filter=b_64605 %s | FileCheck %s
+
+// https://github.com/llvm/llvm-project/issues/64605
+
+#pragma STDC FENV_ACCESS ON
+template <typename>
+int b_64605() {
+  int x;
+  if ((float)0xFFFFFFFF != (float)0x100000000) {
+    x = 1;
+  }
+  return x;
+}
+int f() { return b_64605<void>(); }
+
+// CHECK:      ImplicitCastExpr {{.*}} 'float' <IntegralToFloating> RoundingMath=1 AllowFEnvAccess=1
+// CHECK-NEXT: IntegerLiteral {{.*}} 4294967295
+
+// CHECK:      FunctionDecl {{.*}} b_64605 'int ()' implicit_instantiation
+// CHECK-NEXT: TemplateArgument type 'void'
+
+// CHECK:      ImplicitCastExpr {{.*}} 'float' <IntegralToFloating> RoundingMath=1 AllowFEnvAccess=1 
+// CHECK-NEXT: IntegerLiteral {{.*}} 4294967295
Index: clang/lib/Sema/TreeTransform.h
===================================================================
--- clang/lib/Sema/TreeTransform.h
+++ clang/lib/Sema/TreeTransform.h
@@ -7491,6 +7491,10 @@
 TreeTransform<Derived>::TransformCompoundStmt(CompoundStmt *S,
                                               bool IsStmtExpr) {
   Sema::CompoundScopeRAII CompoundScope(getSema());
+  Sema::FPFeaturesStateRAII FPSave(getSema());
+  if (S->hasStoredFPFeatures())
+    getSema().resetFPOptions(
+        S->getStoredFPFeatures().applyOverrides(getSema().getLangOpts()));
 
   const Stmt *ExprResult = S->getStmtExprResult();
   bool SubStmtInvalid = false;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D158158.551044.patch
Type: text/x-patch
Size: 1600 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230817/a4c40c65/attachment-0001.bin>


More information about the cfe-commits mailing list