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