boolean.cocci 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Check violations for rule 14.4
  2. // https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_14_04.c
  3. //
  4. // Confidence: Moderate
  5. // Copyright: (C) 2021 Intel Corporation
  6. //
  7. // SPDX-License-Identifier: Apache-2.0
  8. virtual report
  9. @initialize:python@
  10. @@
  11. @rule1_base@
  12. identifier function, v;
  13. type T1, T2;
  14. parameter list[n] P1;
  15. parameter list[n1] P2;
  16. @@
  17. (
  18. T1 function(P1, T2 v, P2) {...}
  19. |
  20. T1 function(P1, T2 *v, P2) {...}
  21. )
  22. @ script:python @
  23. t << rule1_base.T2;
  24. v << rule1_base.v;
  25. @@
  26. if t == "bool":
  27. cocci.include_match(False)
  28. @rule1@
  29. identifier rule1_base.v;
  30. position p;
  31. @@
  32. (
  33. while (v@p) {...}
  34. |
  35. if (v@p) {...}
  36. )
  37. @ script:python @
  38. p << rule1.p;
  39. @@
  40. msg = "WARNING: Violation to rule 14.4 (Controlling expression shall have essentially Boolean type)"
  41. coccilib.report.print_report(p[0], msg)
  42. @rule2_base@
  43. identifier v;
  44. type T;
  45. @@
  46. T v;
  47. ...
  48. @ script:python @
  49. t << rule2_base.T;
  50. v << rule2_base.v;
  51. @@
  52. if t == "bool":
  53. cocci.include_match(False)
  54. @rule2@
  55. position p;
  56. identifier rule2_base.v;
  57. @@
  58. while (v@p) {...}
  59. @ script:python @
  60. p << rule2.p;
  61. @@
  62. msg = "WARNING: Violation to rule 14.4 (Controlling expression shall have essentially Boolean type)"
  63. coccilib.report.print_report(p[0], msg)
  64. @rule3@
  65. position p;
  66. constant c;
  67. @@
  68. (
  69. while (c@p) {...}
  70. |
  71. while (!c@p) {...}
  72. |
  73. if (c@p) {...}
  74. |
  75. if (!c@p) {...}
  76. )
  77. @ script:python @
  78. p << rule3.p;
  79. @@
  80. msg = "WARNING: Violation to rule 14.4 (Controlling expression shall have essentially Boolean type)"
  81. coccilib.report.print_report(p[0], msg)