sepavloff created this revision. sepavloff added reviewers: rsmith, rjmccall, aaron.ballman, efriedma. Herald added a project: All. sepavloff requested review of this revision. Herald added a project: clang.
Previously function template instantiations occurred with FP options that were in effect at the end of translation unit. It was an problem for late template parsing as these options were used as attributes of AST nodes and may result in crash. With this change the FP options are reset to the set defined by command line options before every function instatntiation. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D143241 Files: clang/include/clang/Sema/Sema.h clang/lib/Sema/SemaTemplateInstantiateDecl.cpp clang/test/CodeGen/fp-template.cpp Index: clang/test/CodeGen/fp-template.cpp =================================================================== --- /dev/null +++ clang/test/CodeGen/fp-template.cpp @@ -0,0 +1,18 @@ +// RUN: %clang_cc1 -triple x86_64-linux-gnu -fdelayed-template-parsing -emit-llvm -o - %s | FileCheck %s + +template <typename Ty> +Ty func_01(Ty x, Ty y) { + return x + y; +} + +float func_02(float x, float y) { + return func_01(x, y); +} + +// This pragma sets non-default rounding mode and then delayed parsing occurs. +// Check that the parsing uses FP options defined by command line options and +// not by this pragma. +#pragma STDC FENV_ROUND FE_TOWARDZERO + +// CHECK-LABEL: define {{.*}} float @_Z7func_01IfET_S0_S0_ +// CHECK: %add = fadd float %0, %1 Index: clang/lib/Sema/SemaTemplateInstantiateDecl.cpp =================================================================== --- clang/lib/Sema/SemaTemplateInstantiateDecl.cpp +++ clang/lib/Sema/SemaTemplateInstantiateDecl.cpp @@ -4903,6 +4903,11 @@ /*Enabled=*/Recursive); LocalEagerInstantiationScope LocalInstantiations(*this); + // Reset FP option to the state specified by command line. + FpPragmaStack.clear(); + FPFeaturesStateRAII SavedFPFeatures(*this); + CurFPFeatures = FPOptions(getLangOpts()); + // Call the LateTemplateParser callback if there is a need to late parse // a templated function definition. if (!Pattern && PatternDecl->isLateTemplateParsed() && Index: clang/include/clang/Sema/Sema.h =================================================================== --- clang/include/clang/Sema/Sema.h +++ clang/include/clang/Sema/Sema.h @@ -660,6 +660,11 @@ bool hasValue() const { return CurrentValue != DefaultValue; } + void clear() { + Stack.clear(); + CurrentValue = DefaultValue; + } + SmallVector<Slot, 2> Stack; ValueType DefaultValue; // Value used for PSK_Reset action. ValueType CurrentValue;
Index: clang/test/CodeGen/fp-template.cpp =================================================================== --- /dev/null +++ clang/test/CodeGen/fp-template.cpp @@ -0,0 +1,18 @@ +// RUN: %clang_cc1 -triple x86_64-linux-gnu -fdelayed-template-parsing -emit-llvm -o - %s | FileCheck %s + +template <typename Ty> +Ty func_01(Ty x, Ty y) { + return x + y; +} + +float func_02(float x, float y) { + return func_01(x, y); +} + +// This pragma sets non-default rounding mode and then delayed parsing occurs. +// Check that the parsing uses FP options defined by command line options and +// not by this pragma. +#pragma STDC FENV_ROUND FE_TOWARDZERO + +// CHECK-LABEL: define {{.*}} float @_Z7func_01IfET_S0_S0_ +// CHECK: %add = fadd float %0, %1 Index: clang/lib/Sema/SemaTemplateInstantiateDecl.cpp =================================================================== --- clang/lib/Sema/SemaTemplateInstantiateDecl.cpp +++ clang/lib/Sema/SemaTemplateInstantiateDecl.cpp @@ -4903,6 +4903,11 @@ /*Enabled=*/Recursive); LocalEagerInstantiationScope LocalInstantiations(*this); + // Reset FP option to the state specified by command line. + FpPragmaStack.clear(); + FPFeaturesStateRAII SavedFPFeatures(*this); + CurFPFeatures = FPOptions(getLangOpts()); + // Call the LateTemplateParser callback if there is a need to late parse // a templated function definition. if (!Pattern && PatternDecl->isLateTemplateParsed() && Index: clang/include/clang/Sema/Sema.h =================================================================== --- clang/include/clang/Sema/Sema.h +++ clang/include/clang/Sema/Sema.h @@ -660,6 +660,11 @@ bool hasValue() const { return CurrentValue != DefaultValue; } + void clear() { + Stack.clear(); + CurrentValue = DefaultValue; + } + SmallVector<Slot, 2> Stack; ValueType DefaultValue; // Value used for PSK_Reset action. ValueType CurrentValue;
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits