This is an archive of the discontinued LLVM Phabricator instance.

[clang] Fix ternary operator in the second for loop statement
ClosedPublic

Authored by danlark on May 14 2021, 8:30 AM.

Details

Summary

Fix ternary operator in for loop argument, it was by mistake not set as CanBeForRangeDecl and led to incorrect codegen. It fixes https://bugs.llvm.org/show_bug.cgi?id=50038. I don't have commit rights. Danila Kutenin. kutdanila@yandex.ru

Diff Detail

Event Timeline

danlark requested review of this revision.May 14 2021, 8:30 AM
danlark created this revision.
Herald added a project: Restricted Project. · View Herald TranscriptMay 14 2021, 8:30 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript

This should have codegen, and maybe AST, tests.

clang/test/Parser/cxx2a-init-statement.cpp
18

This isn't cxx2a-specific isn't it?
Isn't this valid even in C99?

danlark added a comment.EditedMay 14 2021, 8:45 AM

This should have codegen, and maybe AST, tests.

Done

clang/test/Parser/cxx2a-init-statement.cpp
18

clang even in C++11 versions go to this branch and produce warning that this feature is c++2a, so it fails currently for everything but the culprit was https://github.com/llvm/llvm-project/commit/8baa50013c86c34a58d8327c5d1a043898b86398

danlark updated this revision to Diff 345458.May 14 2021, 9:07 AM
  • Add codegen test
danlark updated this revision to Diff 345460.May 14 2021, 9:15 AM
  • Add codegen and AST tests

Done, please, take a look

Thanks, nice catch!

Can we also add an assert when parsing a for statement that we actually find a range if the tentative parse said we were expecting one?

clang/test/CodeGenCXX/for-loop-init-ternary-operator-statement.cpp
2

We don't use -O3 tests for this kind of thing, to avoid depending on the behaviour of the optimizer; instead you should test that the (unoptimized) IR generated by clang is correct.

clang/test/PCH/for-loop-init-ternary-operator-statement.cpp
6

This ast-print output looks wrong; I assume that's an unrelated bug?

danlark retitled this revision from [clang] Fix ternary operator in second for loop argument to [clang] Fix ternary operator in second for loop statement.May 14 2021, 1:02 PM
danlark retitled this revision from [clang] Fix ternary operator in second for loop statement to [clang] Fix ternary operator in the second for loop statement.
danlark updated this revision to Diff 345536.May 14 2021, 1:02 PM
danlark marked 3 inline comments as done.
  • Add codegen and AST tests
clang/test/CodeGenCXX/for-loop-init-ternary-operator-statement.cpp
2

Done.

clang/test/PCH/for-loop-init-ternary-operator-statement.cpp
6

Yes, I think so, in a buggy version x as a second parameter is missing so at least it tests correctly

danlark updated this revision to Diff 345561.May 14 2021, 2:15 PM
  • Add assert of finding a for range declaration

Thanks, nice catch!

Can we also add an assert when parsing a for statement that we actually find a range if the tentative parse said we were expecting one?

Done.

rsmith accepted this revision.May 14 2021, 2:34 PM
rsmith added inline comments.
clang/lib/Parse/ParseExprCXX.cpp
2036

If parsing failed, I don't think this assertion should be expected to hold: error recovery after the parse error might not have interpreted the : as the start of the range expression.

This revision is now accepted and ready to land.May 14 2021, 2:34 PM
danlark updated this revision to Diff 345568.May 14 2021, 2:37 PM
  • Address comment from Richard
danlark marked an inline comment as done.May 14 2021, 2:37 PM
danlark added a comment.EditedMay 14 2021, 4:16 PM

Failure looks unrelated, please submit as I don't have commit rights

This revision was landed with ongoing or failed builds.May 16 2021, 10:43 AM
This revision was automatically updated to reflect the committed changes.