Skip to content

Commit 4e2d40a

Browse files
authored
Merge pull request #484 from geoffw0/limitedscopefile
CPP: Fix Limitedscopefile.ql
2 parents 1b12e84 + 9922e36 commit 4e2d40a

File tree

8 files changed

+43
-0
lines changed

8 files changed

+43
-0
lines changed

change-notes/1.19/analysis-cpp.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
|----------------------------|------------------------|------------------------------------------------------------------|
1818
| Empty branch of conditional | Fewer false positive results | The query now recognizes commented blocks more reliably. |
1919
| Expression has no effect | Fewer false positive results | Expressions in template instantiations are now excluded from this query. |
20+
| Global could be static | Fewer false positive results | Variables with declarations in header files are now excluded from this query. |
2021
| Resource not released in destructor | Fewer false positive results | Placement new is now excluded from the query. Also fixed an issue where false positives could occur if the destructor body was not in the snapshot. |
2122
| Missing return statement (`cpp/missing-return`) | Visible by default | The precision of this query has been increased from 'medium' to 'high', which makes it visible by default in LGTM. It was 'medium' in release 1.17 and 1.18 because it had false positives due to an extractor bug that was fixed in 1.18. |
2223
| Missing return statement | Fewer false positive results | The query is now produces correct results when a function returns a template-dependent type. |

cpp/ql/src/JPL_C/LOC-3/Rule 13/LimitedScopeFile.ql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ from GlobalVariable v
1212
where forex(VariableAccess va | va.getTarget() = v | va.getFile() = v.getDefinitionLocation().getFile())
1313
and not v.hasSpecifier("static")
1414
and strictcount(v.getAnAccess().getEnclosingFunction()) > 1 // If = 1, variable should be function-scope.
15+
and not v.getADeclarationEntry().getFile() instanceof HeaderFile // intended to be accessed elsewhere
1516
select v, "The global variable " + v.getName() + " is not accessed outside of " + v.getFile().getBaseName()
1617
+ " and could be made static."

cpp/ql/src/Power of 10/Rule 6/GlobalCouldBeStatic.ql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ from GlobalVariable v
1212
where forex(VariableAccess va | va.getTarget() = v | va.getFile() = v.getDefinitionLocation().getFile())
1313
and not v.hasSpecifier("static")
1414
and strictcount(v.getAnAccess().getEnclosingFunction()) > 1 // If = 1, variable should be function-scope.
15+
and not v.getADeclarationEntry().getFile() instanceof HeaderFile // intended to be accessed elsewhere
1516
select v, "The global variable " + v.getName() + " is not accessed outside of " + v.getFile().getBaseName() +
1617
" and could be made static."
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| file1.c:3:5:3:14 | globalInt1 | The global variable globalInt1 is not accessed outside of file1.c and could be made static. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
JPL_C/LOC-3/Rule 13/LimitedScopeFile.ql
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// file1.c
2+
3+
int globalInt1; // BAD [only accessed in this file]
4+
int globalInt2; // GOOD [accessed in file1.c and file2.c]
5+
int globalInt3; // GOOD [referenced in file1.h]
6+
int globalInt4; // GOOD [only accessed in one function, should be function scope instead]
7+
int globalInt5; // GOOD [not accessed]
8+
9+
static int staticInt1; // GOOD [static]
10+
11+
void file1Func1()
12+
{
13+
globalInt1++;
14+
globalInt2++;
15+
globalInt3++;
16+
globalInt4++;
17+
staticInt1++;
18+
}
19+
20+
void file1Func2()
21+
{
22+
globalInt1++;
23+
globalInt3++;
24+
staticInt1++;
25+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// file1.h
2+
3+
extern int globalInt3;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// file2.c
2+
3+
#include "file1.h"
4+
5+
extern int globalInt2;
6+
7+
void file2Func()
8+
{
9+
globalInt2++;
10+
}

0 commit comments

Comments
 (0)