This is an archive of the discontinued LLVM Phabricator instance.

[clang] Handle 128-bits IntegerLiterals in StmtPrinter
ClosedPublic

Authored by davidstone on Jun 30 2020, 7:57 AM.

Details

Summary

This fixes PR35677: "int128_t or uint128_t as non-type template parameter causes crash when considering invalid constructor".

Diff Detail

Event Timeline

davidstone created this revision.Jun 30 2020, 7:57 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 30 2020, 7:57 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
echristo edited reviewers, added: HAPPY; removed: klimek.Jun 30 2020, 4:53 PM
riccibruno added inline comments.
clang/lib/AST/StmtPrinter.cpp
1159

i128 and Ui128 are not valid integer literal suffix. The output of StmtPrinter is intended to be valid C++. Unfortunately here I think that your only choice is to print the high and low parts separately.

davidstone marked an inline comment as done.Jul 1 2020, 8:24 AM
davidstone added inline comments.
clang/lib/AST/StmtPrinter.cpp
1159

I'm confused. i8, Ui8, i16, and Ui16 are also not valid C++ suffixes, but just a few lines up we use those. What am I missing here?

riccibruno added inline comments.
clang/lib/AST/StmtPrinter.cpp
1159

The literal suffixes [u]i8, [u]i16, [u]i32, and [u]i64 are an MSVC extension (see NumericLiteralParser::NumericLiteralParser in Lex/LiteralSupport.cpp).

This does not explain why they are used even in non-ms compatibility mode
but at least there is some reason for their existence.

However I don't think that MSVC supports 128-bits integers (?), and clang certainly
does not support [u]i128 so there is no reason to use them.

@aaron.ballman Do you know why are these suffixes used outside of ms-compatibility mode?

riccibruno added inline comments.
clang/lib/AST/StmtPrinter.cpp
1159

This does not explain why they are used even in non-ms compatibility mode
but at least there is some reason for their existence.

Let's just ask the author @majnemer

aaron.ballman added inline comments.Jul 2 2020, 4:10 AM
clang/lib/AST/StmtPrinter.cpp
1159

@aaron.ballman Do you know why are these suffixes used outside of ms-compatibility mode?

Our pretty printing is *supposed* to generate valid code, but we get it wrong fairly often and I don't think we've ever promised (let alone tested) that you can use this output to compile code (and get the same results). I think that's more of a stretch goal. That said, the pretty printer should probably be looking at the language options to decide whether it wants to output those suffixes or not.

As for historical context, I think the thread starting here is relevant: http://lists.llvm.org/pipermail/cfe-dev/2012-September/024423.html

davidstone marked an inline comment as done.Jul 13 2020, 10:33 AM
davidstone added inline comments.
clang/lib/AST/StmtPrinter.cpp
1159

So what is the next step for this patch?

aaron.ballman added inline comments.Jul 16 2020, 10:54 AM
clang/lib/AST/StmtPrinter.cpp
1159

So what is the next step for this patch?

I don't think we should print any suffix for these, similar to how we handle int. There is no 128-bit suffix we support.

In addition to Aaron's suggestion, can you also change the diff's title to something a little bit more informative. Suggestion: "[clang] Handle 128-bits IntegerLiterals in StmtPrinter", and say in the description that this addresses PR35677.

This comment was removed by davidstone.
davidstone retitled this revision from Fix PR35677: UB on __int128_t or __uint128_t template parameters. to [clang] Handle 128-bits IntegerLiterals in StmtPrinter.Mar 24 2021, 2:47 PM
davidstone edited the summary of this revision. (Show Details)
davidstone marked 2 inline comments as done.

I believe this addresses the review comments.

aaron.ballman accepted this revision.Mar 25 2021, 6:39 AM

LGTM, thanks!

This revision is now accepted and ready to land.Mar 25 2021, 6:39 AM

Reformatted

Do you need me to commit on your behalf? If so, which email address would you like me to use for attribution?

Do you need me to commit on your behalf? If so, which email address would you like me to use for attribution?

Yeah, I do not have commit access. davidfromonline@gmail.com, please

aaron.ballman closed this revision.Mar 25 2021, 2:28 PM

Do you need me to commit on your behalf? If so, which email address would you like me to use for attribution?

Yeah, I do not have commit access. davidfromonline@gmail.com, please

I've committed on your behalf in 4b5baa5b8244778b0e7253cdb98924c3dab611b7, thank you for the patch!