check-format-test-positives.c 8.7 KB

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