Repository navigation
Conversation
orbitcowboy
left a comment
There was a problem hiding this comment.
Thanks. Are other operators like %, * or / ? Are they handled already?
Those operators are not valid with pointers. |
|
Thanks! Did you try += and -= operators as well? |
Thats a good point. Let me add those. |
|
So it seems dataflow analysis doesn't work that well with |
danmar
left a comment
There was a problem hiding this comment.
Thanks! I have a few stylistic nits, but overall it looks promising.
| if (value->condition && !_settings->isEnabled(Settings::WARNING)) | ||
| continue; | ||
| arithmeticError(tok,value); | ||
| if (tok->str() == "-" || tok->str() == "+" || tok->str() == "+=" || tok->str() == "-=") |
There was a problem hiding this comment.
I prefer early continue. And a Token::Match should be used:
if (!Token::Match(tok, "-|+|+=|-="))
continue;
| if (tok->str() == "-" || tok->str() == "+" || tok->str() == "+=" || tok->str() == "-=") | ||
| { | ||
| const Token *arguments[] = {tok->astOperand1(), tok->astOperand2()}; | ||
| for(const Token *argument:arguments) { |
There was a problem hiding this comment.
Unfortunately, we have not started using range for loops yet. We want to support some old compilers that only has partial c++11 support.
There was a problem hiding this comment.
What C++11 features are allowed?
There was a problem hiding this comment.
c++11 should be mostly avoided. off the top of my head we do use; nullptr, long long, r-value references, right angle brackets. The nullptr can be emulated easily if the compiler does not support it.
Even though I'd say we have been very conservative, people are sometimes contacting me needing help to compile with old versions of GCC.
| { | ||
| const Token *arguments[] = {tok->astOperand1(), tok->astOperand2()}; | ||
| for(const Token *argument:arguments) { | ||
| if(argument && argument->valueType() && argument->valueType()->pointer) { |
There was a problem hiding this comment.
I'd prefer early continue(s) here.
| // checking for zero we check that the value is equal | ||
| // to RHS | ||
| if (tok->astOperand2()->hasKnownIntValue()) { | ||
| if (tok->str() == "-=") checkValue -= tok->astOperand2()->values().front().intvalue; |
There was a problem hiding this comment.
please write the conditional code on its own line to match the normal cppcheck coding style.
if (tok->str() == "-=")
checkValue -= tok->astOperand2()->values().front().intvalue;
| arithmeticError(tok,value); | ||
| if (tok->str() == "-" || tok->str() == "+" || tok->str() == "+=" || tok->str() == "-=") | ||
| { | ||
| const Token *arguments[] = {tok->astOperand1(), tok->astOperand2()}; |
There was a problem hiding this comment.
Imho operands would be a better name.
|
Did you take increment and decrement-operators into account? For example: |
danmar
left a comment
There was a problem hiding this comment.
some more stylistic nits
| continue; | ||
| arithmeticError(tok,value); | ||
| const Token *operands[] = {tok->astOperand1(), tok->astOperand2()}; | ||
| for(const Token **operand_it = begin(operands); operand_it != end(operands);operand_it++) { |
There was a problem hiding this comment.
I am not sure about this code.
How about:
for (int i = 1; i <= 2; i++) {
const Token * const operand = (i==1) ? tok->astOperand1() : tok->astOperand2();
Or how about breaking out the for loop body in a function so no for loop is needed.
There was a problem hiding this comment.
Whats wrong with the regular for loop there? It follows the idiomatic for loop over a range in C++98:
Range r;
for(Iterator it = begin(r); it != end(r);r++)
...There was a problem hiding this comment.
std::begin and std::end are not available in c++98. and I believe, unlike those functions you are not using an iterator.
There was a problem hiding this comment.
std::begin and std::end are not available in c++98.
They are available as member functions in C++98. In C++11, they added free functions, so they can be used on arrays.
Also, I added begin and end function for arrays in utils.h, so they are available in C++98.
believe, unlike those functions you are not using an iterator.
A pointer meets all the requirements for a random access iterator. In fact, std::iterator_traits are specialized for pointers. This C++98 program prints out "Pointer is a random access iterator":
#include <iterator>
#include <cstdio>
char is_random_access(std::random_access_iterator_tag);
long is_random_access(...);
int main() {
if (sizeof(is_random_access(std::iterator_traits<int*>::iterator_category())) == 1)
printf("Pointer is a random access iterator\n");
}
There was a problem hiding this comment.
sorry for late reply. I would like some kind of refactoring. I am skeptic about adding these begin and end. I think that range for loops would be elegant, the day we start using c++11 I want to use range for loops here.
I know that pointers can be used but I am not sure that it is specified they are used. For instance sometimes debug random access iterators implement extra runtime checks. access out of bounds etc. That can not be implemented in the library if it just uses a pointer typedef.
There was a problem hiding this comment.
How about skipping the for loop.
const Token *pointerOperand;
if (tok->astOperand1()->valueType() && tok->astOperand1()->valueType()->pointer>0)
pointerOperand = tok->astOperand1();
else if (tok->astOperand2()->valueType() && tok->astOperand2()->valueType()->pointer>0)
pointerOperand = tok->astOperand2();
else
continue;
There was a problem hiding this comment.
sorry for late reply. I would like some kind of refactoring. I am skeptic about adding these begin and end. I think that range for loops would be elegant, the day we start using c++11 I want to use range for loops here.
Well with PR #1138, I added a foreach emulation for older compilers so we can start using this now.
I know that pointers can be used but I am not sure that it is specified they are used.
For iterator_traits, the standard library is required to provide the pointer specialization, and the begin and end array overloads are specified to return pointers as well(see here).
For instance sometimes debug random access iterators implement extra runtime checks. access out of bounds etc.
That is not a requirement for an iterator, and in fact, some older STL implementations used raw pointers directly.
That can not be implemented in the library if it just uses a pointer typedef.
Is it necessary for the iterators to operands to have out of bounds check for debug? Accessing the array directly with operands[i] has no out of bounds check in debug, but I could add iterators with debug checking if you think its necessary.
| const Token *operand = *operand_it; | ||
| if(!(operand && operand->valueType() && operand->valueType()->pointer)) | ||
| continue; | ||
| long checkValue = 0; |
There was a problem hiding this comment.
I suggest MathLib::bigint instead of long.
I added those. |
|
Thanks! I applied the changes with d240a36 |
Check for addition with null pointer as it is most likely an error: