Skip to content

[WIP] MDEV-35770 Augment DBUG_ASSERT with __builtin_assume #3944

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions client/mysqltest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1580,7 +1580,7 @@ ATTRIBUTE_NORETURN static void cleanup_and_exit(int exit_code,
break;
default:
printf("unknown exit code: %d\n", exit_code);
DBUG_ASSERT(0);
DBUG_ASSERT_NO_ASSUME(0);
}
}

Expand Down Expand Up @@ -8147,7 +8147,7 @@ static void append_session_track_info(DYNAMIC_STRING *ds, MYSQL *mysql)
}
else
{
DBUG_ASSERT(0);
DBUG_ASSERT_NO_ASSUME(0);
dynstr_append_mem(ds, STRING_WITH_LEN("Tracker???\n"));
}
}
Expand Down
1 change: 1 addition & 0 deletions cmake/maintainer.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ SET(MY_WARNING_FLAGS
-Wno-unused-parameter
-Wno-unused-private-field
-Wnon-virtual-dtor
-Wno-assume
-Woverloaded-virtual
-Wvla
-Wwrite-strings
Expand Down
14 changes: 14 additions & 0 deletions include/my_compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,20 @@
# define MY_ALIGNED(n) __attribute__((__aligned__((n))))
#endif

/*
An analog of c++23 [[assume(cond)]]
for GCC >= 13.0, Clang and MSVC.
*/
#if defined __GNUC__ && __GNUC__ >= 13 && defined __cplusplus
#define MY_ASSUME(A) __attribute__((__assume__(A)));
#elif defined __clang__
#define MY_ASSUME(A) __builtin_assume(A);
#elif defined _MSC_VER
#define MY_ASSUME(A) __assume(A);
#else
#define MY_ASSUME(A) do { } while(0)
#endif

/**
Generic (compiler-independent) features.
*/
Expand Down
11 changes: 8 additions & 3 deletions include/my_dbug.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#ifndef _my_dbug_h
#define _my_dbug_h

#include "my_compiler.h"

#ifndef _WIN32
#include <signal.h>
#endif
Expand Down Expand Up @@ -115,10 +117,11 @@ extern int (*dbug_sanity)(void);
#define DBUG_END() _db_end_ ()
#define DBUG_LOCK_FILE _db_lock_file_()
#define DBUG_UNLOCK_FILE _db_unlock_file_()
#define DBUG_ASSERT(A) do { \
#define DBUG_ASSERT_NO_ASSUME(A) do { \
if (unlikely(!(A)) && _db_my_assert(__FILE__, __LINE__, #A)) assert(A); \
} while (0)
#define DBUG_SLOW_ASSERT(A) DBUG_ASSERT(A)
#define DBUG_ASSERT(A) DBUG_ASSERT_NO_ASSUME(A)
#define DBUG_SLOW_ASSERT(A) DBUG_ASSERT_NO_ASSUME(A)
#define DBUG_ASSERT_EXISTS
#define DBUG_EXPLAIN(buf,len) _db_explain_(0, (buf),(len))
#define DBUG_EXPLAIN_INITIAL(buf,len) _db_explain_init_((buf),(len))
Expand Down Expand Up @@ -200,10 +203,12 @@ extern void _db_suicide_(void);
#ifdef DBUG_ASSERT_AS_PRINTF
extern void (*my_dbug_assert_failed)(const char *assert_expr, const char* file, unsigned long line);
#define DBUG_ASSERT(assert_expr) do { if (!(assert_expr)) { my_dbug_assert_failed(#assert_expr, __FILE__, __LINE__); }} while (0)
#define DBUG_ASSERT_NO_ASSUME(A) do { } while (0)
#define DBUG_ASSERT_EXISTS
#define IF_DBUG_ASSERT(A,B) A
#else
#define DBUG_ASSERT(A) do { } while(0)
#define DBUG_ASSERT(A) MY_ASSUME(A)
#define DBUG_ASSERT_NO_ASSUME(A) do { } while (0)
#define IF_DBUG_ASSERT(A,B) B
#endif /* DBUG_ASSERT_AS_PRINTF */
#endif /* !defined(DBUG_OFF) && !defined(_lint) */
Expand Down
2 changes: 1 addition & 1 deletion mysys/charset.c
Original file line number Diff line number Diff line change
Expand Up @@ -1652,7 +1652,7 @@ static UINT get_codepage(const char *s)
UINT cp;
if (s[0] != 'c' || s[1] != 'p')
{
DBUG_ASSERT(0);
DBUG_ASSERT_NO_ASSUME(0);
return 0;
}
cp= strtoul(s + 2, NULL, 10);
Expand Down
10 changes: 5 additions & 5 deletions mysys/ma_dyncol.c
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ static my_bool type_and_offset_read_num(DYNAMIC_COLUMN_TYPE *type,
lim= 0x1fffffff;
break;
default:
DBUG_ASSERT(0); /* impossible */
DBUG_ASSERT_NO_ASSUME(0); /* impossible */
return 1;
}
*type= (val & 0x7) + 1;
Expand Down Expand Up @@ -562,7 +562,7 @@ static my_bool type_and_offset_read_named(DYNAMIC_COLUMN_TYPE *type,
break;
case 1:
default:
DBUG_ASSERT(0); /* impossible */
DBUG_ASSERT_NO_ASSUME(0); /* impossible */
return 1;
}
*type= (val & 0xf) + 1;
Expand Down Expand Up @@ -960,7 +960,7 @@ dynamic_column_value_len(DYNAMIC_COLUMN_VALUE *value,
*/
if (scale < 0 || precision <= 0)
{
DBUG_ASSERT(0); /* Impossible */
DBUG_ASSERT_NO_ASSUME(0); /* Impossible */
return (size_t) ~0;
}
return (dynamic_column_var_uint_bytes(value->x.decimal.value.intg) +
Expand All @@ -985,7 +985,7 @@ dynamic_column_value_len(DYNAMIC_COLUMN_VALUE *value,
case DYN_COL_DYNCOL:
return value->x.string.value.length;
}
DBUG_ASSERT(0);
DBUG_ASSERT_NO_ASSUME(0);
return 0;
}

Expand Down Expand Up @@ -1563,7 +1563,7 @@ data_store(DYNAMIC_COLUMN *str, DYNAMIC_COLUMN_VALUE *value,
case DYN_COL_NULL:
break; /* Impossible */
}
DBUG_ASSERT(0);
DBUG_ASSERT_NO_ASSUME(0);
return ER_DYNCOL_OK; /* Impossible */
}

Expand Down
2 changes: 1 addition & 1 deletion mysys/ptr_cmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ my_off_t my_get_ptr(uchar *ptr, size_t pack_length)
case 3: pos= (my_off_t) mi_uint3korr(ptr); break;
case 2: pos= (my_off_t) mi_uint2korr(ptr); break;
case 1: pos= (my_off_t) *(uchar*) ptr; break;
default: DBUG_ASSERT(0); return 0;
default: DBUG_ASSERT_NO_ASSUME(0); return 0;
}
return pos;
}
2 changes: 1 addition & 1 deletion plugin/type_mysql_json/mysql_json.cc
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ static bool print_mysql_datetime_value(String *buffer, enum_field_types type,
TIME_from_longlong_datetime_packed(&t, sint8korr(data));
break;
default:
DBUG_ASSERT(0);
DBUG_ASSERT_NO_ASSUME(0);
return true;
}
/* Wrap all datetime strings within double quotes. */
Expand Down
2 changes: 1 addition & 1 deletion sql-common/my_time.c
Original file line number Diff line number Diff line change
Expand Up @@ -1622,7 +1622,7 @@ static char* fmt_usec(uint val, char *out, uint digits)
case 6:
return fmt_number6(val, out);
}
DBUG_ASSERT(0);
DBUG_ASSERT_NO_ASSUME(0);
return out;
}

