This is an archive of the discontinued LLVM Phabricator instance.

[clang][CGExprConstant] handle FunctionToPointerDecay
Needs ReviewPublic

Authored by nickdesaulniers on Jul 27 2023, 1:34 PM.

Details

Reviewers
efriedma
shafik
Summary

Consider the following code:

void foo (void) {}
void (*bar)(void) = foo;

And the corresponding AST:

|-FunctionDecl 0x561e9cf12728 <x.c:14:1, col:18> col:6 used foo 'void (void)'
| `-CompoundStmt 0x561e9cf12818 <col:17, col:18>
`-VarDecl 0x561e9cf12958 <line:15:1, col:21> col:8 bar 'void (*)(void)' cinit
  `-ImplicitCastExpr 0x561e9cf129e0 <col:21> 'void (*)(void)' <FunctionToPointerDecay>
    `-DeclRefExpr 0x561e9cf129c0 <col:21> 'void (void)' Function 0x561e9cf12728 'foo' 'void (void)'

The ImplicitCastExpr of FunctionToPointerDecay is obviously const.
Lookup the corresponding llvm::Function in the Module by name.

Diff Detail

Event Timeline

Herald added a project: Restricted Project. · View Herald TranscriptJul 27 2023, 1:34 PM
nickdesaulniers requested review of this revision.Jul 27 2023, 1:34 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 27 2023, 1:34 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript

Makes sense but I want @efriedma to look at as well.

efriedma added inline comments.Aug 2 2023, 4:00 PM
clang/lib/CodeGen/CGExprConstant.cpp
1128

I think I'd prefer to continue treating an undecayed function as an "lvalue", to keep things straightforward. To that end, I'd prefer a separate "ConstLValueExprEmitter", or something like that, so ConstExprEmitter only visits rvalues.

The new emitter should reuse code from ConstLValueExprEmitter where possible.

1247

You 100% can't use VD->getName() like this; I'm shocked this isn't causing any test failures. GetAddrOfFunction() resolves names correctly.