This is an archive of the discontinued LLVM Phabricator instance.

llvm/ADT/StringExtras.h hexDigitValue - Init of integer buffer
Needs ReviewPublic

Authored by jerker.back on Nov 23 2020, 7:41 AM.
This revision needs review, but there are no reviewers specified.

Details

Reviewers
None
Summary

Hi,

In the header StringExtras.h, function hexDigitValue

due to the expression

LUT[i] = -1U;

my compiler gives me

error C4146: unary minus operator applied to unsigned type, result still unsigned

This is a warning turned into an error by a default compiler switch
The patch gives one solution which I believe also could be more efficient

Thanks

Diff Detail

Event Timeline

jerker.back created this revision.Nov 23 2020, 7:41 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 23 2020, 7:41 AM
jerker.back requested review of this revision.Nov 23 2020, 7:41 AM
lebedev.ri added inline comments.
llvm/include/llvm/ADT/StringExtras.h
70

This doesn't fill the entire array with -1
https://godbolt.org/z/G3Yqbh

74

How about just ~0U ?

How to submit a new patch?

llvm/include/llvm/ADT/StringExtras.h
74

Yes, of course

LUT[i] = ~0U;

works without the compiler complaining

joerg added a subscriber: joerg.Nov 23 2020, 11:52 AM

Do we really want to change the code here? It is perfectly well defined behavior.

Expanding the patch to similar code places in the file.
Compiler warnings are gone under -Wall

Do we really want to change the code here? It is perfectly well defined behavior.

I weakly agree with @joerg, I would have thought we have lots of cases of -1U since it's a common / well-understood way to get this value. Is the warning possible to turn off? Have you reported a compiler bug?

Do we really want to change the code here? It is perfectly well defined behavior.

I weakly agree with @joerg, I would have thought we have lots of cases of -1U since it's a common / well-understood way to get this value. Is the warning possible to turn off? Have you reported a compiler bug?

As I said in the beginning, this is a compiler warning turned into an error by a default compiler option (-sdl, Security Development Lifecycle (SDL) checks). The option can be turned off. It's a bit unclear under what conditions exactly the option is default, off or disabled. The issue is well known and not a bug.

Do we really want to change the code here? It is perfectly well defined behavior.

I weakly agree with @joerg, I would have thought we have lots of cases of -1U since it's a common / well-understood way to get this value. Is the warning possible to turn off? Have you reported a compiler bug?

As I said in the beginning, this is a compiler warning turned into an error by a default compiler option (-sdl, Security Development Lifecycle (SDL) checks). The option can be turned off. It's a bit unclear under what conditions exactly the option is default, off or disabled. The issue is well known and not a bug.

Well, it seems like a bigger decision than just this patch, since there are 306 instances of this in llvm-project:

$ git grep -e -1U | wc -l
     306

I don't know if you were planning to change them all one-by-one, but it seems like a policy change if we're going to disallow this literal. Personally I find -1U more readable than ~0U and certainly easier to type (there are 121 of those, too, though).

I don't feel that strongly one way or the other, but this might deserve a discussion on llvm-dev.

Do we really want to change the code here? It is perfectly well defined behavior.

I weakly agree with @joerg, I would have thought we have lots of cases of -1U since it's a common / well-understood way to get this value. Is the warning possible to turn off? Have you reported a compiler bug?

As I said in the beginning, this is a compiler warning turned into an error by a default compiler option (-sdl, Security Development Lifecycle (SDL) checks). The option can be turned off. It's a bit unclear under what conditions exactly the option is default, off or disabled. The issue is well known and not a bug.

Well, it seems like a bigger decision than just this patch, since there are 306 instances of this in llvm-project:

$ git grep -e -1U | wc -l
     306

I don't know if you were planning to change them all one-by-one, but it seems like a policy change if we're going to disallow this literal. Personally I find -1U more readable than ~0U and certainly easier to type (there are 121 of those, too, though).

I don't feel that strongly one way or the other, but this might deserve a discussion on llvm-dev.

Thanks for the effort to investigate and I agree fully with you. Since all the other occurrences throw a warning as well I'm aware of the extent. My immediate interests lie to the PDB debug info code, which is based on Microsoft code at GitHub, which in turn I think must have been published just for the need in LLVM. It's not easy to break out functionality like the debug info handling from LLVM and other dependencies follow, especially headers.