Skip to content

Commit

Permalink
Slightly optimise S_trie_bitmap_set_folded()
Browse files Browse the repository at this point in the history
This static function in regcomp_trie.c evaluates UTF8_TWO_BYTE_HI(uvc)
twice, because a macro it calls uses the passed expression twice.
So calculate the value once into a local var.

Now, the compiler might already optimise this. But this commit may
or may not also have the (hoped for) side effect of shutting up a
Coverity warning.

The code is taking an 8-bit variant char, calculating what its upper
byte would be if converted to utf-8 (so in this case, only 0xc2 and 0xc3
are possible), then setting a bit in a trie representing this value. The
bit setting involves calculating the byte and bit offsets - the former
by doing >>3. But doing that shift on c2/c3 always gives the same
value, so Coverity is rightfully warning that UTF8_TWO_BYTE_HI(uvc) >> 3
is always constant.

By splitting that expression into two statements, maybe Coverity won't
notice. If it still does, at least this commit simplifies the code and
will make any eventual fix easier.

Here's the original Coverity report (it's complaining about the macro
which the previous commit subsequently turned into a static function):

>>>     CID 488118:    (CONSTANT_EXPRESSION_RESULT)
>>>     "(UV)(U8)(((UV)(*ch | 0) >> 6) | 192UL /* (U8)~(0xff >> 2) */) >> 3" is
0x18 regardless of the values of its operands. This occurs as a value.
1403                                         TRIE_BITMAP_SET_FOLDED(trie,*ch, folder);
  • Loading branch information
iabyn committed Apr 1, 2024
1 parent f7c9d55 commit d3ddc7c
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion regcomp_trie.c
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,8 @@ S_trie_bitmap_set_folded(pTHX_ RExC_state_t *pRExC_state,
/* store first byte of utf8 representation of */
/* variant codepoints */
if (! UVCHR_IS_INVARIANT(uvc)) {
TRIE_BITMAP_SET(trie, UTF8_TWO_BYTE_HI(uvc));
U8 hi = UTF8_TWO_BYTE_HI(uvc);
TRIE_BITMAP_SET(trie, hi);
}
}
}
Expand Down

0 comments on commit d3ddc7c

Please sign in to comment.