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
2222where
2323 not isExcluded ( v , DeadCodePackage:: unusedGlobalOrNamespaceVariableQuery ( ) ) and
2424 // 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 )
2628select v , "Variable " + v .getQualifiedName ( ) + " is unused."
Original file line number Diff line number Diff line change 2525 // No variable access
2626 not exists ( v .getAnAccess ( ) ) and
2727 // 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 )
2931select 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; }
4141m1 (); // ignore dead code in macros
4242} // namespace N1
4343
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() {
4747 d.getT ();
4848}
4949
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+
5064} // namespace test
Original file line number Diff line number Diff line change 11import cpp
22import codingstandards.cpp.FunctionEquivalence
3+ import codingstandards.cpp.Scope
34
45/**
56 * A type that contains a template parameter type (doesn't count pointers or references).
@@ -121,3 +122,17 @@ class UserProvidedConstructorFieldInit extends ConstructorFieldInit {
121122 not getEnclosingFunction ( ) .isCompilerGenerated ( )
122123 }
123124}
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