Variable Access Control in C / C ++

I am working on a criterion for the coverage of multithreaded code, and as part of it I would like to write down calls to variables. For example, in the code below, I would like to write that the variable x was written and y, z, a[i], , and I was read.

 x = y * (int)z + a[i] 

I looked at this using Clang RecursiveASTVisitor and changing the source to enable recording functions. However, I'm not sure if this is a reasonable approach, as my understanding of how Klang works is very incomplete.

Currently, when I find the instruction, I check if it is BinaryOperator, UnaryOperator, Cast, or DeclRefExpr . (I will expand on what I am capable of as soon as my foundations are based.) If it is a BinaryOperator, UnaryOperator, or Cast , I check the subexpressions of the expression. If it is DeclRefExpr, I can check if the expression is lvalue or rvalue (again, simplifying now), but as soon as I found DeclRefExpr, they are always lvalues. To determine if they were used as lvalues or rvalues , I have to check its parent object, if it was an lvaluetorvalue sheet, it was used as an rvalue.

I really like that I am using this problem incorrectly because I see that it is becoming much more complicated, since I have to consider more complex code.

Would there be a better way to get close to this?

thanks

Edit

I am not going to write this information statically. I intend to find the use of variables and insert code that will record calls to these variables when the code runs.

For example, given the code above ( x = y * (int)z + a[i]; ), I would like to create something like

 x = y * (int)z + a[i]; recordAccess(<file>, <line>, "x", &x, WRITE); recordAccess(<file>, <line>, "y", &y, READ); recordAccess(<file>, <line>, "z", &z, READ); recordAccess(<file>, <line>, "a[i]", &a[i], READ); recordAccess(<file>, <line>, "i", &i, READ); 
+8
c ++ c clang code-coverage
source share
2 answers

As others have pointed out, smoothing makes this impossible. Static code analysis is not possible to answer your questions. If it would be possible to take the source code file and determine the result, simply by analyzing the syntax, compilers will generate the result of the resulting program instead of the compiled program. In short, you are trying to answer a problem.

Dynamic analysis is what you really need to answer your questions. The dynamic analysis of multithreaded software already has a large market.

+1
source share

The main problem here is that you are not considering aliasing. You can record simple, direct access.

But in this case, a simple expression AST visitor is the main way. But Clang RecursiveASTVisitor should, from memory, be able to cut shit for you and allow you to directly visit the final variable nodes. In the end, he must visit every AST node.

0
source share

All Articles