Expand Down
2 changes: 1 addition & 1 deletion sql/create_options.cc
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ static bool set_one_value(ha_create_table_option *opt, THD *thd,
suppress_warning));
}
}
DBUG_ASSERT(0);
DBUG_ASSERT_NO_ASSUME(0);
my_error(ER_UNKNOWN_ERROR, MYF(0));
DBUG_RETURN(1);
}
Expand Down
6 changes: 3 additions & 3 deletions sql/ddl_log.cc
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ static uchar *store_string(uchar *pos, uchar *end, const LEX_CSTRING *str)
uint32 length= (uint32) str->length;
if (unlikely(pos + 2 + length + 1 > end))
{
DBUG_ASSERT(0);
DBUG_ASSERT_NO_ASSUME(0);
return end; // Overflow
}

Expand Down Expand Up @@ -587,7 +587,7 @@ static LEX_CSTRING get_string(uchar **pos, const uchar *end)
Overflow on read, should never happen
Set *pos to end to ensure any future calls also returns empty string
*/
DBUG_ASSERT(0);
DBUG_ASSERT_NO_ASSUME(0);
*pos= (uchar*) end;
tmp.str= "";
tmp.length= 0;
Expand Down Expand Up @@ -2771,7 +2771,7 @@ int ddl_log_execute_recovery()
*/
if (!(thd=new THD(0)))
{
DBUG_ASSERT(0); // Fatal error
DBUG_ASSERT_NO_ASSUME(0); // Fatal error
DBUG_RETURN(1);
}
original_thd= current_thd; // Probably NULL
Expand Down
10 changes: 5 additions & 5 deletions sql/field.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2785,7 +2785,7 @@ bool Field_row::row_create_fields(THD *thd, const Spvar_definition &def)
if (def.is_row()) // e.g. ROW(a INT, b VARCHAR(32))
return row_create_fields(thd, def.row_field_definitions());

