2 * Copyright 2007-2022 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright Siemens AG 2015-2022
5 * Licensed under the Apache License 2.0 (the "License"). You may not use
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
12 * This demonstrates/tests cases where check-format.pl should report issues.
13 * Some of the reports are due to sanity checks for proper nesting of comment
14 * delimiters and parenthesis-like symbols, e.g., on unexpected/unclosed braces.
18 * The '@'s after '*' are used for self-tests: they mark lines containing
19 * a single flaw that should be reported. Normally it should be reported
20 * while handling the given line, but in case of delayed checks there is a
21 * following digit indicating the number of reports expected for this line.
24 /* For each of the following set of lines the tool should complain once */
26 /*@ intra-line carriage return character:
\r */
27 /*@ non-printable ASCII character:
\b */
28 /*@ non-ASCII character: รค */
29 /*@ whitespace at EOL: */
30 // /*@ end-of-line comment style not allowed (for C90 compatibility) */
31 /*@0 intra-line comment indent off by 1, reported unless sloppy-cmt */
32 /*X */ /*@2 missing spc or '*' after comment start reported unless sloppy-spc */
33 /* X*/ /*@ missing space before comment end , reported unless sloppy-spc */
34 /*@ comment starting delimiter: /* inside intra-line comment */
36 *@ above multi-line comment start indent off by 1, reported unless sloppy-cmt; this comment line is too long
37 *@ multi-line comment indent further off by 1 relative to comment start
38 *@ multi-line comment ending with text on last line */
39 /*@2 multi-line comment starting with text on first line
40 *@ comment starting delimiter: /* inside multi-line comment
41 *@ multi-line comment indent off by -1
42 *X*@ no spc after leading '*' in multi-line comment, reported unless sloppy-spc
43 *@0 more than two spaces after . in comment, no more reported
44 *@0 more than two spaces after ? in comment, no more reported
45 *@0 more than two spaces after ! in comment, no more reported
46 */ /*@ multi-line comment end indent off by -1 (relative to comment start) */
47 */ /*@ unexpected comment ending delimiter outside comment */
48 /*- '-' for formatted comment not allowed in intra-line comment */
49 /*@ comment line is 4 columns tooooooooooooooooo wide, reported unless sloppy-len */
50 /*@ comment line is 5 columns toooooooooooooooooooooooooooooooooooooooooooooo wide */
51 #if ~0 /*@ '#if' with constant condition */
52 #endif /*@ indent of preproc. directive off by 1 (must be 0) */
53 #define X (1 + 1) /*@0 extra space in body, reported unless sloppy-spc */
54 #define Y 1 /*@ extra space before body, reported unless sloppy-spc */ \
55 #define Z /*@2 preprocessor directive within multi-line directive */
56 typedef struct { /*@0 extra space in code, reported unless sloppy-spc */
57 enum { /*@1 extra space in intra-line comment, no more reported */
58 w = 0 /*@ hanging expr indent off by 1, or 3 for lines after '{' */
59 && 1, /*@ hanging expr indent off by 3, or -1 for leading '&&' */
60 x = 1, /*@ hanging expr indent off by -1 */
61 y,z /*@ no space after ',', reported unless sloppy-spc */
62 } e_member ; /*@ space before ';', reported unless sloppy-spc */
63 int v[1; /*@ unclosed bracket in type declaration */
64 union { /*@ statement/type declaration indent off by -1 */
65 struct{} s; /*@ no space before '{', reported unless sloppy-spc */
66 }u_member; /*@ no space after '}', reported unless sloppy-spc */
67 } s_type; /*@ statement/type declaration indent off by 4 */
68 int* somefunc(); /*@ no space before '*' in type decl, r unless sloppy-spc */
69 void main(int n) { /*@ opening brace at end of function definition header */
70 for (; ; ) ; /*@ space before ')', reported unless sloppy-spc */
71 for ( ; x; y) ; /*@2 space after '(' and before ';', unless sloppy-spc */
72 for (;;n++) { /*@ missing space after ';', reported unless sloppy-spc */
73 return; /*@0 (1-line) single statement in braces */
74 }} /*@2 code after '}' outside expr */
75 } /*@ unexpected closing brace (too many '}') outside expr */
76 ) /*@ unexpected closing paren outside expr */
77 #endif /*@ unexpected #endif */
78 int f (int a, /*@ space after fn before '(', reported unless sloppy-spc */
79 int b, /*@ hanging expr indent off by -1 */
80 long I) /*@ single-letter name 'I' */
81 { int x; /*@ code after '{' opening a block */
82 int xx = 1) + /*@ unexpected closing parenthesis */
83 0L < /*@ constant on LHS of comparison operator */
84 a] - /*@ unexpected closing bracket */
85 3: * /*@ unexpected ':' (without preceding '?') within expr */
86 4}; /*@ unexpected closing brace within expression */
87 char y[] = { /*@0 unclosed brace within initializer/enum expression */
88 1* 1, /*@ no space etc. before '*', reported unless sloppy-spc */
89 2, /*@ hanging expr indent (for lines after '{') off by 1 */
90 (xx /*@0 unclosed parenthesis in expression */
91 ? y /*@0 unclosed '? (conditional expression) */
92 [0; /*@4 unclosed bracket in expression */
93 /*@ blank line within local decls */
94 s_type s; /*@2 local variable declaration indent off by -1 */
95 t_type t; /*@ local variable declaration indent again off by -1 */
96 /* */ /*@0 missing blank line after local decls */
97 somefunc(a, /*@2 statement indent off by -1 */
98 "aligned" /*@ expr indent off by -2 accepted if sloppy-hang */ "right"
99 , b, /*@ expr indent off by -1 */
100 b, /*@ expr indent as on line above, accepted if sloppy-hang */
101 b, /*@ expr indent off -8 but @ extra indent accepted if sloppy-hang */
102 "again aligned" /*@ expr indent off by -9 (left of stmt indent, */ "right",
103 abc == /*@ .. so reported also with sloppy-hang; this line is too long */ 456
104 # define MAC(A) (A) /*@ nesting indent of preprocessor directive off by 1 */
105 ? 1 /*@ hanging expr indent off by 1 */
106 : 2); /*@ hanging expr indent off by 2, or 1 for leading ':' */
107 if(a /*@ missing space after 'if', reported unless sloppy-spc */
108 /*@0 intra-line comment indent off by -1 (not: by 3 due to '&&') */
109 && ! 0 /*@2 space after '!', reported unless sloppy-spc */
110 || b == /*@ hanging expr indent off by 2, or -2 for leading '||' */
111 (x<<= 1) + /*@ missing space before '<<=' reported unless sloppy-spc */
112 (xx+= 2) + /*@ missing space before '+=', reported unless sloppy-spc */
113 (a^ 1) + /*@ missing space before '^', reported unless sloppy-spc */
114 (y *=z) + /*@ missing space after '*=' reported unless sloppy-spc */
115 a %2 / /*@ missing space after '%', reported unless sloppy-spc */
116 1 +/* */ /*@ no space before comment, reported unless sloppy-spc */
117 /* */+ /*@ no space after comment, reported unless sloppy-spc */
118 s. e_member) /*@ space after '.', reported unless sloppy-spc */
119 xx = a + b /*@ extra single-statement indent off by 1 */
120 + 0; /*@ two times extra single-statement indent off by 3 */
121 if (a ++) /*@ space before postfix '++', reported unless sloppy-spc */
122 { /*@ {' not on same line as preceding 'if' */
123 c; /*@0 single stmt in braces, reported on 1-stmt */
124 } else /*@ missing '{' on same line after '} else' */
125 { /*@ statement indent off by 2 */
126 d; /*@0 single stmt in braces, reported on 1-stmt */
127 } /*@ statement indent off by 6 */
128 if (1) f(a, /*@ (non-brace) code after end of 'if' condition */
129 b); else /*@ (non-brace) code before 'else' */
130 do f(c, c); /*@ (non-brace) code after 'do' */
131 while ( 2); /*@ space after '(', reported unless sloppy-spc */
132 b; c; /*@ more than one statement per line */
133 outer: /*@ outer label special indent off by 1 */
134 do{ /*@ missing space before '{', reported unless sloppy-spc */
135 inner: /*@ inner label normal indent off by 1 */
136 f (3, /*@ space after fn before '(', reported unless sloppy-spc */
137 4); /*@0 false negative: should report single stmt in braces */
138 } /*@0 'while' not on same line as preceding '}' */
139 while (a+ 0); /*@2 missing space before '+', reported unless sloppy-spc */
140 switch (b ) { /*@ space before ')', reported unless sloppy-spc */
141 case 1: /*@ 'case' special statement indent off by -1 */
142 case(2): /*@ missing space after 'case', reported unless sloppy-spc */
143 default: ; /*@ code after 'default:' */
144 } /*@ statement indent off by -4 */
145 return( /*@ missing space after 'return', reported unless sloppy-spc */
146 x); } /*@ code before block-level '}' */
147 /* Here the tool should stop complaining apart from the below issues at EOF */
351 ; /*@ 2 essentially blank lines before, if !sloppy-spc */
352 } /*@ function body length > 200 lines */
353 #if X /*@0 unclosed #if */
354 struct t { /*@0 unclosed brace at decl/block level */
355 enum { /*@0 unclosed brace at enum/expression level */
356 v = (1 /*@0 unclosed parenthesis */
357 etyp /*@0 blank line follows just before EOF, if !sloppy-spc: */