1b0d17251Schristos /* 2*4170684fSchristos * Copyright 2007-2023 The OpenSSL Project Authors. All Rights Reserved. 3b0d17251Schristos * Copyright Siemens AG 2015-2022 4b0d17251Schristos * 5b0d17251Schristos * Licensed under the Apache License 2.0 (the "License"). You may not use 6b0d17251Schristos * this file except in compliance with the License. You can obtain a copy 7b0d17251Schristos * in the file LICENSE in the source distribution or at 8b0d17251Schristos * https://www.openssl.org/source/license.html 9b0d17251Schristos */ 10b0d17251Schristos 11b0d17251Schristos /* 12b0d17251Schristos * This demonstrates/tests cases where check-format.pl should report issues. 13b0d17251Schristos * Some of the reports are due to sanity checks for proper nesting of comment 14b0d17251Schristos * delimiters and parenthesis-like symbols, e.g., on unexpected/unclosed braces. 15b0d17251Schristos */ 16b0d17251Schristos 17b0d17251Schristos /* 18b0d17251Schristos * The '@'s after '*' are used for self-tests: they mark lines containing 19b0d17251Schristos * a single flaw that should be reported. Normally it should be reported 20b0d17251Schristos * while handling the given line, but in case of delayed checks there is a 21b0d17251Schristos * following digit indicating the number of reports expected for this line. 22b0d17251Schristos */ 23b0d17251Schristos 24b0d17251Schristos /* For each of the following set of lines the tool should complain once */ 25b0d17251Schristos /*@ tab character: */ 26b0d17251Schristos /*@ intra-line carriage return character: 27b0d17251Schristos */ 28b0d17251Schristos /*@ non-printable ASCII character: */ 29b0d17251Schristos /*@ non-ASCII character: ä */ 30b0d17251Schristos /*@ whitespace at EOL: */ 31b0d17251Schristos // /*@ end-of-line comment style not allowed (for C90 compatibility) */ 32*4170684fSchristos /*@0 intra-line comment indent off by 1, reported unless sloppy-cmt */ 33*4170684fSchristos /*X */ /*@2 missing spc or '*' after comment start reported unless sloppy-spc */ 34b0d17251Schristos /* X*/ /*@ missing space before comment end , reported unless sloppy-spc */ 35b0d17251Schristos /*@ comment starting delimiter: /* inside intra-line comment */ 36b0d17251Schristos /*@0 37b0d17251Schristos *@ above multi-line comment start indent off by 1, reported unless sloppy-cmt; this comment line is too long 38b0d17251Schristos *@ multi-line comment indent further off by 1 relative to comment start 39b0d17251Schristos *@ multi-line comment ending with text on last line */ 40b0d17251Schristos /*@2 multi-line comment starting with text on first line 41b0d17251Schristos *@ comment starting delimiter: /* inside multi-line comment 42b0d17251Schristos *@ multi-line comment indent off by -1 43*4170684fSchristos *X*@ no spc after leading '*' in multi-line comment, reported unless sloppy-spc 44*4170684fSchristos *@0 more than two spaces after . in comment, no more reported 45*4170684fSchristos *@0 more than two spaces after ? in comment, no more reported 46b0d17251Schristos *@0 more than two spaces after ! in comment, no more reported 47b0d17251Schristos */ /*@ multi-line comment end indent off by -1 (relative to comment start) */ 48*4170684fSchristos */ /*@ unexpected comment ending delimiter outside comment */ 49b0d17251Schristos /*- '-' for formatted comment not allowed in intra-line comment */ 50b0d17251Schristos /*@ comment line is 4 columns tooooooooooooooooo wide, reported unless sloppy-len */ 51*4170684fSchristos /*@ comment line is 5 columns toooooooooooooooooooooooooooooooooooooooooooooo wide */ 52*4170684fSchristos #if ~0 /*@ '#if' with constant condition */ 53*4170684fSchristos #endif /*@ indent of preproc. directive off by 1 (must be 0) */ 54*4170684fSchristos #define X (1 + 1) /*@0 extra space in body, reported unless sloppy-spc */ 55*4170684fSchristos #define Y 1 /*@ extra space before body, reported unless sloppy-spc */ \ 56b0d17251Schristos #define Z /*@2 preprocessor directive within multi-line directive */ 57*4170684fSchristos typedef struct { /*@0 extra space in code, reported unless sloppy-spc */ 58b0d17251Schristos enum { /*@1 extra space in intra-line comment, no more reported */ 59b0d17251Schristos w = 0 /*@ hanging expr indent off by 1, or 3 for lines after '{' */ 60b0d17251Schristos && 1, /*@ hanging expr indent off by 3, or -1 for leading '&&' */ 61b0d17251Schristos x = 1, /*@ hanging expr indent off by -1 */ 62b0d17251Schristos y,z /*@ no space after ',', reported unless sloppy-spc */ 63b0d17251Schristos } e_member ; /*@ space before ';', reported unless sloppy-spc */ 64b0d17251Schristos int v[1; /*@ unclosed bracket in type declaration */ 65b0d17251Schristos union { /*@ statement/type declaration indent off by -1 */ 66b0d17251Schristos struct{} s; /*@ no space before '{', reported unless sloppy-spc */ 67b0d17251Schristos }u_member; /*@ no space after '}', reported unless sloppy-spc */ 68b0d17251Schristos } s_type; /*@ statement/type declaration indent off by 4 */ 69b0d17251Schristos int* somefunc(); /*@ no space before '*' in type decl, r unless sloppy-spc */ 70*4170684fSchristos void main(int n) { /*@ opening brace at end of function definition header */ 71*4170684fSchristos for (; ; ) ; /*@ space before ')', reported unless sloppy-spc */ 72*4170684fSchristos for ( ; x; y) ; /*@2 space after '(' and before ';', unless sloppy-spc */ 73b0d17251Schristos for (;;n++) { /*@ missing space after ';', reported unless sloppy-spc */ 74b0d17251Schristos return; /*@0 (1-line) single statement in braces */ 75b0d17251Schristos }} /*@2 code after '}' outside expr */ 76b0d17251Schristos } /*@ unexpected closing brace (too many '}') outside expr */ 77b0d17251Schristos ) /*@ unexpected closing paren outside expr */ 78b0d17251Schristos #endif /*@ unexpected #endif */ 79b0d17251Schristos int f (int a, /*@ space after fn before '(', reported unless sloppy-spc */ 80b0d17251Schristos int b, /*@ hanging expr indent off by -1 */ 81b0d17251Schristos long I) /*@ single-letter name 'I' */ 82b0d17251Schristos { int x; /*@ code after '{' opening a block */ 83b0d17251Schristos int xx = 1) + /*@ unexpected closing parenthesis */ 84b0d17251Schristos 0L < /*@ constant on LHS of comparison operator */ 85b0d17251Schristos a] - /*@ unexpected closing bracket */ 86b0d17251Schristos 3: * /*@ unexpected ':' (without preceding '?') within expr */ 87b0d17251Schristos 4}; /*@ unexpected closing brace within expression */ 88b0d17251Schristos char y[] = { /*@0 unclosed brace within initializer/enum expression */ 89b0d17251Schristos 1* 1, /*@ no space etc. before '*', reported unless sloppy-spc */ 90b0d17251Schristos 2, /*@ hanging expr indent (for lines after '{') off by 1 */ 91b0d17251Schristos (xx /*@0 unclosed parenthesis in expression */ 92b0d17251Schristos ? y /*@0 unclosed '? (conditional expression) */ 93b0d17251Schristos [0; /*@4 unclosed bracket in expression */ 94b0d17251Schristos /*@ blank line within local decls */ 95b0d17251Schristos s_type s; /*@2 local variable declaration indent off by -1 */ 96b0d17251Schristos t_type t; /*@ local variable declaration indent again off by -1 */ 97b0d17251Schristos /* */ /*@0 missing blank line after local decls */ 98b0d17251Schristos somefunc(a, /*@2 statement indent off by -1 */ 99b0d17251Schristos "aligned" /*@ expr indent off by -2 accepted if sloppy-hang */ "right" 100b0d17251Schristos , b, /*@ expr indent off by -1 */ 101b0d17251Schristos b, /*@ expr indent as on line above, accepted if sloppy-hang */ 102b0d17251Schristos b, /*@ expr indent off -8 but @ extra indent accepted if sloppy-hang */ 103b0d17251Schristos "again aligned" /*@ expr indent off by -9 (left of stmt indent, */ "right", 104b0d17251Schristos abc == /*@ .. so reported also with sloppy-hang; this line is too long */ 456 105b0d17251Schristos # define MAC(A) (A) /*@ nesting indent of preprocessor directive off by 1 */ 106b0d17251Schristos ? 1 /*@ hanging expr indent off by 1 */ 107*4170684fSchristos : 2); /*@ hanging expr indent off by 2, or 1 for leading ':' */ 108b0d17251Schristos if(a /*@ missing space after 'if', reported unless sloppy-spc */ 109b0d17251Schristos /*@0 intra-line comment indent off by -1 (not: by 3 due to '&&') */ 110b0d17251Schristos && ! 0 /*@2 space after '!', reported unless sloppy-spc */ 111*4170684fSchristos || b == /*@ hanging expr indent off by 2, or -2 for leading '||' */ 112*4170684fSchristos (x<<= 1) + /*@ missing space before '<<=' reported unless sloppy-spc */ 113*4170684fSchristos (xx+= 2) + /*@ missing space before '+=', reported unless sloppy-spc */ 114*4170684fSchristos (a^ 1) + /*@ missing space before '^', reported unless sloppy-spc */ 115*4170684fSchristos (y *=z) + /*@ missing space after '*=' reported unless sloppy-spc */ 116b0d17251Schristos a %2 / /*@ missing space after '%', reported unless sloppy-spc */ 117b0d17251Schristos 1 +/* */ /*@ no space before comment, reported unless sloppy-spc */ 118b0d17251Schristos /* */+ /*@ no space after comment, reported unless sloppy-spc */ 119b0d17251Schristos s. e_member) /*@ space after '.', reported unless sloppy-spc */ 120b0d17251Schristos xx = a + b /*@ extra single-statement indent off by 1 */ 121b0d17251Schristos + 0; /*@ two times extra single-statement indent off by 3 */ 122b0d17251Schristos if (a ++) /*@ space before postfix '++', reported unless sloppy-spc */ 123b0d17251Schristos { /*@ {' not on same line as preceding 'if' */ 124*4170684fSchristos c; /*@0 single stmt in braces, reported on 1-stmt */ 125b0d17251Schristos } else /*@ missing '{' on same line after '} else' */ 126b0d17251Schristos { /*@ statement indent off by 2 */ 127b0d17251Schristos d; /*@0 single stmt in braces, reported on 1-stmt */ 128b0d17251Schristos } /*@ statement indent off by 6 */ 129b0d17251Schristos if (1) f(a, /*@ (non-brace) code after end of 'if' condition */ 130b0d17251Schristos b); else /*@ (non-brace) code before 'else' */ 131b0d17251Schristos do f(c, c); /*@ (non-brace) code after 'do' */ 132b0d17251Schristos while ( 2); /*@ space after '(', reported unless sloppy-spc */ 133b0d17251Schristos b; c; /*@ more than one statement per line */ 134*4170684fSchristos outer: /*@ outer label special indent off by 1 */ 135b0d17251Schristos do{ /*@ missing space before '{', reported unless sloppy-spc */ 136b0d17251Schristos inner: /*@ inner label normal indent off by 1 */ 137b0d17251Schristos f (3, /*@ space after fn before '(', reported unless sloppy-spc */ 138b0d17251Schristos 4); /*@0 false negative: should report single stmt in braces */ 139*4170684fSchristos } /*@0 'while' not on same line as preceding '}' */ 140b0d17251Schristos while (a+ 0); /*@2 missing space before '+', reported unless sloppy-spc */ 141b0d17251Schristos switch (b ) { /*@ space before ')', reported unless sloppy-spc */ 142*4170684fSchristos case 1: /*@ 'case' special statement indent off by -1 */ 143b0d17251Schristos case(2): /*@ missing space after 'case', reported unless sloppy-spc */ 144b0d17251Schristos default: ; /*@ code after 'default:' */ 145*4170684fSchristos } /*@ statement indent off by -4 */ 146b0d17251Schristos return( /*@ missing space after 'return', reported unless sloppy-spc */ 147b0d17251Schristos x); } /*@ code before block-level '}' */ 148b0d17251Schristos /* Here the tool should stop complaining apart from the below issues at EOF */ 149b0d17251Schristos 150b0d17251Schristos void f_looong_body() 151b0d17251Schristos { 152b0d17251Schristos ; 153b0d17251Schristos ; 154b0d17251Schristos ; 155b0d17251Schristos ; 156b0d17251Schristos ; 157b0d17251Schristos ; 158b0d17251Schristos ; 159b0d17251Schristos ; 160b0d17251Schristos ; 161b0d17251Schristos ; 162b0d17251Schristos ; 163b0d17251Schristos ; 164b0d17251Schristos ; 165b0d17251Schristos ; 166b0d17251Schristos ; 167b0d17251Schristos ; 168b0d17251Schristos ; 169b0d17251Schristos ; 170b0d17251Schristos ; 171b0d17251Schristos ; 172b0d17251Schristos ; 173b0d17251Schristos ; 174b0d17251Schristos ; 175b0d17251Schristos ; 176b0d17251Schristos ; 177b0d17251Schristos ; 178b0d17251Schristos ; 179b0d17251Schristos ; 180b0d17251Schristos ; 181b0d17251Schristos ; 182b0d17251Schristos ; 183b0d17251Schristos ; 184b0d17251Schristos ; 185b0d17251Schristos ; 186b0d17251Schristos ; 187b0d17251Schristos ; 188b0d17251Schristos ; 189b0d17251Schristos ; 190b0d17251Schristos ; 191b0d17251Schristos ; 192b0d17251Schristos ; 193b0d17251Schristos ; 194b0d17251Schristos ; 195b0d17251Schristos ; 196b0d17251Schristos ; 197b0d17251Schristos ; 198b0d17251Schristos ; 199b0d17251Schristos ; 200b0d17251Schristos ; 201b0d17251Schristos ; 202b0d17251Schristos ; 203b0d17251Schristos ; 204b0d17251Schristos ; 205b0d17251Schristos ; 206b0d17251Schristos ; 207b0d17251Schristos ; 208b0d17251Schristos ; 209b0d17251Schristos ; 210b0d17251Schristos ; 211b0d17251Schristos ; 212b0d17251Schristos ; 213b0d17251Schristos ; 214b0d17251Schristos ; 215b0d17251Schristos ; 216b0d17251Schristos ; 217b0d17251Schristos ; 218b0d17251Schristos ; 219b0d17251Schristos ; 220b0d17251Schristos ; 221b0d17251Schristos ; 222b0d17251Schristos ; 223b0d17251Schristos ; 224b0d17251Schristos ; 225b0d17251Schristos ; 226b0d17251Schristos ; 227b0d17251Schristos ; 228b0d17251Schristos ; 229b0d17251Schristos ; 230b0d17251Schristos ; 231b0d17251Schristos ; 232b0d17251Schristos ; 233b0d17251Schristos ; 234b0d17251Schristos ; 235b0d17251Schristos ; 236b0d17251Schristos ; 237b0d17251Schristos ; 238b0d17251Schristos ; 239b0d17251Schristos ; 240b0d17251Schristos ; 241b0d17251Schristos ; 242b0d17251Schristos ; 243b0d17251Schristos ; 244b0d17251Schristos ; 245b0d17251Schristos ; 246b0d17251Schristos ; 247b0d17251Schristos ; 248b0d17251Schristos ; 249b0d17251Schristos ; 250b0d17251Schristos ; 251b0d17251Schristos ; 252b0d17251Schristos ; 253b0d17251Schristos ; 254b0d17251Schristos ; 255b0d17251Schristos ; 256b0d17251Schristos ; 257b0d17251Schristos ; 258b0d17251Schristos ; 259b0d17251Schristos ; 260b0d17251Schristos ; 261b0d17251Schristos ; 262b0d17251Schristos ; 263b0d17251Schristos ; 264b0d17251Schristos ; 265b0d17251Schristos ; 266b0d17251Schristos ; 267b0d17251Schristos ; 268b0d17251Schristos ; 269b0d17251Schristos ; 270b0d17251Schristos ; 271b0d17251Schristos ; 272b0d17251Schristos ; 273b0d17251Schristos ; 274b0d17251Schristos ; 275b0d17251Schristos ; 276b0d17251Schristos ; 277b0d17251Schristos ; 278b0d17251Schristos ; 279b0d17251Schristos ; 280b0d17251Schristos ; 281b0d17251Schristos ; 282b0d17251Schristos ; 283b0d17251Schristos ; 284b0d17251Schristos ; 285b0d17251Schristos ; 286b0d17251Schristos ; 287b0d17251Schristos ; 288b0d17251Schristos ; 289b0d17251Schristos ; 290b0d17251Schristos ; 291b0d17251Schristos ; 292b0d17251Schristos ; 293b0d17251Schristos ; 294b0d17251Schristos ; 295b0d17251Schristos ; 296b0d17251Schristos ; 297b0d17251Schristos ; 298b0d17251Schristos ; 299b0d17251Schristos ; 300b0d17251Schristos ; 301b0d17251Schristos ; 302b0d17251Schristos ; 303b0d17251Schristos ; 304b0d17251Schristos ; 305b0d17251Schristos ; 306b0d17251Schristos ; 307b0d17251Schristos ; 308b0d17251Schristos ; 309b0d17251Schristos ; 310b0d17251Schristos ; 311b0d17251Schristos ; 312b0d17251Schristos ; 313b0d17251Schristos ; 314b0d17251Schristos ; 315b0d17251Schristos ; 316b0d17251Schristos ; 317b0d17251Schristos ; 318b0d17251Schristos ; 319b0d17251Schristos ; 320b0d17251Schristos ; 321b0d17251Schristos ; 322b0d17251Schristos ; 323b0d17251Schristos ; 324b0d17251Schristos ; 325b0d17251Schristos ; 326b0d17251Schristos ; 327b0d17251Schristos ; 328b0d17251Schristos ; 329b0d17251Schristos ; 330b0d17251Schristos ; 331b0d17251Schristos ; 332b0d17251Schristos ; 333b0d17251Schristos ; 334b0d17251Schristos ; 335b0d17251Schristos ; 336b0d17251Schristos ; 337b0d17251Schristos ; 338b0d17251Schristos ; 339b0d17251Schristos ; 340b0d17251Schristos ; 341b0d17251Schristos ; 342b0d17251Schristos ; 343b0d17251Schristos ; 344b0d17251Schristos ; 345b0d17251Schristos ; 346b0d17251Schristos ; 347b0d17251Schristos ; 348b0d17251Schristos ; 349b0d17251Schristos ; 350b0d17251Schristos 351b0d17251Schristos 352b0d17251Schristos ; /*@ 2 essentially blank lines before, if !sloppy-spc */ 353*4170684fSchristos } /*@ function body length > 200 lines */ 354b0d17251Schristos #if X /*@0 unclosed #if */ 355b0d17251Schristos struct t { /*@0 unclosed brace at decl/block level */ 356b0d17251Schristos enum { /*@0 unclosed brace at enum/expression level */ 357b0d17251Schristos v = (1 /*@0 unclosed parenthesis */ 358b0d17251Schristos etyp /*@0 blank line follows just before EOF, if !sloppy-spc: */ 359 360