2
0

check-format-test-positives.c 11 KB

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