This is an archive of the discontinued LLVM Phabricator instance.

[clang][Interp] Properly identify not-yet-defined functions
ClosedPublic

Authored by tbaeder on Jan 12 2023, 3:37 AM.

Details

Summary
Since we now handle functions without a body as well, we can't just use
getHasBody() anymore. Funtions that haven't been defined yet are those
that don't have a body *and* aren't valid.

Diff Detail

Event Timeline

tbaeder created this revision.Jan 12 2023, 3:37 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 12 2023, 3:37 AM
tbaeder requested review of this revision.Jan 12 2023, 3:37 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 12 2023, 3:37 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
tbaeder updated this revision to Diff 489020.Jan 13 2023, 8:05 AM

Just get the information whether the Function has a body directly from the FunctionDecl. This fixes referencing member functions that are defined later.

aaron.ballman added inline comments.Jan 31 2023, 5:26 AM
clang/test/AST/Interp/functions.cpp
166–182

Should we have some similar tests involving free functions as well?

Also, how does BodylessMemberFunction differ from F? They both look to be testing the same thing aside from return types.

Should we have a test that involves a defaulted special member function that is called explicitly? e.g., https://godbolt.org/z/nzjEcPMKG (Clang is correct here, GCC fails to catch the UB).

tbaeder added inline comments.Feb 3 2023, 1:39 AM
clang/test/AST/Interp/functions.cpp
166–182

We have a test for free-standing functions already higher up:

constexpr int f(); // expected-note {{declared here}} \
                   // ref-note {{declared here}}
[...]
constexpr int a() {
  return f();
}
constexpr int f() {
  return 5;
}
static_assert(a() == 5, "");

I think the two used to test something different, but now they test the same thing, yes. I'll remove one of them.

Should we have a test that involves a defaulted special member function that is called explicitly? e.g., https://godbolt.org/z/nzjEcPMKG (Clang is correct here, GCC fails to catch the UB).

That is also caught but the diagnostics are in the wrong place (and slightly different. The old interpreter emits "destruction of dereferenced null pointer" while the new one does "member call on dereferenced null pointer").

shafik added inline comments.Feb 16 2023, 7:01 PM
clang/lib/AST/Interp/ByteCodeExprGen.cpp
1468

Why the check for isContexpr()?

tbaeder added inline comments.Mar 1 2023, 12:25 AM
clang/lib/AST/Interp/ByteCodeExprGen.cpp
1468

isConstexpr() just returns whether the function is valid (i.e. was compiled successfully). hasBody() can still return false for a valid function, if the funcdecl had no body.

This revision is now accepted and ready to land.Mar 1 2023, 7:51 AM
shafik accepted this revision.Mar 1 2023, 9:18 PM

LGTM

This revision was landed with ongoing or failed builds.Mar 30 2023, 10:27 PM
This revision was automatically updated to reflect the committed changes.