egorzhdan created this revision. egorzhdan added a reviewer: zahiraam. Herald added a project: All. egorzhdan requested review of this revision. Herald added a project: clang. Herald added a subscriber: cfe-commits.
When running `clang -E -Ofast` on macOS, the `__FLT_EVAL_METHOD__` macro is `0`, which causes the following typedef to be emitted into the preprocessed source: `typedef float float_t`. However, when running `clang -c -Ofast`, `__FLT_EVAL_METHOD__` is `-1`, and `typedef long double float_t` is emitted. This causes build errors for certain projects, which are not reproducible when compiling from preprocessed source. The issue is that `__FLT_EVAL_METHOD__` is configured in `Sema::Sema` which is not executed when running in `-E` mode. This change moves that logic into the preprocessor initialization method, which is invoked correctly in `-E` mode. rdar://92748429 Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D128814 Files: clang/lib/Lex/Preprocessor.cpp clang/lib/Sema/Sema.cpp clang/test/Preprocessor/flt_eval_macro.cpp Index: clang/test/Preprocessor/flt_eval_macro.cpp =================================================================== --- clang/test/Preprocessor/flt_eval_macro.cpp +++ 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; } Index: clang/lib/Sema/Sema.cpp =================================================================== --- clang/lib/Sema/Sema.cpp +++ 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. Index: clang/lib/Lex/Preprocessor.cpp =================================================================== --- clang/lib/Lex/Preprocessor.cpp +++ 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() {
Index: clang/test/Preprocessor/flt_eval_macro.cpp =================================================================== --- clang/test/Preprocessor/flt_eval_macro.cpp +++ 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; } Index: clang/lib/Sema/Sema.cpp =================================================================== --- clang/lib/Sema/Sema.cpp +++ 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. Index: clang/lib/Lex/Preprocessor.cpp =================================================================== --- clang/lib/Lex/Preprocessor.cpp +++ 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() {
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits