Library configuration: function argument direction fixes and enhancements#1722
Conversation
|
The relevant test is: I don't really get it. This test seems to depend on the direction of the first argument, which i set to "out". |
I did not check carefully but I assume maybe |
|
@danmar: we could name it direction-ptr? |
|
Or even better: we introduce a type-option. So each argument can be associated with its corresponding argument data type. |
|
Hmm ok. My assumption was that the content is meant (memory where the pointer points to). I believe IMHO direction-ptr is misleading if the content is meant. direction-data would be good. |
|
IIRC in the Microsoft SAL the direction is also specified for either the variable or the memory a pointer points to. But i have to read over it again. IMHO it would make sense to let Cppchecks library configuration work similar if there is not a good reason to do otherwise. |
yes it does. it is confusing. I would not be against that we define some new better configuration names.. The |
Yes that makes sense |
|
Here is a link to the Microsoft SAL: So, how do we define the currently implemented "in", "out" and "inout" exactly? I would add a corresponding comment to the library.cpp or so. |
|
It is fine to match the msdn SAL annotation. but we need to determine if If you have this: Then the "refout" should be configured instead of "out". |
|
I do not see why refout or something is necessary. If the argument is configured as out and a simple variable is given like in your example Cppcheck could be sure that it must be handed over by reference. What am i missing here? |
|
ok.. well I am satisfied with this in/out/inout definition. We can probably configure 99% of the functions with that definition. one case that cannot be configured then would for instance be: |
|
Ah ok. A reference to a pointer is a case i have not thought of. I am now pro refptr 😀 |
|
hmm.. we also have the option to add argument type information. if the argument type is "int *&" for a "out" parameter then we can assume that it might be changed. If the argument type is "int *" then it is obvious it won't be changed. |
|
I would recommend that in the near future we will not configure the direction at all for "refptr" cases. If no configuration is seen cppcheck should make the safe guess. Then we should be able to configure 99% of the functions properly now. Some day in the future we can add some "refout" or argument type info also.. |
|
Ok. And how can we fix this PR? |
Can you try to change your fix in I am guessing that if you ignore the direction when |
ded7bcb to
e1e9d91
Compare
I have changed the code to this: if (!tok->function()) {
// Check if direction (in, out, inout) is specified in the library configuration and use that
if (!addressOf && settings) {
// With in, out, inout the direction of the content is specified, not for a pointer itself
const ValueType * const valueType = tok->valueType();
if (valueType && !valueType->pointer) {
const Library::ArgumentChecks::Direction argDirection = settings->library.getArgDirection(tok, 1 + argnr);
if (argDirection == Library::ArgumentChecks::Direction::DIR_IN)
return false;
else if (argDirection == Library::ArgumentChecks::Direction::DIR_OUT ||
argDirection == Library::ArgumentChecks::Direction::DIR_INOUT)
return true;
}
}But this can not be correct i think. |
Aha .. sorry my mistake. The |
Also fix further unmatched uninitvar messages in std configuration tests.
|
Thanks. With I have added a test to verify that the direction is correctly loaded from the library configuration. |
ok I guess most tests call some function Here is a code example: Cppcheck writes an inconclusive warning for this about array index out of bounds. But if the I hope this can be fixed by your code. |
Yes, that really works. The inconclusive goes away :) Many of the functions in the tests are named |
|
Yes it's convoluted, not sure if we can "reduce it". otherwise I suggest that it is added to test/cfg/std.c .. I also think that testastutils should have simple tests to ensure the function returns false when the direction is in (no matter if it's a pointer or not?). |
test/cfg/std.c sounds good, i will add it there.
I also thought about returning false when it is an "in" argument even when it is a pointer. |
| enum Direction { DIR_IN, DIR_OUT, DIR_INOUT, DIR_UNKNOWN }; | ||
| enum Direction { | ||
| DIR_IN, ///< argument points to / references memory that the function only reads | ||
| DIR_OUT, ///< argument points to / references memory that the function writes to (do not specify for references to pointers) |
There was a problem hiding this comment.
@danmar Does that description make sense, any suggestions for improvement?
There was a problem hiding this comment.
We could explicitly mention in a comment above the enum that we use a Microsoft SAL compatible definition.
My suggestion (inspired by the SAL documentation)
DIR_IN Input to called function, Data is treated as read-only.
DIR_INOUT Input to called function, and output to caller. Data is passed by reference or address and is potentially modified.
DIR_OUT Output to caller. Data is passed by reference or address and is potentially written.
if you mean "inout" also.. then yes! |
|
as far as I know we use definitions here that are compatible with Microsoft SAL. So we could extract the SAL definitions from the microsoft headers and put the information in our cfg files. If we write a tool that does this then maybe users could use that on their own code even and get automatically generated cfg files. |
yes, of course. I will change it. |
IMHO that is a hard task. It would be great if we have such a tool, but it really is not easy to implement i guess. The headers are complex and so is the Microsoft SAL. It is not impossible, but a huge task i think. |
|
@danmar I think this PR is ready to merge, do you agree? |
| enum Direction { DIR_IN, DIR_OUT, DIR_INOUT, DIR_UNKNOWN }; | ||
| enum Direction { | ||
| DIR_IN, ///< argument points to / references memory that the function only reads | ||
| DIR_OUT, ///< argument points to / references memory that the function writes to (do not specify for references to pointers) |
There was a problem hiding this comment.
We could explicitly mention in a comment above the enum that we use a Microsoft SAL compatible definition.
My suggestion (inspired by the SAL documentation)
DIR_IN Input to called function, Data is treated as read-only.
DIR_INOUT Input to called function, and output to caller. Data is passed by reference or address and is potentially modified.
DIR_OUT Output to caller. Data is passed by reference or address and is potentially written.
danmar
left a comment
There was a problem hiding this comment.
If you tweak the enum comment then I am happy about these changes.. feel free to merge this when you feel ready!
No description provided.