Skip to content

Commit

Permalink
Merge pull request #1562 from svaarala/codepolicy-ll-ull-constants
Browse files Browse the repository at this point in the history
Add codepolicycheck for longlong consts, fix a few consts
  • Loading branch information
svaarala authored Jun 14, 2017
2 parents 0d12c98 + c6b482e commit 3f3a93c
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 27 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,7 @@ codepolicycheck:
--check-nonleading-tab \
--check-cpp-comment \
--check-ifdef-ifndef \
--check-longlong-constants \
--dump-vim-commands \
src-input/*.c src-input/*.h src-input/*.h.in tests/api/*.c
@$(PYTHON) util/check_code_policy.py \
Expand Down
3 changes: 3 additions & 0 deletions RELEASES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2927,6 +2927,9 @@ Planned

* Fix MSVC cast warning in error augmentation code (GH-1511)

* Improve support for old MSVC versions without __pragma(), long long, and
LL/ULL constants (GH-1559, GH-1562)

* Simplify handling of ENDFIN opcode a bit (GH-1508)

* Rework value stack grow/shrink handling; value stack is now grown when
Expand Down
2 changes: 1 addition & 1 deletion src-input/duk_bi_date_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,6 @@ DUK_INTERNAL_DECL duk_int_t duk_bi_date_get_local_tzoffset_windows_no_dst(duk_do
FileTimeToSystemTime((const FILETIME *) &ft2, &st2);
duk__convert_systime_to_ularge((const SYSTEMTIME *) &st2, &tmp2);

return (duk_int_t) (((LONGLONG) tmp2.QuadPart - (LONGLONG) tmp1.QuadPart) / 10000000LL); /* seconds */
return (duk_int_t) (((LONGLONG) tmp2.QuadPart - (LONGLONG) tmp1.QuadPart) / DUK_I64_CONSTANT(10000000)); /* seconds */
}
#endif /* DUK_USE_DATE_TZO_WINDOWS_NO_DST */
4 changes: 2 additions & 2 deletions src-input/duk_heap_alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1110,8 +1110,8 @@ duk_heap *duk_heap_alloc(duk_alloc_function alloc_func,
res->rnd_state[0] = (duk_uint64_t) DUK_USE_DATE_GET_NOW((duk_context *) res->heap_thread);
DUK_ASSERT(res->rnd_state[1] == 0); /* Not filled here, filled in by seed preparation. */
#if 0 /* Manual test values matching misc/xoroshiro128plus_test.c. */
res->rnd_state[0] = 0xdeadbeef12345678ULL;
res->rnd_state[1] = 0xcafed00d12345678ULL;
res->rnd_state[0] = DUK_U64_CONSTANT(0xdeadbeef12345678);
res->rnd_state[1] = DUK_U64_CONSTANT(0xcafed00d12345678);
#endif
duk_util_tinyrandom_prepare_seed(res->heap_thread);
/* Mix in heap pointer: this ensures that if two Duktape heaps are
Expand Down
4 changes: 2 additions & 2 deletions src-input/duk_hobject_props.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ DUK_LOCAL duk_uint32_t duk__tval_fastint_to_arr_idx(duk_tval *tv) {
DUK_ASSERT(DUK_TVAL_IS_FASTINT(tv));

t = DUK_TVAL_GET_FASTINT(tv);
if ((t & ~0xffffffffULL) != 0) {
if ((t & ~DUK_U64_CONSTANT(0xffffffff)) != 0) {
/* Catches >0x100000000 and negative values. */
return DUK__NO_ARRAY_INDEX;
}
Expand Down Expand Up @@ -2901,7 +2901,7 @@ DUK_LOCAL duk_uint32_t duk__to_new_array_length_checked(duk_hthread *thr, duk_tv
/* Very common case. */
duk_int64_t fi;
fi = DUK_TVAL_GET_FASTINT(tv);
if (fi < 0 || fi > 0xffffffffLL) {
if (fi < 0 || fi > DUK_I64_CONSTANT(0xffffffff)) {
goto fail_range;
}
return (duk_uint32_t) fi;
Expand Down
8 changes: 4 additions & 4 deletions src-input/duk_js_executor.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ DUK_LOCAL DUK__INLINE_PERF void duk__vm_arith_add(duk_hthread *thr, duk_tval *tv
v2 = DUK_TVAL_GET_FASTINT(tv_y);
v3 = v1 + v2;
v3_hi = (duk_int32_t) (v3 >> 32);
if (DUK_LIKELY(v3_hi >= -0x8000LL && v3_hi <= 0x7fffLL)) {
if (DUK_LIKELY(v3_hi >= DUK_I64_CONSTANT(-0x8000) && v3_hi <= DUK_I64_CONSTANT(0x7fff))) {
tv_z = thr->valstack_bottom + idx_z;
DUK_TVAL_SET_FASTINT_UPDREF(thr, tv_z, v3); /* side effects */
return;
Expand Down Expand Up @@ -240,8 +240,8 @@ DUK_LOCAL DUK__INLINE_PERF void duk__vm_arith_binary_op(duk_hthread *thr, duk_tv
* 32-bit inputs. Avoid zero inputs to avoid
* negative zero issues (-1 * 0 = -0, for instance).
*/
if (v1 >= -0x80000000LL && v1 <= 0x7fffffffLL && v1 != 0 &&
v2 >= -0x80000000LL && v2 <= 0x7fffffffLL && v2 != 0) {
if (v1 >= DUK_I64_CONSTANT(-0x80000000) && v1 <= DUK_I64_CONSTANT(0x7fffffff) && v1 != 0 &&
v2 >= DUK_I64_CONSTANT(-0x80000000) && v2 <= DUK_I64_CONSTANT(0x7fffffff) && v2 != 0) {
v3 = v1 * v2;
} else {
goto skip_fastint;
Expand Down Expand Up @@ -284,7 +284,7 @@ DUK_LOCAL DUK__INLINE_PERF void duk__vm_arith_binary_op(duk_hthread *thr, duk_tv
}

v3_hi = (duk_int32_t) (v3 >> 32);
if (DUK_LIKELY(v3_hi >= -0x8000LL && v3_hi <= 0x7fffLL)) {
if (DUK_LIKELY(v3_hi >= DUK_I64_CONSTANT(-0x8000) && v3_hi <= DUK_I64_CONSTANT(0x7fff))) {
tv_z = thr->valstack_bottom + idx_z;
DUK_TVAL_SET_FASTINT_UPDREF(thr, tv_z, v3); /* side effects */
return;
Expand Down
2 changes: 1 addition & 1 deletion src-input/duk_selftest.c
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ DUK_LOCAL duk_uint_t duk__selftest_64bit_arithmetic(void) {
/* Catch a double-to-int64 cast issue encountered in practice. */
d = 2147483648.0;
i = (duk_int64_t) d;
if (i != 0x80000000LL) {
if (i != DUK_I64_CONSTANT(0x80000000)) {
DUK__FAILED("casting 2147483648.0 to duk_int64_t failed");
}
#else
Expand Down
28 changes: 14 additions & 14 deletions src-input/duk_tval.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ DUK_INTERNAL DUK_ALWAYS_INLINE void duk_tval_set_number_chkfast_fast(duk_tval *t
if (shift >= 0 && shift <= 46) { /* exponents 1023 to 1069 */
duk_int64_t t;

if (((0x000fffffffffffffLL >> shift) & i) == 0) {
t = i | 0x0010000000000000LL; /* implicit leading one */
t = t & 0x001fffffffffffffLL;
if (((DUK_I64_CONSTANT(0x000fffffffffffff) >> shift) & i) == 0) {
t = i | DUK_I64_CONSTANT(0x0010000000000000); /* implicit leading one */
t = t & DUK_I64_CONSTANT(0x001fffffffffffff);
t = t >> (52 - shift);
if (i < 0) {
t = -t;
Expand All @@ -46,13 +46,13 @@ DUK_INTERNAL DUK_ALWAYS_INLINE void duk_tval_set_number_chkfast_fast(duk_tval *t
return;
}
} else if (shift == -1023) { /* exponent 0 */
if (i >= 0 && (i & 0x000fffffffffffffLL) == 0) {
if (i >= 0 && (i & DUK_I64_CONSTANT(0x000fffffffffffff)) == 0) {
/* Note: reject negative zero. */
DUK_TVAL_SET_FASTINT(tv, (duk_int64_t) 0);
return;
}
} else if (shift == 47) { /* exponent 1070 */
if (i < 0 && (i & 0x000fffffffffffffLL) == 0) {
if (i < 0 && (i & DUK_I64_CONSTANT(0x000fffffffffffff)) == 0) {
DUK_TVAL_SET_FASTINT(tv, (duk_int64_t) DUK_FASTINT_MIN);
return;
}
Expand All @@ -78,15 +78,15 @@ DUK_INTERNAL DUK_ALWAYS_INLINE duk_double_t duk_tval_get_number_packed(duk_tval
t = (duk_uint64_t) DUK_DBLUNION_GET_UINT64(tv);
if ((t >> 48) != DUK_TAG_FASTINT) {
return tv->d;
} else if (t & 0x0000800000000000ULL) {
} else if (t & DUK_U64_CONSTANT(0x0000800000000000)) {
t = (duk_uint64_t) (-((duk_int64_t) t)); /* avoid unary minus on unsigned */
t = t & 0x0000ffffffffffffULL; /* negative */
t |= 0xc330000000000000ULL;
t = t & DUK_U64_CONSTANT(0x0000ffffffffffff); /* negative */
t |= DUK_U64_CONSTANT(0xc330000000000000);
DUK_DBLUNION_SET_UINT64(&du, t);
return du.d + 4503599627370496.0; /* 1 << 52 */
} else if (t != 0) {
t &= 0x0000ffffffffffffULL; /* positive */
t |= 0x4330000000000000ULL;
t &= DUK_U64_CONSTANT(0x0000ffffffffffff); /* positive */
t |= DUK_U64_CONSTANT(0x4330000000000000);
DUK_DBLUNION_SET_UINT64(&du, t);
return du.d - 4503599627370496.0; /* 1 << 52 */
} else {
Expand All @@ -105,11 +105,11 @@ DUK_INTERNAL DUK_ALWAYS_INLINE duk_double_t duk_tval_get_number_unpacked(duk_tva

if (tv->t == DUK_TAG_FASTINT) {
if (tv->v.fi >= 0) {
t = 0x4330000000000000ULL | (duk_uint64_t) tv->v.fi;
t = DUK_U64_CONSTANT(0x4330000000000000) | (duk_uint64_t) tv->v.fi;
DUK_DBLUNION_SET_UINT64(&du, t);
return du.d - 4503599627370496.0; /* 1 << 52 */
} else {
t = 0xc330000000000000ULL | (duk_uint64_t) (-tv->v.fi);
t = DUK_U64_CONSTANT(0xc330000000000000) | (duk_uint64_t) (-tv->v.fi);
DUK_DBLUNION_SET_UINT64(&du, t);
return du.d + 4503599627370496.0; /* 1 << 52 */
}
Expand All @@ -128,11 +128,11 @@ DUK_INTERNAL DUK_ALWAYS_INLINE duk_double_t duk_tval_get_number_unpacked_fastint
DUK_ASSERT(tv->t == DUK_TAG_FASTINT);

if (tv->v.fi >= 0) {
t = 0x4330000000000000ULL | (duk_uint64_t) tv->v.fi;
t = DUK_U64_CONSTANT(0x4330000000000000) | (duk_uint64_t) tv->v.fi;
DUK_DBLUNION_SET_UINT64(&du, t);
return du.d - 4503599627370496.0; /* 1 << 52 */
} else {
t = 0xc330000000000000ULL | (duk_uint64_t) (-tv->v.fi);
t = DUK_U64_CONSTANT(0xc330000000000000) | (duk_uint64_t) (-tv->v.fi);
DUK_DBLUNION_SET_UINT64(&du, t);
return du.d + 4503599627370496.0; /* 1 << 52 */
}
Expand Down
6 changes: 3 additions & 3 deletions src-input/duk_tval.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ typedef struct {
} while (0)
#else
#define DUK__TVAL_SET_I48(tv,i) do { \
(tv)->ull[DUK_DBL_IDX_ULL0] = (((duk_uint64_t) DUK_TAG_FASTINT) << 48) | (((duk_uint64_t) (i)) & 0x0000ffffffffffffULL); \
(tv)->ull[DUK_DBL_IDX_ULL0] = (((duk_uint64_t) DUK_TAG_FASTINT) << 48) | (((duk_uint64_t) (i)) & DUK_U64_CONSTANT(0x0000ffffffffffff)); \
} while (0)
#define DUK__TVAL_SET_U32(tv,i) do { \
(tv)->ull[DUK_DBL_IDX_ULL0] = (((duk_uint64_t) DUK_TAG_FASTINT) << 48) | (duk_uint64_t) (i); \
Expand Down Expand Up @@ -621,8 +621,8 @@ DUK_INTERNAL_DECL duk_double_t duk_tval_get_number_unpacked_fastint(duk_tval *tv

/* fastint constants etc */
#if defined(DUK_USE_FASTINT)
#define DUK_FASTINT_MIN (-0x800000000000LL)
#define DUK_FASTINT_MAX 0x7fffffffffffLL
#define DUK_FASTINT_MIN (DUK_I64_CONSTANT(-0x800000000000))
#define DUK_FASTINT_MAX (DUK_I64_CONSTANT(0x7fffffffffff))
#define DUK_FASTINT_BITS 48

DUK_INTERNAL_DECL void duk_tval_set_number_chkfast_fast(duk_tval *tv, duk_double_t x);
Expand Down
15 changes: 15 additions & 0 deletions util/check_code_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ def __init__(self, filename, linenumber, line, reason):
re_nonascii = re.compile(r'^.*?[\x80-\xff].*?$')
re_func_decl_or_def = re.compile(r'^(\w+)\s+(?:\w+\s+)*(\w+)\(.*?.*?$') # may not finish on same line
re_cpp_comment = re.compile(r'^.*?//.*?$')
re_longlong_constant_dec = re.compile(r'[0-9]+(LL|ULL)')
re_longlong_constant_hex = re.compile(r'0x[0-9a-fA-F]+(LL|ULL)')

fixmeString = 'FIX' + 'ME' # avoid triggering a code policy check warning :)

Expand Down Expand Up @@ -351,6 +353,16 @@ def checkIfdefIfndef(lines, idx, filename):
if line.startswith('#ifndef'):
raise Exception('#ifndef found, #if !defined is preferred')

def checkLongLongConstants(lines, idx, filename):
line = lines[idx]

if not 'LL' in line:
return
for m in re.finditer(re_longlong_constant_hex, line):
raise Exception('plain longlong constant, use DUK_U64_CONSTANT or DUK_I64_CONSTANT: ' + str(m.group(0)))
for m in re.finditer(re_longlong_constant_dec, line):
raise Exception('plain longlong constant, use DUK_U64_CONSTANT or DUK_I64_CONSTANT: ' + str(m.group(0)))

def checkCppComment(lines, idx, filename):
line = lines[idx]
m = re_cpp_comment.match(line)
Expand Down Expand Up @@ -419,6 +431,7 @@ def main():
parser.add_option('--check-nonleading-tab', dest='check_nonleading_tab', action='store_true', default=False, help='Check for non-leading tab characters')
parser.add_option('--check-cpp-comment', dest='check_cpp_comment', action='store_true', default=False, help='Check for c++ comments ("// ...")')
parser.add_option('--check-ifdef-ifndef', dest='check_ifdef_ifndef', action='store_true', default=False, help='Check for #ifdef and #ifndef (prefer #if defined and #if !defined)')
parser.add_option('--check-longlong-constants', dest='check_longlong_constants', action='store_true', default=False, help='Check for plain 123LL or 123ULL constants')
parser.add_option('--fail-on-errors', dest='fail_on_errors', action='store_true', default=False, help='Fail on errors (exit code != 0)')

(opts, args) = parser.parse_args()
Expand All @@ -436,6 +449,8 @@ def main():
checkersRaw.append(checkNoSymbolVisibility)
if opts.check_ifdef_ifndef:
checkersRaw.append(checkIfdefIfndef)
if opts.check_longlong_constants:
checkersRaw.append(checkLongLongConstants)

checkersNoCCommentsOrLiterals = []
if opts.check_cpp_comment:
Expand Down

0 comments on commit 3f3a93c

Please sign in to comment.