Opened 17 years ago
Last modified 16 years ago
#395 new enhancement
macro and variable have the same name
| Reported by: | ettlmartin | Owned by: | noone |
|---|---|---|---|
| Priority: | Milestone: | ||
| Component: | New check | Version: | |
| Keywords: | preprocessor check | Cc: |
Description
consider the code below
#include <iostream>
int main()
{
int foo=1;
#define foo (4 + foo)
std::cout << "foo: " <<foo << std::endl;
}
the macro definition and the integer variable have the same name. This can result in unpredicted results.
running the testprogramm prints
foo:5
Best regards
Martin
Change History (5)
follow-up: 3 comment:1 by , 17 years ago
comment:2 by , 17 years ago
to hyd_danmar: This is a new check request. It was asked that we check if variable and macro have the same name and print error (style?) about it.
E.g. something like:
[file.cpp:6] (style) Macro and variable foo share the same name. Consider renaming either one to avoid confusion.
I think this could be a valid for style error.
comment:3 by , 17 years ago
Replying to hyd_danmar:
Can you clarify the problem?
cppcheck will tokenize and simplify the code into:
##file 0 1: int main ( ) 2: { 3: int foo@1 ; foo@1 = 1 ; 4: 5: std :: cout << "foo: " << ( 4 + foo@1 ) << std :: endl ; 6: }And to me that looks correct. I would prefer though if the "foo@1" on line 5 were simplified to "1".
The issue in the code above is that a macro definition has the same name as a variable. I see, its correctly tokenized by cppcheck. Nevertheless, this can lead (in larger projects) to unpredicted output/behaviour. Because these are issues, that hard to find. Imagine you have thousends lines of code.... so it would be really helpful if cppcheck could warn about that. Maybe as a style error?
Best regards
Martin
comment:5 by , 16 years ago
| Keywords: | preprocessor check added |
|---|
IMHO this is a bit out of scope. Because it can't be checked in the token lists.
Can you clarify the problem?
cppcheck will tokenize and simplify the code into:
##file 0 1: int main ( ) 2: { 3: int foo@1 ; foo@1 = 1 ; 4: 5: std :: cout << "foo: " << ( 4 + foo@1 ) << std :: endl ; 6: }And to me that looks correct. I would prefer though if the "foo@1" on line 5 were simplified to "1".