Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2d75523

Browse files
committed
C++: Put in a better fix.
1 parent a1c7fd8 commit 2d75523

3 files changed

Lines changed: 20 additions & 8 deletions

File tree

cpp/ql/src/Best Practices/Hiding/DeclarationHidesParameter.ql

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,21 @@
1111

1212
import cpp
1313

14+
/**
15+
* Gets the template that a function `f` is constructed from, or just `f` if it
16+
* is not from a template instantiation.
17+
*/
18+
Function getConstructedFrom(Function f)
19+
{
20+
exists(Function mid |
21+
f.isConstructedFrom(mid) and
22+
result = getConstructedFrom(mid)
23+
) or (
24+
not f.isConstructedFrom(_) and
25+
result = f
26+
)
27+
}
28+
1429
/**
1530
* Gets the parameter of `f` with name `name`, which has to come from the
1631
* _definition_ of `f` and not a prototype declaration.
@@ -22,7 +37,7 @@ import cpp
2237
ParameterDeclarationEntry functionParameterNames(Function f, string name) {
2338
exists(FunctionDeclarationEntry fe |
2439
result.getFunctionDeclarationEntry() = fe and
25-
fe.getFunction() = f and
40+
getConstructedFrom(f).getDefinition() = fe and
2641
fe.getLocation() = f.getDefinitionLocation() and
2742
strictcount(f.getDefinitionLocation()) = 1 and
2843
result.getName() = name
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
| hiding.cpp:4:17:4:18 | ii | Local variable 'ii' hides a $@. | hiding.cpp:2:12:2:13 | definition of ii | parameter of the same name |
22
| hiding.cpp:15:15:15:16 | kk | Local variable 'kk' hides a $@. | hiding.cpp:12:25:12:26 | definition of kk | parameter of the same name |
33
| hiding.cpp:28:7:28:7 | a | Local variable 'a' hides a $@. | hiding.cpp:26:21:26:21 | definition of a | parameter of the same name |
4-
| hiding.cpp:45:7:45:7 | a | Local variable 'a' hides a $@. | hiding.cpp:39:20:39:20 | definition of a | parameter of the same name |
5-
| hiding.cpp:47:7:47:7 | c | Local variable 'c' hides a $@. | hiding.cpp:39:34:39:34 | definition of c | parameter of the same name |
4+
| hiding.cpp:45:7:45:7 | a | Local variable 'a' hides a $@. | hiding.cpp:43:41:43:41 | definition of a | parameter of the same name |
65
| hiding.cpp:64:11:64:11 | i | Local variable 'i' hides a $@. | hiding.cpp:61:20:61:20 | definition of i | parameter of the same name |
7-
| hiding.cpp:76:7:76:15 | protoArg1 | Local variable 'protoArg1' hides a $@. | hiding.h:5:21:5:29 | definition of protoArg1 | parameter of the same name |
8-
| hiding.cpp:77:5:77:13 | protoArg2 | Local variable 'protoArg2' hides a $@. | hiding.h:5:34:5:42 | definition of protoArg2 | parameter of the same name |
96
| hiding.cpp:78:7:78:10 | arg1 | Local variable 'arg1' hides a $@. | hiding.cpp:74:28:74:31 | definition of arg1 | parameter of the same name |
107
| hiding.cpp:79:5:79:8 | arg2 | Local variable 'arg2' hides a $@. | hiding.cpp:74:36:74:39 | definition of arg2 | parameter of the same name |

cpp/ql/test/query-tests/Best Practices/Hiding/DeclarationHidesParameter/hiding.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ void MyTemplateClass<T> :: myMethod(int a, int b, int _c) {
4444
{
4545
int a = a; // local variable hides global variable
4646
int _b = b;
47-
int c = _c; // [FALSE POSITIVE]
47+
int c = _c;
4848

4949
// ...
5050
}
@@ -73,8 +73,8 @@ void myClass::myCaller(void) {
7373
template <typename T>
7474
void myClass::myMethod(int arg1, T arg2) {
7575
{
76-
int protoArg1; // [FALSE POSITIVE]
77-
T protoArg2; // [FALSE POSITIVE]
76+
int protoArg1;
77+
T protoArg2;
7878
int arg1; // local variable hides global variable
7979
T arg2; // local variable hides global variable
8080
}

0 commit comments

Comments
 (0)