QtCreator debugger: display enum texts when used as array indexes, instead of 0,1,2...
-
In my program's i have arrays of data, with enums describing the meaning like this:
typedef enum { ITEM_0 = 0, ITEM_1, ITEM_2, ITEMS_ARRAY_SIZE, }ITEMS_ENUM; uint32_t myItems[ITEMS_ARRAY_SIZE];
Then in my code I can access a place in the array like this:
myItems[ITEM_0] = 34;
The debugger doesn't understand the indexes of the array are coming from an enum, and therefore displays the value of the array like this:
myItems value type [0] 34 uint32_t
It would be nice if the debugger would show it like this:
myItems value type [ITEM_0] 34 uint32_t
For this to work, the Qt debugger should first see the size of the array is an element of an enum, and then assume the rest of the values of the enum are used to name the indexes of the array. Of course this doesn't work for every situation, so we also need a way to switch back from enum display to 0,1,2,3... display.
I'm looking for a solution that works with standard C enums and arrays, as I'm using Qt to run and debug legacy C programs. Therefore I can't (easily) use C++ constructions to achieve this.
My versions:
Qt Creator 12.0.1
Based on Qt 6.6.1 (GCC 13.2.1 20230801, x86_64)
$ uname -a
Linux cedric 6.6.7-arch1-1 #1 SMP PREEMPT_DYNAMIC Thu, 14 Dec 2023 03:45:42 +0000 x86_64 GNU/Linuxmainwindow.cpp (the other project files are default)
#include "mainwindow.h" #include "ui_mainwindow.h" typedef enum { ITEM_0 = 0, ITEM_1, ITEM_2, ITEMS_ARRAY_SIZE, }ITEMS_ENUM; uint32_t myItems[ITEMS_ARRAY_SIZE]; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); myItems[ITEM_0] = 34; myItems[ITEM_1] = 16; myItems[ITEM_2] = 45; } MainWindow::~MainWindow() { delete ui; }
-
@Cedric-air said in QtCreator debugger: display enum texts when used as array indexes, instead of 0,1,2...:
For this to work, the Qt debugger should first see the size of the array is an element of an enum, and then assume the rest of the values of the enum are used to name the indexes of the array.
Do you know of any C++ debugger which does this? (I certainly do not, C/C++ arrays are indexed by integer not enumerated types.)
Are you making a suggestion for the devs to change the behaviour of Creator (or even of gdb)? In which case be aware that posting here is not going to cause that.
-
@JonB said in QtCreator debugger: display enum texts when used as array indexes, instead of 0,1,2...:
@Cedric-air said in QtCreator debugger: display enum texts when used as array indexes, instead of 0,1,2...:
For this to work, the Qt debugger should first see the size of the array is an element of an enum, and then assume the rest of the values of the enum are used to name the indexes of the array.
Do you know of any C++ debugger which does this?
I don't know any C nor C++ debugger which does this.
Are you making a suggestion for the devs to change the behaviour of Creator (or even of gdb)? In which case be aware that posting here is not going to cause that.
I am indeed making a suggestion for the devs to change the behavior of QtCreator (or even of gdb). What can I do to cause such a change? What's a better place to post this suggestion?
-
@Cedric-air said in QtCreator debugger: display enum texts when used as array indexes, instead of 0,1,2...:
What can I do to cause such a change
Post it here https://cc-github.bmwgroup.net/swh/adp/pull/154534 if it is for QtCreator.
If it is for GDB, then research where to post change requests for GDB. -
@Cedric-air
For a change to Creator you would need to raise it at https://bugreports.qt.io/, or even join the developers' discussion group to pre-discuss your suggestion. [ @jsulm's post crossed with this, you may use whatever link he supplies.]For gdb you would need to do similar in a gdb developers forum.
I would suggest neither of these are going to change their behaviour at this point, but that's for you to discover.
I am not sure that the Creator debugging could do this even if it wanted to. Creator does not have its own debugger, it sits atop gdb/gdbserver. And I don't think those would supply any information along the lines of "the source code declared the array size using a value from an enumerated type" (as opposed to an integer, which it got converted to) for the debugging engine to even be aware of.
-
@jsulm said in QtCreator debugger: display enum texts when used as array indexes, instead of 0,1,2...:
@Cedric-air said in QtCreator debugger: display enum texts when used as array indexes, instead of 0,1,2...:
What can I do to cause such a change
Post it here https://cc-github.bmwgroup.net/swh/adp/pull/154534 if it is for QtCreator.
If it is for GDB, then research where to post change requests for GDB.https://cc-github.bmwgroup.net/swh/adp/pull/154534 is a dead link. I've posted it here instead:
https://bugreports.qt.io/browse/QTCREATORBUG-30160 -
Christian Ehrlicher Lifetime Qt Championreplied to Cedric-air on last edited by Christian Ehrlicher
@Cedric-air t.b.h. I would expect that the debugger is using the enum types as it does now - an array is indexed by an integral, not an enum. And it's for sure not a QtCreator bug as @JonB already explained to you.
-
@Cedric-air
IMHO the issue is very simple.
You are using a standard array, which most likely has an index of the type size_t. Whatever you use as a replacement for that type, will be implicitly cast to size_t. The debugger will debug a size_t value, because the array index is of that type. That is how gdb is implemented, and I don’t see an alternative suggestion could have any merits. How should the array know about the enum?What can be done, is implementing your own array as a template. Its index could be typed to your enum and would be debugged accordingly.
-
@Axel-Spoerl said in QtCreator debugger: display enum texts when used as array indexes, instead of 0,1,2...:
What can be done, is implementing your own array as a template. Its index could be typed to your enum and would be debugged accordingly.
Have you actually tried this (not saying you haven't) to address what the OP is asking for? I might have a look at it tomorrow....
It's not a question of showing a value as an enumerated value, the OP wants the indexes into an array to be shown as such in the debugger's Watch pane. I imagine that handling of showing the available indexes as a sub-list of an array-type variable must be a feature of debugger code, and I would have thought (guess) it would be constrained to showing integers increasing from 0, no?
-
@JonB
Qt Creator uses ptrace and gdb/lldb for that purpose. And an array index is just an int, or size_t, depending on C++ version and compiler. Re-translating that back into an enum, goes a long way. I doubt it will be implemented. But I may be wrong… -
@Axel-Spoerl said in QtCreator debugger: display enum texts when used as array indexes, instead of 0,1,2...:
Re-translating that back into an enum, goes a long way. I doubt it will be implemented.
Which is all I said :)