check-format-test-positives.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. * Copyright 2007-2021 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. *@0 more than two spaces after ? in comment, reported unless sloppy-spc
  43. *@0 more than two spaces after ! in comment, reported unless sloppy-spc
  44. */ /*@ multi-line comment end indent off by -1 (relative to comment start) */
  45. */ /*@ unexpected comment ending delimiter outside comment */
  46. /*@ comment line is 4 columns tooooooooooooooooo wide, reported unless sloppy-len */
  47. /*@ comment line is 5 columns toooooooooooooooooooooooooooooooooooooooooooooo wide */
  48. #define X 1 /*@0 extra space false negative due to coincidence */
  49. #define Y 2 /*@ indent of preprocessor directive off by 1 (must be 0) */
  50. typedef struct { /*@0 extra space in code, reported unless sloppy-spc */
  51. enum { /*@1 extra space in comment, reported unless sloppy-spc */
  52. w = 0 /*@ hanging expr indent off by 1, or 3 for lines after '{' */
  53. && 1, /*@ hanging expr indent off by 3, or -1 for leading '&&' */
  54. x = 1, /*@ hanging expr indent off by -1 */
  55. y,z /*@ no space after ',', reported unless sloppy-spc */
  56. } e_member ; /*@ space before ';', reported unless sloppy-spc */
  57. int v[1; /*@ unclosed bracket in type declaration */
  58. union { /*@ statement/type declaration indent off by -1 */
  59. struct{} s; /*@ no space before '{', reported unless sloppy-spc */
  60. }u_member; /*@ no space after '}', reported unless sloppy-spc */
  61. } s_type; /*@ statement/type declaration indent off by 4 */
  62. int* somefunc(); /*@ no space before '*' in type decl, r unless sloppy-spc */
  63. void main(int n) { /*@ opening brace at end of function definition header */
  64. for (;;n++) { /*@ no space after ';', reported unless sloppy-spc */
  65. return; /*@0 (1-line) single statement in braces */
  66. }} /*@2 code after '}' outside expr */
  67. } /*@ unexpected closing brace (too many '}') outside expr */
  68. ) /*@ unexpected closing paren outside expr */
  69. #endif /*@ unexpected #endif */
  70. int f (int a, /*@ space after fn before '(', reported unless sloppy-spc */
  71. int b, /*@ hanging expr indent off by -1 */
  72. long I) /*@ single-letter name 'I' */
  73. { int /*@ code after '{' opening a block */
  74. xx = 1) + /*@ unexpected closing parenthesis */
  75. 0L < /*@ constant on LHS of comparison operator */
  76. a] - /*@ unexpected closing bracket */
  77. 3: * /*@ unexpected ':' (without preceding '?') within expr */
  78. 4}; /*@ unexpected closing brace within expression */
  79. char y[] = { /*@0 unclosed brace within initializer/enum expression */
  80. 1* 1, /*@ no space etc. before '*', reported unless sloppy-spc */
  81. 2, /*@ hanging expr indent (for lines after '{') off by 1 */
  82. (xx /*@0 unclosed parenthesis in expression */
  83. ? y /*@0 unclosed '? (conditional expression) */
  84. [0; /*@4 unclosed bracket in expression */
  85. s_type s; /*@ local variable declaration indent off by -1 */
  86. somefunc(a, /*@ statement indent off by -1 */
  87. "aligned" /*@ expr indent off by -2 accepted if sloppy-hang */ "right"
  88. , b, /*@ expr indent off by -1 */
  89. b, /*@ expr indent as on line above, accepted if sloppy-hang */
  90. b, /*@ expr indent off -8 but @ extra indent accepted if sloppy-hang */
  91. "again aligned" /*@ expr indent off by -9 (left of stmt indent, */ "right",
  92. abc == /*@ .. so reported also with sloppy-hang; this line is too long */ 456
  93. # define MAC(A) (A) /*@ nesting indent of preprocessor directive off by 1 */
  94. ? 1 /*@ hanging expr indent off by 1 */
  95. : 2); /*@ hanging expr indent off by 2, or 1 for leading ':' */
  96. if(a /*@ no space after 'if', reported unless sloppy-spc */
  97. /*@0 intra-line comment indent off by -1 (not: by 3 due to '&&') */
  98. && ! 0 /*@2 space after '!', reported unless sloppy-spc */
  99. || b == /*@ hanging expr indent off by 2, or -2 for leading '||' */
  100. (xx+= 2) + /*@ no space before '+=', reported unless sloppy-spc */
  101. (a^ 1) + /*@ no space before '^', reported unless sloppy-spc */
  102. a %2 / /*@ no space after '%', reported unless sloppy-spc */
  103. 1 +/* */ /*@ no space before comment, reported unless sloppy-spc */
  104. /* */+ /*@ no space after comment, reported unless sloppy-spc */
  105. s. e_member) /*@ space after '.', reported unless sloppy-spc */
  106. xx = a + b /*@ extra single-statement indent off by 1 */
  107. + 0; /*@ two times extra single-statement indent off by 3 */
  108. if (a ++) /*@ space before postfix '++', reported unless sloppy-spc */
  109. { /*@ {' not on same line as preceding 'if' */
  110. c; /*@0 single stmt in braces, reported on 1-stmt */
  111. } else /*@ no '{' on same line after '} else' */
  112. { /*@ statement indent off by 2 */
  113. d; /*@0 single stmt in braces, reported on 1-stmt */
  114. } /*@ statement indent off by 6 */
  115. if (1) f(a, /*@ (non-brace) code after end of 'if' condition */
  116. b); else /*@ (non-brace) code before 'else' */
  117. do f(c, c); /*@ (non-brace) code after 'do' */
  118. while ( 2); /*@ space after '(', reported unless sloppy-spc */
  119. b; c; /*@ more than one statement per line */
  120. outer: /*@ outer label special indent off by 1 */
  121. do{ /*@ no space before '{', reported unless sloppy-spc */
  122. inner: /*@ inner label normal indent off by 1 */
  123. f (3, /*@ space after fn before '(', reported unless sloppy-spc */
  124. 4); /*@0 false negative: should report single stmt in braces */
  125. } /*@0 'while' not on same line as preceding '}' */
  126. while (a+ 0); /*@2 no space before '+', reported unless sloppy-spc */
  127. switch (b ) { /*@ space before ')', reported unless sloppy-spc */
  128. case 1: /*@ 'case' special statement indent off by -1 */
  129. case(2): /*@ no space after 'case', reported unless sloppy-spc */
  130. default: ; /*@ code after 'default:' */
  131. } /*@ statement indent off by -4 */
  132. return( /*@ no space after 'return', reported unless sloppy-spc */
  133. x); } /*@ code before block-level '}' */
  134. /* Here the tool should stop complaining apart from the below issues at EOF */
  135. void f_looong_body()
  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. ;
  333. ;
  334. ;
  335. ; /*@ 2 essentially empty lines before, if !sloppy-spc */
  336. } /*@ function body length > 200 lines */
  337. #if 0 /*@0 unclosed #if */
  338. struct t { /*@0 unclosed brace at decl/block level */
  339. enum { /*@0 unclosed brace at enum/expression level */
  340. v = (1 /*@0 unclosed parenthesis */
  341. etyp /*@0 empty line follows just before EOF, if !sloppy-spc: */