check-format-test-positives.c 9.8 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. outer: /*@ outer label special indent off by 1 */
  118. do{ /*@ no space before '{', reported unless sloppy-spc */
  119. inner: /*@ inner label normal indent off by 1 */
  120. f (3, /*@ space after fn before '(', reported unless sloppy-spc */
  121. 4); /*@0 false negative: should report single stmt in braces */
  122. } /*@0 'while' not on same line as preceding '}' */
  123. while (a+ 0); /*@2 no space before '+', reported unless sloppy-spc */
  124. switch (b ) { /*@ space before ')', reported unless sloppy-spc */
  125. case 1: /*@ 'case' special statement indent off by -1 */
  126. case(2): /*@ no space after 'case', reported unless sloppy-spc */
  127. default: ; /*@ code after 'default:' */
  128. } /*@ statement indent off by -4 */
  129. return( /*@ no space after 'return', reported unless sloppy-spc */
  130. x); } /*@ code before block-level '}' */
  131. /* Here the tool should stop complaining apart from the below issues at EOF */
  132. void f_looong_body()
  133. {
  134. ;
  135. ;
  136. ;
  137. ;
  138. ;
  139. ;
  140. ;
  141. ;
  142. ;
  143. ;
  144. ;
  145. ;
  146. ;
  147. ;
  148. ;
  149. ;
  150. ;
  151. ;
  152. ;
  153. ;
  154. ;
  155. ;
  156. ;
  157. ;
  158. ;
  159. ;
  160. ;
  161. ;
  162. ;
  163. ;
  164. ;
  165. ;
  166. ;
  167. ;
  168. ;
  169. ;
  170. ;
  171. ;
  172. ;
  173. ;
  174. ;
  175. ;
  176. ;
  177. ;
  178. ;
  179. ;
  180. ;
  181. ;
  182. ;
  183. ;
  184. ;
  185. ;
  186. ;
  187. ;
  188. ;
  189. ;
  190. ;
  191. ;
  192. ;
  193. ;
  194. ;
  195. ;
  196. ;
  197. ;
  198. ;
  199. ;
  200. ;
  201. ;
  202. ;
  203. ;
  204. ;
  205. ;
  206. ;
  207. ;
  208. ;
  209. ;
  210. ;
  211. ;
  212. ;
  213. ;
  214. ;
  215. ;
  216. ;
  217. ;
  218. ;
  219. ;
  220. ;
  221. ;
  222. ;
  223. ;
  224. ;
  225. ;
  226. ;
  227. ;
  228. ;
  229. ;
  230. ;
  231. ;
  232. ;
  233. ;
  234. ;
  235. ;
  236. ;
  237. ;
  238. ;
  239. ;
  240. ;
  241. ;
  242. ;
  243. ;
  244. ;
  245. ;
  246. ;
  247. ;
  248. ;
  249. ;
  250. ;
  251. ;
  252. ;
  253. ;
  254. ;
  255. ;
  256. ;
  257. ;
  258. ;
  259. ;
  260. ;
  261. ;
  262. ;
  263. ;
  264. ;
  265. ;
  266. ;
  267. ;
  268. ;
  269. ;
  270. ;
  271. ;
  272. ;
  273. ;
  274. ;
  275. ;
  276. ;
  277. ;
  278. ;
  279. ;
  280. ;
  281. ;
  282. ;
  283. ;
  284. ;
  285. ;
  286. ;
  287. ;
  288. ;
  289. ;
  290. ;
  291. ;
  292. ;
  293. ;
  294. ;
  295. ;
  296. ;
  297. ;
  298. ;
  299. ;
  300. ;
  301. ;
  302. ;
  303. ;
  304. ;
  305. ;
  306. ;
  307. ;
  308. ;
  309. ;
  310. ;
  311. ;
  312. ;
  313. ;
  314. ;
  315. ;
  316. ;
  317. ;
  318. ;
  319. ;
  320. ;
  321. ;
  322. ;
  323. ;
  324. ;
  325. ;
  326. ;
  327. ;
  328. ;
  329. ;
  330. ;
  331. ;
  332. ; /*@ 2 essentially empty lines before, if !sloppy-spc */
  333. } /*@ function body length > 200 lines */
  334. #if 0 /*@0 unclosed #if */
  335. struct t { /*@0 unclosed brace at decl/block level */
  336. enum { /*@0 unclosed brace at enum/expression level */
  337. v = (1 /*@0 unclosed parenthesis */
  338. etyp /*@0 empty line follows just before EOF, if !sloppy-spc: */