DBUG_ASSERT(0); // Unknown ROW declaration style
DBUG_ASSERT_NO_ASSUME(0); // Unknown ROW declaration style
return true;
}

Expand Down Expand Up @@ -3942,7 +3942,7 @@ Item *Field_new_decimal::get_equal_const_item(THD *thd, const Context &ctx,
VDec val(const_item);
if (val.is_null())
{
DBUG_ASSERT(0);
DBUG_ASSERT_NO_ASSUME(0);
return const_item;
}
/*
Expand Down Expand Up @@ -5767,7 +5767,7 @@ static longlong read_native(const uchar *from, uint bytes)
case 3: return uint3korr(from);
case 4: { uint32 tmp; longget(tmp, from); return tmp; }
case 8: { longlong tmp; longlongget(tmp, from); return tmp; }
default: DBUG_ASSERT(0); return 0;
default: DBUG_ASSERT_NO_ASSUME(0); return 0;
}
}
#endif
Expand Down Expand Up @@ -11252,7 +11252,7 @@ uint32 Field_blob::character_octet_length() const
case 4:
return (uint32) UINT_MAX32;
default:
DBUG_ASSERT(0); // we should never go here
DBUG_ASSERT_NO_ASSUME(0); // we should never go here
return 0;
}
}
Expand Down Expand Up @@ -11339,7 +11339,7 @@ uint32 Field_blob::max_display_length() const
case 4:
return (uint32) UINT_MAX32;
default:
DBUG_ASSERT(0); // we should never go here
DBUG_ASSERT_NO_ASSUME(0); // we should never go here
return 0;
}
}
Expand Down
6 changes: 3 additions & 3 deletions sql/field.h
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ inline bool is_temporal_type_with_date(enum_field_types type)
return true;
case MYSQL_TYPE_DATETIME2:
case MYSQL_TYPE_TIMESTAMP2:
DBUG_ASSERT(0); // field->real_type() should not get to here.
DBUG_ASSERT_NO_ASSUME(0); // field->real_type() should not get to here.
return false;
default:
return false;
Expand Down Expand Up @@ -4456,7 +4456,7 @@ static inline longlong read_bigendian(const uchar *from, uint bytes)
case 6: return mi_uint6korr(from);
case 7: return mi_uint7korr(from);
case 8: return mi_sint8korr(from);
default: DBUG_ASSERT(0); return 0;
default: DBUG_ASSERT_NO_ASSUME(0); return 0;
}
}

Expand All @@ -4480,7 +4480,7 @@ static inline longlong read_lowendian(const uchar *from, uint bytes)
case 3: return uint3korr(from);
case 4: return uint4korr(from);
case 8: return sint8korr(from);
default: DBUG_ASSERT(0); return 0;
default: DBUG_ASSERT_NO_ASSUME(0); return 0;
}
}

Expand Down
2 changes: 1 addition & 1 deletion sql/field_conv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ static int set_bad_null_error(Field *field, int err)
my_error(err, MYF(0), field->field_name.str);
return -1;
}
DBUG_ASSERT(0); // impossible
DBUG_ASSERT_NO_ASSUME(0); // impossible
return -1;
}

Expand Down
6 changes: 3 additions & 3 deletions sql/filesort.cc
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ static uint32 read_keypart_length(const uchar *from, uint bytes)
case 2: return uint2korr(from);
case 3: return uint3korr(from);
case 4: return uint4korr(from);
default: DBUG_ASSERT(0); return 0;
default: DBUG_ASSERT_NO_ASSUME(0); return 0;
}
}

Expand Down Expand Up @@ -1236,7 +1236,7 @@ Type_handler_string_result::make_sort_key_part(uchar *to, Item *item,
of memory or have an item marked not null when it can be null.
This code is here mainly to avoid a hard crash in this case.
*/
DBUG_ASSERT(0);
DBUG_ASSERT_NO_ASSUME(0);
DBUG_PRINT("warning",
("Got null on something that shouldn't be null"));
memset(to, 0, sort_field->length); // Avoid crash
Expand Down Expand Up @@ -2567,7 +2567,7 @@ Type_handler_string_result::make_packed_sort_key_part(uchar *to, Item *item,
of memory or have an item marked not null when it can be null.
This code is here mainly to avoid a hard crash in this case.
*/
DBUG_ASSERT(0);
DBUG_ASSERT_NO_ASSUME(0);
DBUG_PRINT("warning",
("Got null on something that shouldn't be null"));
memset(to, 0, sort_field->length); // Avoid crash
Expand Down
2 changes: 1 addition & 1 deletion sql/gtid_index.cc
Original file line number Diff line number Diff line change
Expand Up @@ -999,7 +999,7 @@ int Gtid_index_reader::do_index_search_leaf(bool current_state_updated,
int res= get_offset_count(&offset, &gtid_count);
if (res == 1)
{
DBUG_ASSERT(0);
DBUG_ASSERT_NO_ASSUME(0);
give_error("Corrupt index; empty leaf node");
return -1;
}
Expand Down
14 changes: 7 additions & 7 deletions sql/ha_partition.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3284,7 +3284,7 @@ bool ha_partition::setup_engine_array(MEM_ROOT *mem_root,
DBUG_PRINT("error", ("partition %u engine %d is not same as "
"first partition %d", i, db_type,
(int) first_db_type));
DBUG_ASSERT(0);
DBUG_ASSERT_NO_ASSUME(0);
clear_handler_file();
goto err;
}
Expand Down Expand Up @@ -6206,7 +6206,7 @@ int ha_partition::index_read_idx_map(uchar *buf, uint index,
If not only used with READ_EXACT, we should investigate if possible
to optimize for other find_flag's as well.
*/
DBUG_ASSERT(0);
DBUG_ASSERT_NO_ASSUME(0);
/* fall back on the default implementation */
error= handler::index_read_idx_map(buf, index, key, keypart_map, find_flag);
}
Expand Down Expand Up @@ -7742,7 +7742,7 @@ int ha_partition::handle_unordered_next(uchar *buf, bool is_next_same)
if (m_part_spec.start_part >= m_tot_parts)
{
/* Should never happen! */
DBUG_ASSERT(0);
DBUG_ASSERT_NO_ASSUME(0);
DBUG_RETURN(HA_ERR_END_OF_FILE);
}
file= m_file[m_part_spec.start_part];
Expand Down Expand Up @@ -8291,7 +8291,7 @@ int ha_partition::handle_ordered_next(uchar *buf, bool is_next_same)
if (part_id >= m_tot_parts)
{
/* This should never happen! */
DBUG_ASSERT(0);
DBUG_ASSERT_NO_ASSUME(0);
DBUG_RETURN(HA_ERR_END_OF_FILE);
}

Expand Down Expand Up @@ -10313,7 +10313,7 @@ const char *ha_partition::index_type(uint inx)

if (first_used_partition == MY_BIT_NONE)
{
DBUG_ASSERT(0); // How can this happen?
DBUG_ASSERT_NO_ASSUME(0); // How can this happen?
DBUG_RETURN(handler::index_type(inx));
}

Expand Down Expand Up @@ -10469,7 +10469,7 @@ void ha_partition::print_error(int error, myf errflag)
{
if (m_last_part >= m_tot_parts)
{
DBUG_ASSERT(0);
DBUG_ASSERT_NO_ASSUME(0);
m_last_part= 0;
}
m_file[m_last_part]->print_error(error, errflag);
Expand Down Expand Up @@ -10667,7 +10667,7 @@ ha_partition::check_if_supported_inplace_alter(TABLE *altered_table,
else if (first_is_set != (ha_alter_info->handler_ctx != NULL))
{
/* Either none or all partitions must set handler_ctx! */
DBUG_ASSERT(0);
DBUG_ASSERT_NO_ASSUME(0);
DBUG_RETURN(HA_ALTER_ERROR);
}
if (p_result < result)
Expand Down
Loading