This is an archive of the discontinued LLVM Phabricator instance.

Turn unreachable error into assert
ClosedPublic

Authored by thopre on May 17 2023, 9:16 AM.

Details

Summary

Function valueFromStringRepr() throws an error on missing 0x prefix when
parsing a number string into a value. However, getWildcardRegex() already
ensures that only text with the 0x prefix will match and be parsed,
making that error throwing code dead code. This commit turn the code
into an assert and remove the unit tests exercising that test
accordingly.

Diff Detail

Event Timeline

thopre created this revision.May 17 2023, 9:16 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 17 2023, 9:16 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
thopre requested review of this revision.May 17 2023, 9:16 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 17 2023, 9:16 AM

Given that there are unit tests, this code is in the library's public-facing API. Does removing the error check cause problems if users happen to be leveraging the FileCheck library in some manner?

Given that there are unit tests, this code is in the library's public-facing API. Does removing the error check cause problems if users happen to be leveraging the FileCheck library in some manner?

It is in the library but not exposed in the FileCheck library header. The unit tests use the FileCheck internal header. The library interface is much higher level than that: it is essentially composed of the readCheckFile and checkInput functions. Everything else is internal.

jhenderson added inline comments.May 19 2023, 12:27 AM
llvm/lib/FileCheck/FileCheck.cpp
148–151

I'd get rid of the unnecessary temporary and just do the calculation inline.

Also, is this the right place for the assert? Previously the error was later in the code.

thopre added inline comments.May 19 2023, 5:55 AM
llvm/lib/FileCheck/FileCheck.cpp
148–151

Will the consume_front() happen if it's inside the assert though? Because under normal circumstances (i.e. assert does not trip) the consume_front() *must* happen for the function to do the right thing.

jhenderson accepted this revision.May 21 2023, 11:57 PM

LGTM, with one nit.

llvm/lib/FileCheck/FileCheck.cpp
148–151

Bleh, I missed that consume_front has side effects beyond getting the return value. Okay, I can't think of any real improvements (other than I think static_cast is preferred generally to C-style casts).

This revision is now accepted and ready to land.May 21 2023, 11:57 PM
thopre added inline comments.May 22 2023, 9:02 AM
llvm/lib/FileCheck/FileCheck.cpp
148–151

I agree in the general case but there is not a single use of static_cast<void> to indicate a function return value being ignored in the llvm/ subtree. There is however plenty of (void) C-style cast example. Ok to land as is?

Go ahead with landing. I'm going to start a discussion on Discourse about coding style for casts as I don't see it discussed anywhere in the coding standards.

This revision was automatically updated to reflect the committed changes.