codeql icon indicating copy to clipboard operation
codeql copied to clipboard

[cpp] for C code, query variable does not extract all variables (mostly const variable and not ram variable)

Open totocaca123 opened this issue 1 year ago • 3 comments

I have a database biuld with gcc or clang. In my C code which compiles I want to extract all variables defined in my project.

I get all const variable but nothing concerning static variable or gllobal variable defined in compiled source.

doxygen is able to extract these variables

totocaca123 avatar Mar 20 '24 23:03 totocaca123

Could you please give a short example of a C program illustrating your problem and the CodeQL you use to query it that produces an unexpected result?

smowton avatar Mar 21 '24 10:03 smowton

Here is my request:

import cpp
from Variable v
where
  v.isStatic()
select v.getLocation(), v, v.getType(), v.getInitializer().getExpr(), 
v.getLocation().getFile().getBaseName()

My aim is to watch all static variables. It shows me only variables which are initialized. In my code, some variables are not initialized for example static int foo;

When I remove v.getInitializer().getExpr(), problem is solved. I expect that select doesn't apply a filter if initializer doesn't exists I don't know if it is the intended behavious from a database or a bug

totocaca123 avatar Mar 21 '24 23:03 totocaca123

To me this looks like intended behavior.

What your select clause basically does, is returning a tuple of values. In your case that tuple contains 5 values: v.getLocation() v v.getType() v.getInitializer().getExpr() v.getLocation().getFile().getBaseName()

Now if v.getInitializer().getExpr() is the empty set, i.e., there is no expression, then CodeQL discards the whole tuple, because no value in a tuple can be the empty set.

intrigus-lgtm avatar Mar 26 '24 22:03 intrigus-lgtm