diff --git a/clang/lib/Lex/Preprocessor.cpp b/clang/lib/Lex/Preprocessor.cpp --- a/clang/lib/Lex/Preprocessor.cpp +++ b/clang/lib/Lex/Preprocessor.cpp @@ -206,6 +206,18 @@ // Initialize the __FTL_EVAL_METHOD__ macro to the TargetInfo. setTUFPEvalMethod(getTargetInfo().getFPEvalMethod()); + + if (getLangOpts().getFPEvalMethod() == LangOptions::FEM_UnsetOnCommandLine) + // Use setting from TargetInfo. + setCurrentFPEvalMethod(SourceLocation(), Target.getFPEvalMethod()); + else + // Set initial value of __FLT_EVAL_METHOD__ from the command line. + setCurrentFPEvalMethod(SourceLocation(), getLangOpts().getFPEvalMethod()); + // When `-ffast-math` option is enabled, it triggers several driver math + // options to be enabled. Among those, only one the following two modes + // affect the eval-method: reciprocal or reassociate. + if (getLangOpts().AllowFPReassoc || getLangOpts().AllowRecip) + setCurrentFPEvalMethod(SourceLocation(), LangOptions::FEM_Indeterminable); } void Preprocessor::InitializeForModelFile() { diff --git a/clang/lib/Sema/Sema.cpp b/clang/lib/Sema/Sema.cpp --- a/clang/lib/Sema/Sema.cpp +++ b/clang/lib/Sema/Sema.cpp @@ -249,21 +249,8 @@ SemaPPCallbackHandler = Callbacks.get(); PP.addPPCallbacks(std::move(Callbacks)); SemaPPCallbackHandler->set(*this); - if (getLangOpts().getFPEvalMethod() == LangOptions::FEM_UnsetOnCommandLine) - // Use setting from TargetInfo. - PP.setCurrentFPEvalMethod(SourceLocation(), - ctxt.getTargetInfo().getFPEvalMethod()); - else - // Set initial value of __FLT_EVAL_METHOD__ from the command line. - PP.setCurrentFPEvalMethod(SourceLocation(), - getLangOpts().getFPEvalMethod()); + CurFPFeatures.setFPEvalMethod(PP.getCurrentFPEvalMethod()); - // When `-ffast-math` option is enabled, it triggers several driver math - // options to be enabled. Among those, only one the following two modes - // affect the eval-method: reciprocal or reassociate. - if (getLangOpts().AllowFPReassoc || getLangOpts().AllowRecip) - PP.setCurrentFPEvalMethod(SourceLocation(), - LangOptions::FEM_Indeterminable); } // Anchor Sema's type info to this TU. diff --git a/clang/test/Preprocessor/flt_eval_macro.cpp b/clang/test/Preprocessor/flt_eval_macro.cpp --- a/clang/test/Preprocessor/flt_eval_macro.cpp +++ b/clang/test/Preprocessor/flt_eval_macro.cpp @@ -16,6 +16,9 @@ // RUN: %clang_cc1 -E -dM -triple=arm64_32-apple-ios -target-feature -sse \ // RUN: %s -o - | FileCheck %s -strict-whitespace +// RUN: %clang_cc1 -E -dM -triple=x86_64-apple-macos13.0 -ffast-math \ +// RUN: %s -o - | FileCheck %s -check-prefix=CHECK-MINUS-ONE -strict-whitespace + // RUN: %clang_cc1 -E -dM -triple i386-pc-windows -target-cpu pentium4 %s -o - \ // RUN: | FileCheck %s -strict-whitespace @@ -35,7 +38,9 @@ #define __GLIBC_FLT_EVAL_METHOD 2 #endif -#if __GLIBC_FLT_EVAL_METHOD == 0 || __GLIBC_FLT_EVAL_METHOD == 16 +#if __GLIBC_FLT_EVAL_METHOD == -1 +#define Name "MinusOne" +#elif __GLIBC_FLT_EVAL_METHOD == 0 || __GLIBC_FLT_EVAL_METHOD == 16 #define Name "One" #elif __GLIBC_FLT_EVAL_METHOD == 1 #define Name "Two" @@ -59,6 +64,7 @@ int foo() { // CHECK: #define Name "One" + // CHECK-MINUS-ONE: #define Name "MinusOne" // EXT: #define Name "Three" return Name; }