File tree Expand file tree Collapse file tree 5 files changed +54
-3
lines changed
common/src/codingstandards/cpp/deadcode Expand file tree Collapse file tree 5 files changed +54
-3
lines changed Original file line number Diff line number Diff line change @@ -22,5 +22,7 @@ from PotentiallyUnusedGlobalOrNamespaceVariable v
22
22
where
23
23
not isExcluded ( v , DeadCodePackage:: unusedGlobalOrNamespaceVariableQuery ( ) ) and
24
24
// No variable access
25
- not exists ( v .getAnAccess ( ) )
25
+ not exists ( v .getAnAccess ( ) ) and
26
+ // Exclude members whose value is compile time and is potentially used to inintialize a template
27
+ not maybeACompileTimeTemplateArgument ( v )
26
28
select v , "Variable " + v .getQualifiedName ( ) + " is unused."
Original file line number Diff line number Diff line change 25
25
// No variable access
26
26
not exists ( v .getAnAccess ( ) ) and
27
27
// No explicit initialization in a constructor
28
- not exists ( UserProvidedConstructorFieldInit cfi | cfi .getTarget ( ) = v )
28
+ not exists ( UserProvidedConstructorFieldInit cfi | cfi .getTarget ( ) = v ) and
29
+ // Exclude members whose value is compile time and is potentially used to inintialize a template
30
+ not maybeACompileTimeTemplateArgument ( v )
29
31
select v , "Member variable " + v .getName ( ) + " is unused."
Original file line number Diff line number Diff line change @@ -41,4 +41,22 @@ void test_ns() { x2 = 1; }
41
41
m1 (); // ignore dead code in macros
42
42
} // namespace N1
43
43
44
- int test_access_variable () { return N1::x5; }
44
+ int test_access_variable () { return N1::x5; }
45
+
46
+ template <int t> struct C1 {
47
+ int array[t]; // COMPLIANT
48
+ };
49
+
50
+ constexpr int g5 = 1 ; // COMPLIANT - used as template parameter
51
+
52
+ namespace ns1 {
53
+ constexpr int m1 = 1 ; // COMPLIANT - used a template parameter
54
+ }
55
+
56
+ void test_fp_reported_in_384 () {
57
+ struct C1 <g5> l1;
58
+ struct C1 <ns1::m1> l2;
59
+
60
+ l1.array [0 ] = 1 ;
61
+ l2.array [0 ] = 1 ;
62
+ }
Original file line number Diff line number Diff line change @@ -47,4 +47,18 @@ void test_d() {
47
47
d.getT ();
48
48
}
49
49
50
+ template <int t> struct C1 {
51
+ int array[t]; // COMPLIANT
52
+ };
53
+
54
+ struct C2 {
55
+ static constexpr int m1 = 1 ; // COMPLIANT - used as template parameter
56
+ };
57
+
58
+ void test_fp_reported_in_384 () {
59
+ struct C1 <C2::m1> l1;
60
+
61
+ l1.array [0 ] = 1 ;
62
+ }
63
+
50
64
} // namespace test
Original file line number Diff line number Diff line change 1
1
import cpp
2
2
import codingstandards.cpp.FunctionEquivalence
3
+ import codingstandards.cpp.Scope
3
4
4
5
/**
5
6
* A type that contains a template parameter type (doesn't count pointers or references).
@@ -121,3 +122,17 @@ class UserProvidedConstructorFieldInit extends ConstructorFieldInit {
121
122
not getEnclosingFunction ( ) .isCompilerGenerated ( )
122
123
}
123
124
}
125
+
126
+ predicate maybeACompileTimeTemplateArgument ( Variable v ) {
127
+ v .isConstexpr ( ) and
128
+ exists ( ClassTemplateInstantiation cti , TranslationUnit tu |
129
+ cti .getATemplateArgument ( ) .( Expr ) .getValue ( ) = v .getInitializer ( ) .getExpr ( ) .getValue ( ) and
130
+ (
131
+ cti .getFile ( ) = tu and
132
+ (
133
+ v .getADeclarationEntry ( ) .getFile ( ) = tu or
134
+ tu .getATransitivelyIncludedFile ( ) = v .getADeclarationEntry ( ) .getFile ( )
135
+ )
136
+ )
137
+ )
138
+ }
You can’t perform that action at this time.
0 commit comments