This is an archive of the discontinued LLVM Phabricator instance.

[clang][Interp] Implement ConditionalOperators
ClosedPublic

Authored by tbaeder on Sep 28 2022, 3:50 AM.

Details

Summary

Implement visiting ConditionalOperators.

This also makes it possible to enable the first existing test case with the new constant interpreter. Although I'm not sure if I should actually do that, I quite enjoy just running ninja check-clang-ast-interp and run all the relevant tests in under 2 seconds.

Diff Detail

Event Timeline

tbaeder created this revision.Sep 28 2022, 3:50 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 28 2022, 3:50 AM
tbaeder requested review of this revision.Sep 28 2022, 3:50 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 28 2022, 3:50 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
shafik added inline comments.Sep 28 2022, 10:46 AM
clang/lib/AST/Interp/ByteCodeExprGen.cpp
360

Maybe I am misunderstanding what this is doing but can't we just check the result of the condition and then just visit either the true or false branch?

tbaeder added inline comments.Sep 28 2022, 10:52 AM
clang/lib/AST/Interp/ByteCodeExprGen.cpp
360

That's what this code is doing in the "eval case" (i.e Emitter = EvalEmitter), but when we're emitting bytecode, we can of course not just check the condition but need to do that when the byte code is emitted.

That is, if I understand the question correctly.

shafik added inline comments.Sep 28 2022, 1:28 PM
clang/lib/AST/Interp/ByteCodeExprGen.cpp
360

Makes sense, just me being dense then.

aaron.ballman added inline comments.Sep 29 2022, 12:29 PM
clang/lib/AST/Interp/ByteCodeExprGen.h
91

Do we want to handle BinaryConditionalOperator at the same time? (IIRC, you should be able to use AbstractConditionalOperator here and the logic is handled for you automagically for both constructs.) e.g.,

foo() ? 12 : bar  // C conditional operator, has the value 12 if foo() is nonzero and bar otherwise
foo() ? : bar // GNU "missing middle" conditional operator, has the value of foo() if nonzero and bar otherwise
tbaeder updated this revision to Diff 464165.Sep 30 2022, 12:33 AM
tbaeder marked 3 inline comments as done.
tbaeder added inline comments.Sep 30 2022, 12:36 AM
clang/lib/AST/Interp/ByteCodeExprGen.h
91

Yep, that just worked

This revision is now accepted and ready to land.Sep 30 2022, 5:54 AM
This revision was landed with ongoing or failed builds.Oct 14 2022, 4:28 AM
This revision was automatically updated to reflect the committed changes.