commit 625a8fad5969b39a1ba8484a68935c27ba4a05a7
parent 79d3040398e884808312473b32e65a02a04feeef
Author: lumidify <nobody@lumidify.org>
Date: Fri, 6 Sep 2024 08:30:59 +0200
Make error messages for buffer_get_line more useful
Diffstat:
5 files changed, 16 insertions(+), 15 deletions(-)
diff --git a/assert.h b/assert.h
@@ -4,5 +4,7 @@
/* based on the assert found in OpenBSD */
void ledit_assert_impl(const char *file, int line, const char *func, const char *failedexpr);
#define ledit_assert(e) ((e) ? (void)0 : ledit_assert_impl(__FILE__, __LINE__, __func__, #e))
+/* used by some functions like buffer_get_line */
+#define ledit_assert_manual(e, file, line, func) ((e) ? (void)0 : ledit_assert_impl(file, line, func, #e))
#endif
diff --git a/buffer.c b/buffer.c
@@ -688,8 +688,8 @@ buffer_delete_line_entries_base(ledit_buffer *buffer, size_t index1, size_t inde
}
ledit_line *
-buffer_get_line(ledit_buffer *buffer, size_t index) {
- ledit_assert(index < buffer->lines_num);
+buffer_get_line_impl(ledit_buffer *buffer, size_t index, const char *file, int line, const char *func) {
+ ledit_assert_manual(index < buffer->lines_num, file, line, func);
return index < buffer->lines_gap ?
&buffer->lines[index] :
&buffer->lines[index + buffer->lines_cap - buffer->lines_num];
diff --git a/buffer.h b/buffer.h
@@ -142,8 +142,12 @@ void buffer_normalize_line(ledit_line *line);
* Get the line at logical index 'index'.
* The returned line is only valid until the next
* action that appends or deletes line entries.
+ * The macro is used in order to give better debug
+ * information when there is an error since so many
+ * functions call this one.
*/
-ledit_line *buffer_get_line(ledit_buffer *buffer, size_t index);
+ledit_line *buffer_get_line_impl(ledit_buffer *buffer, size_t index, const char *file, int line, const char *func);
+#define buffer_get_line(buffer, index) (buffer_get_line_impl((buffer), (index), __FILE__, __LINE__, __func__))
/*
* Tell views to recalculate the height of line 'line' and
diff --git a/view.c b/view.c
@@ -162,15 +162,13 @@ view_unlock(ledit_view *view) {
view->lock_text = NULL;
}
-#if 0
ledit_view_line *
-view_get_line(ledit_view *view, size_t index) {
- ledit_assert(index < view->lines_num);
+view_get_line_impl(ledit_view *view, size_t index, const char *file, int line, const char *func) {
+ ledit_assert_manual(index < view->lines_num, file, line, func);
return index < view->lines_gap ?
&view->lines[index] :
&view->lines[index + view->lines_cap - view->lines_num];
}
-#endif
static void
move_line_gap(ledit_view *view, size_t index) {
diff --git a/view.h b/view.h
@@ -112,15 +112,12 @@ void view_unlock(ledit_view *view);
* Get the view line at the given index.
* The returned line is only valid until the next
* action that appends or deletes line entries.
+ * The macro is used in order to give better debug
+ * information when there is an error since so many
+ * functions call this one.
*/
-/* ledit_view_line *view_get_line(ledit_view *view, size_t index); */
-
-/* This is very hacky - it's so the actual function calling view_get_line is returned in the assertion message.
- * There probably is a better way to do this.
- * FIXME: couldn't this just use the same trick that assert does?
- * WARNING: Since this is now a macro, the arguments are not allowed to have side effects!
- */
-#define view_get_line(view, index) (ledit_assert((index) < (view)->lines_num), (index) < (view)->lines_gap ? &(view)->lines[index] : &(view)->lines[(index) + (view)->lines_cap - (view)->lines_num])
+ledit_view_line *view_get_line_impl(ledit_view *view, size_t index, const char *file, int line, const char *func);
+#define view_get_line(view, index) (view_get_line_impl((view), (index), __FILE__, __LINE__, __func__))
/*
* These notification functions are called by the buffer when text