question/problem about parsing pragma's following a function definition

I’m working on a patch to add a new pragma. https://reviews.llvm.org/D72841

Some forms of the pragma are only allowed at file scope. When parsing the pragma I used this expression to test if at file scope:

(Actions.getCurScope()->getParent() == nullptr)

I also tried

(Actions.CurContext->isTranslationUnit())

Both methods failed to recognize that the token following the end of the function body is at file scope. In the debugger I can see that after the pragma parsing is complete, there is a call to ConsumeBrace which has been called in turn by consumeClose<ParseCompoundStatementBody<ParseFunctionStatementBody

So this means that the function body isn’t closed up until after all the following pragma’s have been parsed. Is this the right way to do things or should the function be closed when the brace is seen? It’s surprising to me. Thanks in advance for any insight –Melanie

Void f() {

Do stuff;

}

#pragma float_control(push) <<< The push pragma should only appear at file scope but the function scope hasn’t been closed when the pragma is parsed.

When parser consumes }, it tries to pump next available token. If it sees a pragma, it tries to parse the pragma and convert it into an annotation token. So PragmaFloatControlHandler::HandlePragma occurs in the scope of the current function. It cannot be changed easily because after ‘}’ is consumed, the action ActOnCompoundStmt and ActOnFinishFunctionBody should be executed and they require proper scope/context.

I would propose you to try moving the check into Sema, into the call of Sema::ActOnPragmaFloatControl. It executes latter and CurContext must already be updated.