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