1313import cpp
1414import codingstandards.c.misra
1515
16+ /** Pointer to Wide character type, i.e. `wchar_t*`. */
17+ class WideCharPointerType extends PointerType {
18+ WideCharPointerType ( ) { this .getBaseType ( ) instanceof Wchar_t }
19+
20+ override string getAPrimaryQlClass ( ) { result = "WideCharPointerType" }
21+ }
22+
23+ class GenericCharPointerType extends PointerType {
24+ GenericCharPointerType ( ) {
25+ /* This type resolves to wchar_t* (which is in turn a typedef depending on its implementation) */
26+ this .resolveTypedefs * ( ) instanceof WideCharPointerType
27+ or
28+ /* This type eventually resolves to char* */
29+ this .resolveTypedefs * ( ) instanceof CharPointerType
30+ }
31+
32+ predicate isWideCharPointerType ( ) { this .resolveTypedefs * ( ) instanceof WideCharPointerType }
33+
34+ override string toString ( ) {
35+ if this .isWideCharPointerType ( ) then result = "wchar_t*" else result = "char*"
36+ }
37+ }
38+
1639class NonConstCharStarType extends Type {
1740 NonConstCharStarType ( ) {
18- this instanceof CharPointerType and
41+ this instanceof GenericCharPointerType and
1942 not this .isDeeplyConstBelow ( )
2043 }
2144}
@@ -24,40 +47,48 @@ class NonConstCharStarType extends Type {
2447predicate declaringNonConstCharVar ( Variable decl , string message ) {
2548 not decl instanceof Parameter and // exclude parameters
2649 /* It should be declaring a char* type variable */
27- decl .getUnspecifiedType ( ) instanceof CharPointerType and
28- not decl .getUnderlyingType ( ) .isDeeplyConstBelow ( ) and
29- /* But it's declared to hold a string literal. */
50+ decl .getType ( ) instanceof GenericCharPointerType and
51+ not decl .getType ( ) .isDeeplyConstBelow ( ) and
52+ /* But it's declared to hold a string literal. */
3053 decl .getInitializer ( ) .getExpr ( ) instanceof StringLiteral and
31- message = "char* variable " + decl + " is declared with a string literal."
54+ message =
55+ decl .getType ( ) .( GenericCharPointerType ) + " variable " + decl +
56+ " is declared with a string literal."
3257}
3358
3459/* String literal being assigned to a non-const-char* variable */
3560predicate assignmentToNonConstCharVar ( Assignment assign , string message ) {
3661 /* The variable being assigned is char* */
37- assign .getLValue ( ) .getUnderlyingType ( ) instanceof NonConstCharStarType and
62+ assign .getLValue ( ) .getType ( ) instanceof NonConstCharStarType and
3863 /* But the rvalue is a string literal */
39- exists ( Expr rvalue | rvalue = assign .getRValue ( ) | rvalue instanceof StringLiteral ) and
40- message = "char* variable " + assign .getLValue ( ) + " is assigned a string literal. "
64+ assign .getRValue ( ) instanceof StringLiteral and
65+ message =
66+ assign .getLValue ( ) .getType ( ) .( GenericCharPointerType ) + " variable " + assign .getLValue ( ) +
67+ " is assigned a string literal. "
4168}
4269
4370/* String literal being passed to a non-const-char* parameter */
4471predicate assignmentToNonConstCharParam ( FunctionCall call , string message ) {
4572 exists ( int index |
4673 /* Param at index is a char* */
47- call .getTarget ( ) .getParameter ( index ) .getUnderlyingType ( ) instanceof NonConstCharStarType and
74+ call .getTarget ( ) .getParameter ( index ) .getType ( ) instanceof NonConstCharStarType and
4875 /* But a string literal is passed */
49- call .getArgument ( index ) instanceof StringLiteral
50- ) and
51- message = "char* parameter of " + call .getTarget ( ) + " is passed a string literal."
76+ call .getArgument ( index ) instanceof StringLiteral and
77+ message =
78+ call .getTarget ( ) .getParameter ( index ) .getType ( ) .( GenericCharPointerType ) + " parameter of " +
79+ call .getTarget ( ) + " is passed a string literal."
80+ )
5281}
5382
5483/* String literal being returned by a non-const-char* function */
5584predicate returningNonConstCharVar ( ReturnStmt return , string message ) {
5685 /* The function is declared to return a char* */
57- return .getEnclosingFunction ( ) .getType ( ) . resolveTypedefs ( ) instanceof NonConstCharStarType and
86+ return .getEnclosingFunction ( ) .getType ( ) instanceof NonConstCharStarType and
5887 /* But in reality it returns a string literal */
5988 return .getExpr ( ) instanceof StringLiteral and
60- message = "char* function " + return .getEnclosingFunction ( ) + " is returning a string literal."
89+ message =
90+ return .getEnclosingFunction ( ) .getType ( ) .( GenericCharPointerType ) + " function " +
91+ return .getEnclosingFunction ( ) + " is returning a string literal."
6192}
6293
6394from Element elem , string message
0 commit comments