Framework and proof of concept of a write-time analysis of the value of a variable in c++ in the KDevelop UI

Overview:

In many programming environments, when the program is running, the mouse cursor can be hovered over a variable where upon a tooltip is shown giving the current value of the tooltip. Some UI’s go a bit further and attempt to do some analysis of what exactly is highlighted. For example is you select “x+2” and x is currently 3, then it will show that x+2 is 5.

No programming environment however, to my knowledge, can do such analysis at write time. For example if we have:

int x = 2; x += 2;

Then my code will show that x = 4 when you hover over x after the second line.

If you have:

int doubleAndAddOne(int x) { int y= 2*x; y++; y++; return y; }

Then hovering over the last value of y will show “y = 2*x +2“ using a mixture of code analysis and symbolic mathematics.

The aim is to also be able to support simple for loops:

y = 0; for( int i = 0; i< 10; i++) { y+=i; } return y;

Hovering over the last value of y will show that y = 45, as long as the machine is fast enough to process the for-loop within the time allocated. Please note that this will not be done by running the code through an interpreter. The idea is that this will be a single pass algorithm so that internally y will be analysed as:

y = sum( 0 to 9)

Current State:

Currently the c++ part in KDevelop has a code analysis engine that can detect syntax errors. This parses the c++ file and contains knowledge about where the functions are. It can also tokenize the c++ files, and I feel that I can extend this with hooks so that I can do the code analysis that I require on this directly. Keeping correct state information as the program is changed by the user will be tricky, but I feel it will be doable.

Future work:

GUI

From a GUI perspective I want to show the value of a variable as a mathml equation. Particularly if the result is symbolic (y=x/2 for example). I would also like to show the values in the code.

For example, the user would see the following: (Note the equation isn’t saved in the file)

44 …
45 void f(int x) {
46	y = 0;
47	for(int x = 0; x < 5; x++) {
48		y+= 2*x;
49	}
50	return y;
<mathml equation here>y = sum(2x,from x=0 to 5) = 31</mathml>
51 }

So that with a single quick glance you can see what function f does. This could work with code folding so that the actual code could be folded leaving only the math equation.

This would require MathML support. I am currently trying to organise this but it will take at least 6 months before kdom and MathML are in a state that can be used for this.

Code

More and more complex code could be handled. I don’t know how far this can go. I’m optimistic and think this can be quite useful in real world code.

Types

Future work could also be done on extending the types it can handle. Strings shouldn’t be too hard to cope with, with special cases for QString etc. Handling QT specific types should probably be done as some form of extension that’s loaded when the project is recognised as a KDE project.

Credentials:

Why do I feel I can do this? I have a strong passion with symbolic equations linked with code. I gained 95%+ in my formal computing mathematics university classes, where a first is 70%.
I have also followed with interest the code analysis code in kdevelop and after many years I feel it is in the state where this could be done.

[ Edit ]