Skip to content

Commit 463a99a

Browse files
committed
Extend the solution for more use cases
1 parent 25a7180 commit 463a99a

File tree

7 files changed

+96
-0
lines changed

7 files changed

+96
-0
lines changed

clang/lib/Parse/ParseCXXInlineMethods.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,11 @@ void Parser::ParseLexedMethodDef(LexedMethod &LM) {
603603
// to be re-used for method bodies as well.
604604
ParseScope FnScope(this, Scope::FnScope | Scope::DeclScope |
605605
Scope::CompoundStmtScope);
606+
607+
// Some function attributes (like OptimizeNoneAttr) affect FP options.
608+
Sema::FPFeaturesStateRAII SaveFPFeatures(Actions);
609+
Actions.applyFunctionAttributesBeforeParsingBody(LM.D);
610+
606611
Actions.ActOnStartOfFunctionDef(getCurScope(), LM.D);
607612

608613
if (Tok.is(tok::kw_try)) {

clang/lib/Parse/ParseObjc.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3736,6 +3736,9 @@ void Parser::ParseLexedObjCMethodDefs(LexedMethod &LM, bool parseMethod) {
37363736
ParseScope BodyScope(this, (parseMethod ? Scope::ObjCMethodScope : 0) |
37373737
Scope::FnScope | Scope::DeclScope |
37383738
Scope::CompoundStmtScope);
3739+
// Some function attributes (like OptimizeNoneAttr) affect FP options.
3740+
Sema::FPFeaturesStateRAII SaveFPFeatures(Actions);
3741+
Actions.applyFunctionAttributesBeforeParsingBody(LM.D);
37393742

37403743
// Tell the actions module that we have entered a method or c-function definition
37413744
// with the specified Declarator for the method/function.

clang/lib/Parse/ParseTemplate.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1651,6 +1651,10 @@ void Parser::ParseLateTemplatedFuncDef(LateParsedTemplate &LPT) {
16511651
// Recreate the containing function DeclContext.
16521652
Sema::ContextRAII FunctionSavedContext(Actions, FunD->getLexicalParent());
16531653

1654+
// Some function attributes (like OptimizeNoneAttr) affect FP options.
1655+
Sema::FPFeaturesStateRAII SaveFPFeatures(Actions);
1656+
Actions.applyFunctionAttributesBeforeParsingBody(LPT.D);
1657+
16541658
Actions.ActOnStartOfFunctionDef(getCurScope(), FunD);
16551659

16561660
if (Tok.is(tok::kw_try)) {

clang/lib/Sema/SemaDecl.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15911,6 +15911,10 @@ Decl *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, Decl *D,
1591115911
}
1591215912

1591315913
void Sema::applyFunctionAttributesBeforeParsingBody(Decl *FD) {
15914+
if (!FD || FD->isInvalidDecl())
15915+
return;
15916+
if (auto *TD = dyn_cast<FunctionTemplateDecl>(FD))
15917+
FD = TD->getTemplatedDecl();
1591415918
if (FD && FD->hasAttr<OptimizeNoneAttr>()) {
1591515919
FPOptionsOverride FPO;
1591615920
FPO.setDisallowOptimizations();

clang/test/AST/ast-dump-fpfeatures.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,38 @@ float func_20(float x, float y) try {
214214

215215
struct C21 {
216216
C21(float x, float y);
217+
__attribute__((optnone)) float a_method(float x, float y) {
218+
return x * y;
219+
}
217220
float member;
218221
};
219222

223+
// CHECK-LABEL: CXXMethodDecl {{.*}} a_method 'float (float, float)'
224+
// CHECK: CompoundStmt {{.*}} ConstRoundingMode=downward MathErrno=1
225+
// CHECK: ReturnStmt
226+
// CHECK: BinaryOperator {{.*}} 'float' '*' ConstRoundingMode=downward MathErrno=1
227+
220228
__attribute__((optnone)) C21::C21(float x, float y) : member(x + y) {}
221229

222230
// CHECK-LABEL: CXXConstructorDecl {{.*}} C21 'void (float, float)'
223231
// CHECK: CXXCtorInitializer {{.*}} 'member' 'float'
224232
// CHECK: BinaryOperator {{.*}} 'float' '+' ConstRoundingMode=downward MathErrno=1
233+
234+
template <typename T>
235+
__attribute__((optnone)) T func_22(T x, T y) {
236+
return x + y;
237+
}
238+
239+
// CHECK-LABEL: FunctionTemplateDecl {{.*}} func_22
240+
// CHECK: FunctionDecl {{.*}} func_22 'T (T, T)'
241+
// CHECK: CompoundStmt {{.*}} ConstRoundingMode=downward MathErrno=1
242+
// CHECK: ReturnStmt
243+
// CHECK: BinaryOperator {{.*}} '+' ConstRoundingMode=downward MathErrno=1
244+
// CHECK: FunctionDecl {{.*}} func_22 'float (float, float)'
245+
// CHECK: CompoundStmt {{.*}} ConstRoundingMode=downward MathErrno=1
246+
// CHECK: ReturnStmt
247+
// CHECK: BinaryOperator {{.*}} 'float' '+' ConstRoundingMode=downward MathErrno=1
248+
249+
float func_23(float x, float y) {
250+
return func_22(x, y);
251+
}

clang/test/AST/ast-dump-fpfeatures.m

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Test without serialization:
2+
// RUN: %clang_cc1 -fsyntax-only -triple x86_64-pc-linux -ast-dump %s \
3+
// RUN: | FileCheck --strict-whitespace %s
4+
5+
// Test with serialization:
6+
// RUN: %clang_cc1 -triple x86_64-pc-linux -emit-pch -o %t %s
7+
// RUN: %clang_cc1 -x objective-c -triple x86_64-pc-linux -include-pch %t -ast-dump-all /dev/null \
8+
// RUN: | sed -e "s/ <undeserialized declarations>//" -e "s/ imported//" \
9+
// RUN: | FileCheck --strict-whitespace %s
10+
11+
12+
@interface Adder
13+
- (float) sum: (float)x with: (float)y __attribute((optnone));
14+
@end
15+
16+
#pragma float_control(precise, off)
17+
18+
@implementation Adder
19+
- (float) sum: (float)x with: (float)y __attribute((optnone)) {
20+
return x + y;
21+
}
22+
23+
@end
24+
25+
// CHECK-LABEL: ObjCImplementationDecl {{.*}} Adder
26+
// CHECK: ObjCMethodDecl {{.*}} - sum:with: 'float'
27+
// CHECK: CompoundStmt {{.*}} MathErrno=1
28+
// CHECK-NEXT: ReturnStmt
29+
// CHECK-NEXT: BinaryOperator {{.*}} 'float' '+' MathErrno=1
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// RUN: %clang_cc1 -fsyntax-only -triple x86_64-pc-linux -std=c++11 -fcxx-exceptions -fdelayed-template-parsing -ast-dump %s \
2+
// RUN: | FileCheck %s
3+
4+
#pragma STDC FENV_ROUND FE_DOWNWARD
5+
#pragma float_control(precise, off)
6+
7+
template <typename T>
8+
__attribute__((optnone)) T func_22(T x, T y) {
9+
return x + y;
10+
}
11+
12+
// CHECK-LABEL: FunctionTemplateDecl {{.*}} func_22
13+
// CHECK: FunctionDecl {{.*}} func_22 'T (T, T)'
14+
// CHECK: CompoundStmt {{.*}} ConstRoundingMode=downward MathErrno=1
15+
// CHECK: ReturnStmt
16+
// CHECK: BinaryOperator {{.*}} '+' ConstRoundingMode=downward MathErrno=1
17+
// CHECK: FunctionDecl {{.*}} func_22 'float (float, float)'
18+
// CHECK: CompoundStmt {{.*}} ConstRoundingMode=downward MathErrno=1
19+
// CHECK: ReturnStmt
20+
// CHECK: BinaryOperator {{.*}} 'float' '+' ConstRoundingMode=downward MathErrno=1
21+
22+
float func_23(float x, float y) {
23+
return func_22(x, y);
24+
}

0 commit comments

Comments
 (0)