@@ -8,67 +8,91 @@ private import semmle.code.csharp.frameworks.System
8
8
private import semmle.code.csharp.frameworks.system.Text
9
9
10
10
/** A method that formats a string, for example `string.Format()`. */
11
- class FormatMethod extends Method {
12
- FormatMethod ( ) {
13
- exists ( Class declType | declType = this .getDeclaringType ( ) |
11
+ abstract class FormatMethod extends Method {
12
+ /**
13
+ * Gets the argument containing the format string. For example, the argument of
14
+ * `string.Format(IFormatProvider, String, Object)` is `1`.
15
+ */
16
+ abstract int getFormatArgument ( ) ;
17
+ }
18
+
19
+ private class StringAndStringBuilderFormatMethods extends FormatMethod {
20
+ StringAndStringBuilderFormatMethods ( ) {
21
+ (
14
22
this .getParameter ( 0 ) .getType ( ) instanceof SystemIFormatProviderInterface and
15
- this .getParameter ( 1 ) .getType ( ) instanceof StringType and
23
+ this .getParameter ( 1 ) .getType ( ) instanceof StringType
24
+ or
25
+ this .getParameter ( 0 ) .getType ( ) instanceof StringType
26
+ ) and
27
+ (
28
+ this = any ( SystemStringClass c ) .getFormatMethod ( )
29
+ or
30
+ this = any ( SystemTextStringBuilderClass c ) .getAppendFormatMethod ( )
31
+ )
32
+ }
33
+
34
+ override int getFormatArgument ( ) {
35
+ if this .getParameter ( 0 ) .getType ( ) instanceof SystemIFormatProviderInterface
36
+ then result = 1
37
+ else result = 0
38
+ }
39
+ }
40
+
41
+ private class SystemConsoleAndSystemIoTextWriterFormatMethods extends FormatMethod {
42
+ SystemConsoleAndSystemIoTextWriterFormatMethods ( ) {
43
+ this .getParameter ( 0 ) .getType ( ) instanceof StringType and
44
+ exists ( Class declType | declType = this .getDeclaringType ( ) |
45
+ this .hasName ( [ "Write" , "WriteLine" ] ) and
16
46
(
17
- this = any ( SystemStringClass c ) . getFormatMethod ( )
47
+ declType . hasFullyQualifiedName ( "System" , "Console" )
18
48
or
19
- this = any ( SystemTextStringBuilderClass c ) . getAppendFormatMethod ( )
49
+ declType . hasFullyQualifiedName ( "System.IO" , "TextWriter" )
20
50
)
21
- or
22
- this .getParameter ( 0 ) .getType ( ) instanceof StringType and
51
+ )
52
+ }
53
+
54
+ override int getFormatArgument ( ) { result = 0 }
55
+ }
56
+
57
+ private class SystemDiagnosticsDebugAssert extends FormatMethod {
58
+ SystemDiagnosticsDebugAssert ( ) {
59
+ this .hasName ( "Assert" ) and
60
+ this .getDeclaringType ( ) .hasFullyQualifiedName ( "System.Diagnostics" , "Debug" ) and
61
+ this .getNumberOfParameters ( ) = 4
62
+ }
63
+
64
+ override int getFormatArgument ( ) { result = 2 }
65
+ }
66
+
67
+ private class SystemDiagnosticsFormatMethods extends FormatMethod {
68
+ SystemDiagnosticsFormatMethods ( ) {
69
+ this .getParameter ( 0 ) .getType ( ) instanceof StringType and
70
+ exists ( Class declType |
71
+ declType = this .getDeclaringType ( ) and
72
+ declType .getNamespace ( ) .getFullName ( ) = "System.Diagnostics"
73
+ |
74
+ declType .hasName ( "Trace" ) and
23
75
(
24
- this = any ( SystemStringClass c ) .getFormatMethod ( )
25
- or
26
- this = any ( SystemTextStringBuilderClass c ) .getAppendFormatMethod ( )
27
- or
28
- ( this .hasName ( "Write" ) or this .hasName ( "WriteLine" ) ) and
29
- (
30
- declType .hasFullyQualifiedName ( "System" , "Console" )
31
- or
32
- declType .hasFullyQualifiedName ( "System.IO" , "TextWriter" )
33
- or
34
- declType .hasFullyQualifiedName ( "System.Diagnostics" , "Debug" ) and
35
- this .getParameter ( 1 ) .getType ( ) instanceof ArrayType
36
- )
76
+ this .hasName ( "TraceError" )
37
77
or
38
- declType .hasFullyQualifiedName ( "System.Diagnostics" , "Trace" ) and
39
- (
40
- this .hasName ( "TraceError" ) or
41
- this .hasName ( "TraceInformation" ) or
42
- this .hasName ( "TraceWarning" )
43
- )
78
+ this .hasName ( "TraceInformation" )
44
79
or
45
- this .hasName ( "TraceInformation" ) and
46
- declType .hasFullyQualifiedName ( "System.Diagnostics" , "TraceSource" )
47
- or
48
- this .hasName ( "Print" ) and
49
- declType .hasFullyQualifiedName ( "System.Diagnostics" , "Debug" )
80
+ this .hasName ( "TraceWarning" )
50
81
)
51
82
or
52
- this .hasName ( "Assert" ) and
53
- declType .hasFullyQualifiedName ( "System.Diagnostics" , "Debug" ) and
54
- this .getNumberOfParameters ( ) = 4
83
+ declType .hasName ( "TraceSource" ) and this .hasName ( "TraceInformation" )
84
+ or
85
+ declType .hasName ( "Debug" ) and
86
+ (
87
+ this .hasName ( "Print" )
88
+ or
89
+ this .hasName ( [ "Write" , "WriteLine" ] ) and
90
+ this .getParameter ( 1 ) .getType ( ) instanceof ArrayType
91
+ )
55
92
)
56
93
}
57
94
58
- /**
59
- * Gets the argument containing the format string. For example, the argument of
60
- * `string.Format(IFormatProvider, String, Object)` is `1`.
61
- */
62
- int getFormatArgument ( ) {
63
- if this .getParameter ( 0 ) .getType ( ) instanceof SystemIFormatProviderInterface
64
- then result = 1
65
- else
66
- if
67
- this .hasName ( "Assert" ) and
68
- this .getDeclaringType ( ) .hasFullyQualifiedName ( "System.Diagnostics" , "Debug" )
69
- then result = 2
70
- else result = 0
71
- }
95
+ override int getFormatArgument ( ) { result = 0 }
72
96
}
73
97
74
98
pragma [ nomagic]
0 commit comments