@specs-feup/clava-misra 1.0.2 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (322) hide show
  1. package/.gitignore +8 -0
  2. package/README.md +53 -19
  3. package/dist/MISRA.d.ts +28 -10
  4. package/dist/MISRA.d.ts.map +1 -1
  5. package/dist/MISRA.js +30 -10
  6. package/dist/MISRA.js.map +1 -1
  7. package/dist/MISRAContext.d.ts +65 -11
  8. package/dist/MISRAContext.d.ts.map +1 -1
  9. package/dist/MISRAContext.js +131 -35
  10. package/dist/MISRAContext.js.map +1 -1
  11. package/dist/MISRARule.d.ts +38 -25
  12. package/dist/MISRARule.d.ts.map +1 -1
  13. package/dist/MISRARule.js +40 -18
  14. package/dist/MISRARule.js.map +1 -1
  15. package/dist/MISRATool.d.ts +46 -5
  16. package/dist/MISRATool.d.ts.map +1 -1
  17. package/dist/MISRATool.js +118 -45
  18. package/dist/MISRATool.js.map +1 -1
  19. package/dist/StandardGuideline.d.ts +22 -0
  20. package/dist/StandardGuideline.d.ts.map +1 -0
  21. package/dist/StandardGuideline.js +12 -0
  22. package/dist/StandardGuideline.js.map +1 -0
  23. package/dist/ast-visitor/Context.d.ts +11 -0
  24. package/dist/ast-visitor/Context.d.ts.map +1 -0
  25. package/dist/ast-visitor/Context.js +15 -0
  26. package/dist/ast-visitor/Context.js.map +1 -0
  27. package/dist/ast-visitor/Visit.d.ts +23 -0
  28. package/dist/ast-visitor/Visit.d.ts.map +1 -0
  29. package/dist/ast-visitor/Visit.js +18 -0
  30. package/dist/ast-visitor/Visit.js.map +1 -0
  31. package/dist/ast-visitor/VisitWithContext.d.ts +32 -0
  32. package/dist/ast-visitor/VisitWithContext.d.ts.map +1 -0
  33. package/dist/ast-visitor/VisitWithContext.js +26 -0
  34. package/dist/ast-visitor/VisitWithContext.js.map +1 -0
  35. package/dist/main.js +1 -2
  36. package/dist/main.js.map +1 -1
  37. package/dist/rules/Section13_SideEffects/Rule_13_6_SafeSizeOfOperand.d.ts +27 -0
  38. package/dist/rules/Section13_SideEffects/Rule_13_6_SafeSizeOfOperand.d.ts.map +1 -0
  39. package/dist/rules/Section13_SideEffects/Rule_13_6_SafeSizeOfOperand.js +98 -0
  40. package/dist/rules/Section13_SideEffects/Rule_13_6_SafeSizeOfOperand.js.map +1 -0
  41. package/dist/rules/Section16_SwitchStatements/Rule_16_2_TopLevelSwitch.d.ts +45 -6
  42. package/dist/rules/Section16_SwitchStatements/Rule_16_2_TopLevelSwitch.d.ts.map +1 -1
  43. package/dist/rules/Section16_SwitchStatements/Rule_16_2_TopLevelSwitch.js +91 -8
  44. package/dist/rules/Section16_SwitchStatements/Rule_16_2_TopLevelSwitch.js.map +1 -1
  45. package/dist/rules/Section16_SwitchStatements/Rule_16_3_UnconditionalBreak.d.ts +15 -5
  46. package/dist/rules/Section16_SwitchStatements/Rule_16_3_UnconditionalBreak.d.ts.map +1 -1
  47. package/dist/rules/Section16_SwitchStatements/Rule_16_3_UnconditionalBreak.js +20 -6
  48. package/dist/rules/Section16_SwitchStatements/Rule_16_3_UnconditionalBreak.js.map +1 -1
  49. package/dist/rules/Section16_SwitchStatements/Rule_16_4_SwitchHasDefault.d.ts +11 -5
  50. package/dist/rules/Section16_SwitchStatements/Rule_16_4_SwitchHasDefault.d.ts.map +1 -1
  51. package/dist/rules/Section16_SwitchStatements/Rule_16_4_SwitchHasDefault.js +13 -10
  52. package/dist/rules/Section16_SwitchStatements/Rule_16_4_SwitchHasDefault.js.map +1 -1
  53. package/dist/rules/Section16_SwitchStatements/Rule_16_5_DefaultFirstOrLast.d.ts +11 -6
  54. package/dist/rules/Section16_SwitchStatements/Rule_16_5_DefaultFirstOrLast.d.ts.map +1 -1
  55. package/dist/rules/Section16_SwitchStatements/Rule_16_5_DefaultFirstOrLast.js +19 -11
  56. package/dist/rules/Section16_SwitchStatements/Rule_16_5_DefaultFirstOrLast.js.map +1 -1
  57. package/dist/rules/Section16_SwitchStatements/Rule_16_6_SwitchMinTwoClauses.d.ts +15 -5
  58. package/dist/rules/Section16_SwitchStatements/Rule_16_6_SwitchMinTwoClauses.d.ts.map +1 -1
  59. package/dist/rules/Section16_SwitchStatements/Rule_16_6_SwitchMinTwoClauses.js +24 -10
  60. package/dist/rules/Section16_SwitchStatements/Rule_16_6_SwitchMinTwoClauses.js.map +1 -1
  61. package/dist/rules/Section16_SwitchStatements/Rule_16_7_NonBooleanSwitchCondition.d.ts +22 -6
  62. package/dist/rules/Section16_SwitchStatements/Rule_16_7_NonBooleanSwitchCondition.d.ts.map +1 -1
  63. package/dist/rules/Section16_SwitchStatements/Rule_16_7_NonBooleanSwitchCondition.js +37 -10
  64. package/dist/rules/Section16_SwitchStatements/Rule_16_7_NonBooleanSwitchCondition.js.map +1 -1
  65. package/dist/rules/Section17_Functions/Rule_17_3_ImplicitFunction.d.ts +66 -0
  66. package/dist/rules/Section17_Functions/Rule_17_3_ImplicitFunction.d.ts.map +1 -0
  67. package/dist/rules/Section17_Functions/Rule_17_3_ImplicitFunction.js +209 -0
  68. package/dist/rules/Section17_Functions/Rule_17_3_ImplicitFunction.js.map +1 -0
  69. package/dist/rules/Section17_Functions/Rule_17_4_NonVoidReturn.d.ts +37 -12
  70. package/dist/rules/Section17_Functions/Rule_17_4_NonVoidReturn.d.ts.map +1 -1
  71. package/dist/rules/Section17_Functions/Rule_17_4_NonVoidReturn.js +112 -39
  72. package/dist/rules/Section17_Functions/Rule_17_4_NonVoidReturn.js.map +1 -1
  73. package/dist/rules/Section17_Functions/Rule_17_6_StaticArraySizeParam.d.ts +15 -5
  74. package/dist/rules/Section17_Functions/Rule_17_6_StaticArraySizeParam.d.ts.map +1 -1
  75. package/dist/rules/Section17_Functions/Rule_17_6_StaticArraySizeParam.js +21 -7
  76. package/dist/rules/Section17_Functions/Rule_17_6_StaticArraySizeParam.js.map +1 -1
  77. package/dist/rules/Section17_Functions/Rule_17_7_UnusedReturnValue.d.ts +11 -6
  78. package/dist/rules/Section17_Functions/Rule_17_7_UnusedReturnValue.d.ts.map +1 -1
  79. package/dist/rules/Section17_Functions/Rule_17_7_UnusedReturnValue.js +17 -8
  80. package/dist/rules/Section17_Functions/Rule_17_7_UnusedReturnValue.js.map +1 -1
  81. package/dist/rules/Section21-StandardLibraries/DisallowedStdLibFunctionRule.d.ts +105 -0
  82. package/dist/rules/Section21-StandardLibraries/DisallowedStdLibFunctionRule.d.ts.map +1 -0
  83. package/dist/rules/Section21-StandardLibraries/DisallowedStdLibFunctionRule.js +258 -0
  84. package/dist/rules/Section21-StandardLibraries/DisallowedStdLibFunctionRule.js.map +1 -0
  85. package/dist/rules/Section21-StandardLibraries/Rule_21_10_NoTimeDateFunctions.d.ts +25 -0
  86. package/dist/rules/Section21-StandardLibraries/Rule_21_10_NoTimeDateFunctions.d.ts.map +1 -0
  87. package/dist/rules/Section21-StandardLibraries/Rule_21_10_NoTimeDateFunctions.js +27 -0
  88. package/dist/rules/Section21-StandardLibraries/Rule_21_10_NoTimeDateFunctions.js.map +1 -0
  89. package/dist/rules/Section21-StandardLibraries/Rule_21_11_NoTgmathFunctions.d.ts +29 -0
  90. package/dist/rules/Section21-StandardLibraries/Rule_21_11_NoTgmathFunctions.d.ts.map +1 -0
  91. package/dist/rules/Section21-StandardLibraries/Rule_21_11_NoTgmathFunctions.js +31 -0
  92. package/dist/rules/Section21-StandardLibraries/Rule_21_11_NoTgmathFunctions.js.map +1 -0
  93. package/dist/rules/Section21-StandardLibraries/Rule_21_3_NoDynamicMemory.d.ts +25 -0
  94. package/dist/rules/Section21-StandardLibraries/Rule_21_3_NoDynamicMemory.d.ts.map +1 -0
  95. package/dist/rules/Section21-StandardLibraries/Rule_21_3_NoDynamicMemory.js +27 -0
  96. package/dist/rules/Section21-StandardLibraries/Rule_21_3_NoDynamicMemory.js.map +1 -0
  97. package/dist/rules/Section21-StandardLibraries/Rule_21_6_NoStdIOFunctions.d.ts +25 -0
  98. package/dist/rules/Section21-StandardLibraries/Rule_21_6_NoStdIOFunctions.d.ts.map +1 -0
  99. package/dist/rules/Section21-StandardLibraries/Rule_21_6_NoStdIOFunctions.js +27 -0
  100. package/dist/rules/Section21-StandardLibraries/Rule_21_6_NoStdIOFunctions.js.map +1 -0
  101. package/dist/rules/Section21-StandardLibraries/Rule_21_7_NoNumericStringConversions.d.ts +25 -0
  102. package/dist/rules/Section21-StandardLibraries/Rule_21_7_NoNumericStringConversions.d.ts.map +1 -0
  103. package/dist/rules/Section21-StandardLibraries/Rule_21_7_NoNumericStringConversions.js +27 -0
  104. package/dist/rules/Section21-StandardLibraries/Rule_21_7_NoNumericStringConversions.js.map +1 -0
  105. package/dist/rules/Section21-StandardLibraries/Rule_21_8_NoProcessControlFunctions.d.ts +30 -0
  106. package/dist/rules/Section21-StandardLibraries/Rule_21_8_NoProcessControlFunctions.d.ts.map +1 -0
  107. package/dist/rules/Section21-StandardLibraries/Rule_21_8_NoProcessControlFunctions.js +32 -0
  108. package/dist/rules/Section21-StandardLibraries/Rule_21_8_NoProcessControlFunctions.js.map +1 -0
  109. package/dist/rules/Section21-StandardLibraries/Rule_21_9_NoGenericSearchOrSort.d.ts +25 -0
  110. package/dist/rules/Section21-StandardLibraries/Rule_21_9_NoGenericSearchOrSort.d.ts.map +1 -0
  111. package/dist/rules/Section21-StandardLibraries/Rule_21_9_NoGenericSearchOrSort.js +27 -0
  112. package/dist/rules/Section21-StandardLibraries/Rule_21_9_NoGenericSearchOrSort.js.map +1 -0
  113. package/dist/rules/Section2_UnusedCode/Rule_2_3_UnusedTypeDecl.d.ts +6 -14
  114. package/dist/rules/Section2_UnusedCode/Rule_2_3_UnusedTypeDecl.d.ts.map +1 -1
  115. package/dist/rules/Section2_UnusedCode/Rule_2_3_UnusedTypeDecl.js +17 -27
  116. package/dist/rules/Section2_UnusedCode/Rule_2_3_UnusedTypeDecl.js.map +1 -1
  117. package/dist/rules/Section2_UnusedCode/Rule_2_4_UnusedTagDecl.d.ts +13 -7
  118. package/dist/rules/Section2_UnusedCode/Rule_2_4_UnusedTagDecl.d.ts.map +1 -1
  119. package/dist/rules/Section2_UnusedCode/Rule_2_4_UnusedTagDecl.js +31 -17
  120. package/dist/rules/Section2_UnusedCode/Rule_2_4_UnusedTagDecl.js.map +1 -1
  121. package/dist/rules/Section2_UnusedCode/Rule_2_6_UnusedLabels.d.ts +26 -7
  122. package/dist/rules/Section2_UnusedCode/Rule_2_6_UnusedLabels.d.ts.map +1 -1
  123. package/dist/rules/Section2_UnusedCode/Rule_2_6_UnusedLabels.js +32 -14
  124. package/dist/rules/Section2_UnusedCode/Rule_2_6_UnusedLabels.js.map +1 -1
  125. package/dist/rules/Section2_UnusedCode/Rule_2_7_UnusedParameters.d.ts +48 -6
  126. package/dist/rules/Section2_UnusedCode/Rule_2_7_UnusedParameters.d.ts.map +1 -1
  127. package/dist/rules/Section2_UnusedCode/Rule_2_7_UnusedParameters.js +84 -34
  128. package/dist/rules/Section2_UnusedCode/Rule_2_7_UnusedParameters.js.map +1 -1
  129. package/dist/rules/Section3_Comments/Rule_3_1_CommentSequences.d.ts +24 -5
  130. package/dist/rules/Section3_Comments/Rule_3_1_CommentSequences.d.ts.map +1 -1
  131. package/dist/rules/Section3_Comments/Rule_3_1_CommentSequences.js +27 -7
  132. package/dist/rules/Section3_Comments/Rule_3_1_CommentSequences.js.map +1 -1
  133. package/dist/rules/Section5_Identifiers/IdentifierRenameRule.d.ts +46 -0
  134. package/dist/rules/Section5_Identifiers/IdentifierRenameRule.d.ts.map +1 -0
  135. package/dist/rules/Section5_Identifiers/IdentifierRenameRule.js +40 -0
  136. package/dist/rules/Section5_Identifiers/IdentifierRenameRule.js.map +1 -0
  137. package/dist/rules/Section5_Identifiers/Rule_5_1_DistinctExternalIdentifiers.d.ts +25 -0
  138. package/dist/rules/Section5_Identifiers/Rule_5_1_DistinctExternalIdentifiers.d.ts.map +1 -0
  139. package/dist/rules/Section5_Identifiers/Rule_5_1_DistinctExternalIdentifiers.js +46 -0
  140. package/dist/rules/Section5_Identifiers/Rule_5_1_DistinctExternalIdentifiers.js.map +1 -0
  141. package/dist/rules/Section5_Identifiers/Rule_5_6_UniqueTypedefNames.d.ts +26 -0
  142. package/dist/rules/Section5_Identifiers/Rule_5_6_UniqueTypedefNames.d.ts.map +1 -0
  143. package/dist/rules/Section5_Identifiers/Rule_5_6_UniqueTypedefNames.js +54 -0
  144. package/dist/rules/Section5_Identifiers/Rule_5_6_UniqueTypedefNames.js.map +1 -0
  145. package/dist/rules/Section5_Identifiers/Rule_5_7_UniqueTagNames.d.ts +27 -0
  146. package/dist/rules/Section5_Identifiers/Rule_5_7_UniqueTagNames.d.ts.map +1 -0
  147. package/dist/rules/Section5_Identifiers/Rule_5_7_UniqueTagNames.js +55 -0
  148. package/dist/rules/Section5_Identifiers/Rule_5_7_UniqueTagNames.js.map +1 -0
  149. package/dist/rules/Section5_Identifiers/Rule_5_8_UniqueExternalLinkIdentifiers.d.ts +24 -0
  150. package/dist/rules/Section5_Identifiers/Rule_5_8_UniqueExternalLinkIdentifiers.d.ts.map +1 -0
  151. package/dist/rules/Section5_Identifiers/Rule_5_8_UniqueExternalLinkIdentifiers.js +42 -0
  152. package/dist/rules/Section5_Identifiers/Rule_5_8_UniqueExternalLinkIdentifiers.js.map +1 -0
  153. package/dist/rules/Section5_Identifiers/Rule_5_9_UniqueInternalLinkIdentifiers.d.ts +24 -0
  154. package/dist/rules/Section5_Identifiers/Rule_5_9_UniqueInternalLinkIdentifiers.d.ts.map +1 -0
  155. package/dist/rules/Section5_Identifiers/Rule_5_9_UniqueInternalLinkIdentifiers.js +42 -0
  156. package/dist/rules/Section5_Identifiers/Rule_5_9_UniqueInternalLinkIdentifiers.js.map +1 -0
  157. package/dist/rules/Section8_DeclarationsAndDefinitions/Rule_8_6_SingleExternalDefinition.d.ts +35 -0
  158. package/dist/rules/Section8_DeclarationsAndDefinitions/Rule_8_6_SingleExternalDefinition.d.ts.map +1 -0
  159. package/dist/rules/Section8_DeclarationsAndDefinitions/Rule_8_6_SingleExternalDefinition.js +98 -0
  160. package/dist/rules/Section8_DeclarationsAndDefinitions/Rule_8_6_SingleExternalDefinition.js.map +1 -0
  161. package/dist/rules/Section8_DeclarationsAndDefinitions/Rule_8_7_RestrictExternalLinkage.d.ts +35 -0
  162. package/dist/rules/Section8_DeclarationsAndDefinitions/Rule_8_7_RestrictExternalLinkage.d.ts.map +1 -0
  163. package/dist/rules/Section8_DeclarationsAndDefinitions/Rule_8_7_RestrictExternalLinkage.js +76 -0
  164. package/dist/rules/Section8_DeclarationsAndDefinitions/Rule_8_7_RestrictExternalLinkage.js.map +1 -0
  165. package/dist/rules/Section8_DeclarationsAndDefinitions/Rule_8_9_BlockScopeDefinition.d.ts +32 -0
  166. package/dist/rules/Section8_DeclarationsAndDefinitions/Rule_8_9_BlockScopeDefinition.d.ts.map +1 -0
  167. package/dist/rules/Section8_DeclarationsAndDefinitions/Rule_8_9_BlockScopeDefinition.js +57 -0
  168. package/dist/rules/Section8_DeclarationsAndDefinitions/Rule_8_9_BlockScopeDefinition.js.map +1 -0
  169. package/dist/rules/UserConfigurableRule.d.ts +55 -0
  170. package/dist/rules/UserConfigurableRule.d.ts.map +1 -0
  171. package/dist/rules/UserConfigurableRule.js +16 -0
  172. package/dist/rules/UserConfigurableRule.js.map +1 -0
  173. package/dist/rules/index.d.ts +11 -18
  174. package/dist/rules/index.d.ts.map +1 -1
  175. package/dist/rules/index.js +47 -7
  176. package/dist/rules/index.js.map +1 -1
  177. package/dist/tests/Section17_Functions/misra_config.json +10 -0
  178. package/dist/tests/Section21-StandardLibraries/misra_config.json +88 -0
  179. package/dist/tests/Section21-StandardLibraries/problematic_misra_config.json +19 -0
  180. package/dist/tests/utils.d.ts +6 -3
  181. package/dist/tests/utils.d.ts.map +1 -1
  182. package/dist/tests/utils.js +33 -7
  183. package/dist/tests/utils.js.map +1 -1
  184. package/dist/utils/CallUtils.d.ts +15 -0
  185. package/dist/utils/CallUtils.d.ts.map +1 -0
  186. package/dist/utils/CallUtils.js +34 -0
  187. package/dist/utils/CallUtils.js.map +1 -0
  188. package/dist/utils/CommentUtils.d.ts +21 -0
  189. package/dist/utils/CommentUtils.d.ts.map +1 -0
  190. package/dist/utils/CommentUtils.js +27 -0
  191. package/dist/utils/CommentUtils.js.map +1 -0
  192. package/dist/utils/FileUtils.d.ts +67 -0
  193. package/dist/utils/FileUtils.d.ts.map +1 -0
  194. package/dist/utils/FileUtils.js +144 -0
  195. package/dist/utils/FileUtils.js.map +1 -0
  196. package/dist/utils/FunctionUtils.d.ts +47 -0
  197. package/dist/utils/FunctionUtils.d.ts.map +1 -0
  198. package/dist/utils/FunctionUtils.js +87 -0
  199. package/dist/utils/FunctionUtils.js.map +1 -0
  200. package/dist/utils/IdentifierUtils.d.ts +63 -0
  201. package/dist/utils/IdentifierUtils.d.ts.map +1 -0
  202. package/dist/utils/IdentifierUtils.js +133 -0
  203. package/dist/utils/IdentifierUtils.js.map +1 -0
  204. package/dist/utils/JoinpointUtils.d.ts +36 -0
  205. package/dist/utils/JoinpointUtils.d.ts.map +1 -0
  206. package/dist/utils/JoinpointUtils.js +63 -0
  207. package/dist/utils/JoinpointUtils.js.map +1 -0
  208. package/dist/utils/ProgramUtils.d.ts +39 -0
  209. package/dist/utils/ProgramUtils.d.ts.map +1 -0
  210. package/dist/utils/ProgramUtils.js +97 -0
  211. package/dist/utils/ProgramUtils.js.map +1 -0
  212. package/dist/utils/SwitchUtils.d.ts +21 -0
  213. package/dist/utils/SwitchUtils.d.ts.map +1 -0
  214. package/dist/utils/SwitchUtils.js +47 -0
  215. package/dist/utils/SwitchUtils.js.map +1 -0
  216. package/dist/utils/TypeDeclUtils.d.ts +35 -0
  217. package/dist/utils/TypeDeclUtils.d.ts.map +1 -0
  218. package/dist/utils/TypeDeclUtils.js +78 -0
  219. package/dist/utils/TypeDeclUtils.js.map +1 -0
  220. package/dist/utils/VarUtils.d.ts +51 -0
  221. package/dist/utils/VarUtils.d.ts.map +1 -0
  222. package/dist/utils/VarUtils.js +91 -0
  223. package/dist/utils/VarUtils.js.map +1 -0
  224. package/package.json +16 -7
  225. package/src/MISRA.ts +33 -17
  226. package/src/MISRAContext.ts +128 -30
  227. package/src/MISRARule.ts +61 -29
  228. package/src/MISRATool.ts +126 -42
  229. package/src/StandardGuideline.ts +23 -0
  230. package/src/ast-visitor/Context.ts +16 -0
  231. package/src/ast-visitor/Visit.ts +26 -0
  232. package/src/ast-visitor/VisitWithContext.ts +42 -0
  233. package/src/main.ts +1 -4
  234. package/src/rules/Section13_SideEffects/Rule_13_6_SafeSizeOfOperand.ts +114 -0
  235. package/src/rules/Section16_SwitchStatements/Rule_16_2_TopLevelSwitch.ts +103 -13
  236. package/src/rules/Section16_SwitchStatements/Rule_16_3_UnconditionalBreak.ts +24 -10
  237. package/src/rules/Section16_SwitchStatements/Rule_16_4_SwitchHasDefault.ts +17 -8
  238. package/src/rules/Section16_SwitchStatements/Rule_16_5_DefaultFirstOrLast.ts +20 -12
  239. package/src/rules/Section16_SwitchStatements/Rule_16_6_SwitchMinTwoClauses.ts +23 -12
  240. package/src/rules/Section16_SwitchStatements/Rule_16_7_NonBooleanSwitchCondition.ts +44 -13
  241. package/src/rules/Section17_Functions/Rule_17_3_ImplicitFunction.ts +153 -104
  242. package/src/rules/Section17_Functions/Rule_17_4_NonVoidReturn.ts +114 -44
  243. package/src/rules/Section17_Functions/Rule_17_6_StaticArraySizeParam.ts +27 -10
  244. package/src/rules/Section17_Functions/Rule_17_7_UnusedReturnValue.ts +20 -12
  245. package/src/rules/Section21-StandardLibraries/DisallowedStdLibFunctionRule.ts +317 -0
  246. package/src/rules/Section21-StandardLibraries/Rule_21_10_NoTimeDateFunctions.ts +30 -0
  247. package/src/rules/Section21-StandardLibraries/Rule_21_11_NoTgmathFunctions.ts +35 -0
  248. package/src/rules/Section21-StandardLibraries/Rule_21_3_NoDynamicMemory.ts +30 -0
  249. package/src/rules/Section21-StandardLibraries/Rule_21_6_NoStdIOFunctions.ts +30 -0
  250. package/src/rules/Section21-StandardLibraries/Rule_21_7_NoNumericStringConversions.ts +29 -0
  251. package/src/rules/Section21-StandardLibraries/Rule_21_8_NoProcessControlFunctions.ts +36 -0
  252. package/src/rules/Section21-StandardLibraries/Rule_21_9_NoGenericSearchOrSort.ts +30 -0
  253. package/src/rules/Section2_UnusedCode/Rule_2_3_UnusedTypeDecl.ts +14 -29
  254. package/src/rules/Section2_UnusedCode/Rule_2_4_UnusedTagDecl.ts +33 -19
  255. package/src/rules/Section2_UnusedCode/Rule_2_6_UnusedLabels.ts +33 -20
  256. package/src/rules/Section2_UnusedCode/Rule_2_7_UnusedParameters.ts +90 -45
  257. package/src/rules/Section3_Comments/Rule_3_1_CommentSequences.ts +27 -8
  258. package/src/rules/Section5_Identifiers/IdentifierRenameRule.ts +63 -0
  259. package/src/rules/Section5_Identifiers/Rule_5_1_DistinctExternalIdentifiers.ts +52 -0
  260. package/src/rules/Section5_Identifiers/Rule_5_6_UniqueTypedefNames.ts +62 -0
  261. package/src/rules/Section5_Identifiers/Rule_5_7_UniqueTagNames.ts +61 -0
  262. package/src/rules/Section5_Identifiers/Rule_5_8_UniqueExternalLinkIdentifiers.ts +47 -0
  263. package/src/rules/Section5_Identifiers/Rule_5_9_UniqueInternalLinkIdentifiers.ts +47 -0
  264. package/src/rules/Section8_DeclarationsAndDefinitions/Rule_8_6_SingleExternalDefinition.ts +118 -0
  265. package/src/rules/Section8_DeclarationsAndDefinitions/Rule_8_7_RestrictExternalLinkage.ts +89 -0
  266. package/src/rules/Section8_DeclarationsAndDefinitions/Rule_8_9_BlockScopeDefinition.ts +65 -0
  267. package/src/rules/UserConfigurableRule.ts +60 -0
  268. package/src/rules/index.ts +45 -7
  269. package/src/tests/Section13_SideEffects/Rule_13_6_SafeSizeOfOperand.test.ts +114 -0
  270. package/src/tests/Section16_SwitchStatements/Rule_16_2_TopLevelSwitch.test.ts +7 -5
  271. package/src/tests/Section16_SwitchStatements/Rule_16_3_UnconditionalBreak.test.ts +40 -13
  272. package/src/tests/Section16_SwitchStatements/Rule_16_4_SwitchHasDefault.test.ts +10 -12
  273. package/src/tests/Section16_SwitchStatements/Rule_16_5_DefaultFirstOrLast.test.ts +5 -5
  274. package/src/tests/Section16_SwitchStatements/Rule_16_6_SwitchMinTwoClauses.test.ts +5 -5
  275. package/src/tests/Section16_SwitchStatements/Rule_16_7_NonBooleanSwitchCondition.test.ts +5 -5
  276. package/src/tests/Section17_Functions/Rule_17_3_ImplicitFunctions.test.ts +68 -40
  277. package/src/tests/Section17_Functions/Rule_17_3_ImplicitFunctions_MissingConfig.test.ts +98 -0
  278. package/src/tests/Section17_Functions/Rule_17_3_ImplicitFunctions_ProblematicConfig.test.ts +71 -0
  279. package/src/tests/Section17_Functions/Rule_17_4_NonVoidReturn.test.ts +103 -49
  280. package/src/tests/Section17_Functions/Rule_17_4_NonVoidReturn_MissingConfig.test.ts +7 -7
  281. package/src/tests/Section17_Functions/Rule_17_6_StaticArraySizeParam.test.ts +36 -7
  282. package/src/tests/Section17_Functions/Rule_17_7_UnusedReturnValue.test.ts +25 -21
  283. package/src/tests/Section17_Functions/misra_config.json +4 -3
  284. package/src/tests/Section21-StandardLibraries/Rule_21_10_NoTimeDateFunctions.test.ts +62 -0
  285. package/src/tests/Section21-StandardLibraries/Rule_21_11_NoTgmathFunctions.test.ts +67 -0
  286. package/src/tests/Section21-StandardLibraries/Rule_21_3_NoDynamicMemory.test.ts +84 -0
  287. package/src/tests/Section21-StandardLibraries/Rule_21_3_NoDynamicMemory_MissingConfig.test.ts +33 -0
  288. package/src/tests/Section21-StandardLibraries/Rule_21_3_NoDynamicMemory_ProblematicConfig.test.ts +71 -0
  289. package/src/tests/Section21-StandardLibraries/Rule_21_6_NoStdIOFunctions.test.ts +63 -0
  290. package/src/tests/Section21-StandardLibraries/Rule_21_7_NoNumericStringConversions.test.ts +91 -0
  291. package/src/tests/Section21-StandardLibraries/Rule_21_8_NoProcessControlFunctions.test.ts +65 -0
  292. package/src/tests/Section21-StandardLibraries/Rule_21_9_NoGenericSearchOrSort.test.ts +89 -0
  293. package/src/tests/Section21-StandardLibraries/misra_config.json +88 -0
  294. package/src/tests/Section21-StandardLibraries/problematic_misra_config.json +19 -0
  295. package/src/tests/Section2_UnusedCode/Rule_2_3_UnusedTypeDecl.test.ts +148 -113
  296. package/src/tests/Section2_UnusedCode/Rule_2_4_UnusedTagDecl.test.ts +243 -163
  297. package/src/tests/Section2_UnusedCode/Rule_2_6_UnusedLabels.test.ts +32 -20
  298. package/src/tests/Section2_UnusedCode/Rule_2_7_UnusedParameters.test.ts +36 -30
  299. package/src/tests/Section3_Comments/Rule_3_1_CommentSequences.test.ts +37 -8
  300. package/src/tests/Section5_Identifiers/Rule_5_1_DistinctExternalIdentifiers.test.ts +78 -0
  301. package/src/tests/Section5_Identifiers/Rule_5_6_UniqueTypedefNames.test.ts +120 -0
  302. package/src/tests/Section5_Identifiers/Rule_5_7_UniqueTagNames.test.ts +51 -0
  303. package/src/tests/Section5_Identifiers/Rule_5_8_UniqueExternalLinkIdentifiers.test.ts +73 -0
  304. package/src/tests/Section5_Identifiers/Rule_5_9_UniqueInternalLinkIdentifiers.test.ts +97 -0
  305. package/src/tests/Section8_DeclarationsAndDefinitions/Rule_8_6_SingleExternalDefinition.test.ts +160 -0
  306. package/src/tests/Section8_DeclarationsAndDefinitions/Rule_8_7_RestrictExternalLinkage.test.ts +46 -0
  307. package/src/tests/Section8_DeclarationsAndDefinitions/Rule_8_9_BlockScopeDefinition.test.ts +69 -0
  308. package/src/tests/utils.ts +32 -7
  309. package/src/utils/CallUtils.ts +37 -0
  310. package/src/utils/CommentUtils.ts +29 -0
  311. package/src/utils/FileUtils.ts +169 -0
  312. package/src/utils/FunctionUtils.ts +97 -0
  313. package/src/utils/IdentifierUtils.ts +142 -0
  314. package/src/utils/JoinpointUtils.ts +70 -0
  315. package/src/utils/ProgramUtils.ts +107 -0
  316. package/src/utils/SwitchUtils.ts +52 -0
  317. package/src/utils/TypeDeclUtils.ts +88 -0
  318. package/src/utils/VarUtils.ts +102 -0
  319. package/tsconfig.json +2 -1
  320. package/src/rules/Section20-PreprocessingDirectives/Rule_20_2_InvalidHeaderFileName.ts +0 -120
  321. package/src/rules/Section3_Comments/Rule_3_2_LineSplicing.ts +0 -36
  322. package/src/utils/utils.ts +0 -280
@@ -1,40 +1,53 @@
1
- import { Joinpoint, RecordJp, EnumDecl, TagType, ElaboratedType } from "@specs-feup/clava/api/Joinpoints.js";
1
+ import { Joinpoint, DeclStmt } from "@specs-feup/clava/api/Joinpoints.js";
2
2
  import MISRARule from "../../MISRARule.js";
3
- import MISRAContext from "../../MISRAContext.js";
4
- import { MISRATransformationReport, MISRATransformationType } from "../../MISRA.js";
5
- import { getTagUses, hasTypeDecl } from "../../utils/utils.js";
3
+ import { AnalysisType, MISRATransformationReport, MISRATransformationType } from "../../MISRA.js";
4
+ import { hasTypeDefDecl, isTypeDeclUsed } from "../../utils/TypeDeclUtils.js";
5
+ import { isTagDecl, TagDecl } from "../../utils/JoinpointUtils.js";
6
6
 
7
7
  /**
8
8
  * MISRA-C Rule 2.4: A project should not contain unused tag declarations.
9
9
  */
10
10
  export default class Rule_2_4_UnusedTagDecl extends MISRARule {
11
- priority = 3;
12
-
13
- constructor(context: MISRAContext) {
14
- super("2.4", context);
11
+ /**
12
+ * Scope of analysis
13
+ */
14
+ readonly analysisType = AnalysisType.SYSTEM;
15
+
16
+ /**
17
+ * @returns Rule identifier according to MISRA-C:2012
18
+ */
19
+ override get name(): string {
20
+ return "2.4";
15
21
  }
16
22
 
17
23
  /**
18
24
  * Checks if the given joinpoint is an unused tag declaration
19
- * A tag is considered to be unused if it has no references in the code or is only used within a typedef
25
+ * A tag is considered unused if it has no references in the code or if it's only used within a typedef
20
26
  *
21
27
  * @param $jp - Joinpoint to analyze
22
28
  * @param logErrors - [logErrors=false] - Whether to log errors if a violation is detected
23
29
  * @returns Returns true if the joinpoint violates the rule, false otherwise
24
30
  */
25
31
  match($jp: Joinpoint, logErrors: boolean = false): boolean {
26
- if (!($jp instanceof RecordJp || $jp instanceof EnumDecl)) return false;
32
+ if (!(isTagDecl($jp) || ($jp instanceof DeclStmt && $jp.decls.length === 1))) {
33
+ return false;
34
+ }
35
+
36
+ if ($jp instanceof DeclStmt && !isTagDecl($jp.decls[0])) {
37
+ return false;
38
+ }
27
39
 
28
- const containsTypeDecl = hasTypeDecl($jp);
29
- const jpName = $jp.name;
30
- if (containsTypeDecl && jpName === undefined || jpName === null || (jpName as string).trim().length === 0) {
40
+ const tagJp: TagDecl = $jp instanceof DeclStmt ? $jp.decls[0] as TagDecl : $jp;
41
+ const containsTypeDecl = hasTypeDefDecl(tagJp);
42
+ const jpName = tagJp.name;
43
+ if (containsTypeDecl && (jpName === undefined || jpName === null || (jpName as string).trim().length === 0)) {
31
44
  return false;
32
45
  }
33
46
 
34
- const isUnused = getTagUses($jp).length === 0;
47
+ const isUnused = !isTypeDeclUsed(tagJp);
35
48
  if (isUnused && logErrors) {
36
- this.logMISRAError($jp,
37
- containsTypeDecl ? `The tag '${$jp.name}' is declared but only used in a typedef.` : `The tag '${$jp.name}' is declared but not used.`);
49
+ this.logMISRAError(tagJp,
50
+ containsTypeDecl ? `The tag '${tagJp.name}' is declared but only used in a typedef.` : `The tag '${tagJp.name}' is declared but not used.`);
38
51
  }
39
52
  return isUnused;
40
53
  }
@@ -47,12 +60,13 @@ export default class Rule_2_4_UnusedTagDecl extends MISRARule {
47
60
  * @param $jp - Joinpoint to transform
48
61
  * @returns Report detailing the transformation result
49
62
  */
50
- transform($jp: Joinpoint): MISRATransformationReport {
63
+ apply($jp: Joinpoint): MISRATransformationReport {
51
64
  if (!this.match($jp))
52
65
  return new MISRATransformationReport(MISRATransformationType.NoChange);
53
66
 
54
- if (hasTypeDecl($jp)) {
55
- ($jp as RecordJp | EnumDecl).setName('');
67
+ const tagJp: TagDecl = $jp instanceof DeclStmt ? $jp.decls[0] as TagDecl : $jp as TagDecl;
68
+ if (hasTypeDefDecl(tagJp)) {
69
+ (tagJp).setName('');
56
70
  return new MISRATransformationReport(MISRATransformationType.DescendantChange);
57
71
  }
58
72
  $jp.detach();
@@ -1,44 +1,57 @@
1
- import { GotoStmt, FunctionJp, Joinpoint, LabelStmt } from "@specs-feup/clava/api/Joinpoints.js";
1
+ import { FunctionJp, Joinpoint } from "@specs-feup/clava/api/Joinpoints.js";
2
2
  import MISRARule from "../../MISRARule.js";
3
- import MISRAContext from "../../MISRAContext.js";
4
- import Query from "@specs-feup/lara/api/weaver/Query.js";
5
- import { MISRATransformationReport, MISRATransformationType } from "../../MISRA.js";
3
+ import { AnalysisType, MISRATransformationReport, MISRATransformationType } from "../../MISRA.js";
4
+ import { getUnusedLabels } from "../../utils/FunctionUtils.js";
6
5
 
7
6
  /**
8
- * Rule 2.6: Unused Labels.
9
- * Checks for labels within a function that are not used.
7
+ * MISRA-C Rule 2.6: A function should not contain unused label declarations.
8
+ *
10
9
  */
11
10
  export default class Rule_2_6_UnusedLabels extends MISRARule {
12
- priority = 3;
13
-
14
- constructor(context: MISRAContext) {
15
- super("2.6", context);
16
- }
11
+ /**
12
+ * Scope of analysis
13
+ */
14
+ readonly analysisType = AnalysisType.SINGLE_TRANSLATION_UNIT;
17
15
 
18
- private getUnusedLabels(func: FunctionJp): LabelStmt[] {
19
- return Query.searchFrom(func, LabelStmt).get().filter(label =>
20
- Query.searchFrom(func, GotoStmt, { label: jp => jp.astId === label.decl.astId }).get().length === 0
21
- );
16
+ /**
17
+ * @returns Rule identifier according to MISRA-C:2012
18
+ */
19
+ override get name(): string {
20
+ return "2.6";
22
21
  }
23
22
 
23
+ /**
24
+ * Checks if the given joinpoint represents a function with unused labels.
25
+ * A tag is considered unused if it is declared but not referenced by any goto statement.
26
+ *
27
+ * @param $jp - Joinpoint to analyze
28
+ * @param logErrors - [logErrors=false] - Whether to log errors if a violation is detected
29
+ * @returns Returns true if the joinpoint violates the rule, false otherwise
30
+ */
24
31
  match($jp: Joinpoint, logErrors: boolean = false): boolean {
25
32
  if (!($jp instanceof FunctionJp))
26
33
  return false;
27
34
 
28
- const unusedLabels = this.getUnusedLabels($jp);
35
+ const unusedLabels = getUnusedLabels($jp);
29
36
  if (logErrors) {
30
37
  unusedLabels.forEach(label =>
31
- this.logMISRAError(label, `Label ${label.decl.name} is unused in function ${$jp.name}.`)
38
+ this.logMISRAError(label, `Label '${label.decl.name}' is unused in function ${$jp.name}.`)
32
39
  )
33
40
  }
34
41
  return unusedLabels.length > 0;
35
42
  }
36
43
 
37
- transform($jp: Joinpoint): MISRATransformationReport {
38
- if(!this.match($jp))
44
+ /**
45
+ * Removes all unused labels if the provided joinpoint represents a function
46
+ *
47
+ * @param $jp - Joinpoint to transform
48
+ * @returns Report detailing the transformation result
49
+ */
50
+ apply($jp: Joinpoint): MISRATransformationReport {
51
+ if (!this.match($jp))
39
52
  return new MISRATransformationReport(MISRATransformationType.NoChange);
40
53
 
41
- const unusedLabels = this.getUnusedLabels($jp as FunctionJp);
54
+ const unusedLabels = getUnusedLabels($jp as FunctionJp);
42
55
  for (const label of unusedLabels) {
43
56
  label.detach();
44
57
  }
@@ -1,67 +1,112 @@
1
- import { FunctionJp, Joinpoint, Param, Varref, Call } from "@specs-feup/clava/api/Joinpoints.js";
1
+ import { FunctionJp, Joinpoint, Param, Varref, Call, Program } from "@specs-feup/clava/api/Joinpoints.js";
2
2
  import MISRARule from "../../MISRARule.js";
3
- import MISRAContext from "../../MISRAContext.js";
4
3
  import Query from "@specs-feup/lara/api/weaver/Query.js";
5
- import { MISRATransformationReport, MISRATransformationType } from "../../MISRA.js";
6
- import { getParamReferences } from "../../utils/utils.js";
7
- import ClavaJoinPoints from "@specs-feup/clava/api/clava/ClavaJoinPoints.js";
4
+ import { AnalysisType, MISRATransformationReport, MISRATransformationType } from "../../MISRA.js";
5
+ import { getDirectParamReferences, getParamReferences, getVLAFieldParamReferences } from "../../utils/FunctionUtils.js";
8
6
 
7
+ /**
8
+ * MISRA-C Rule 2.7: There should be no unused parameters in functions.
9
+ */
9
10
  export default class Rule_2_7_UnusedParameters extends MISRARule {
10
- priority = 3;
11
+ /**
12
+ * Scope of analysis
13
+ */
14
+ readonly analysisType = AnalysisType.SINGLE_TRANSLATION_UNIT;
15
+
16
+ /**
17
+ * @returns Rule identifier according to MISRA-C:2012
18
+ */
19
+ override get name(): string {
20
+ return "2.7";
21
+ }
22
+
23
+ #unusedParams: Param[] = [];
24
+
25
+ /**
26
+ * Checks if the program contains function with unused parameters.
27
+ * A parameter is considered unused if it is never referenced anywhere in the function.
28
+ *
29
+ * @param $jp - Joinpoint to analyze
30
+ * @param logErrors - [logErrors=false] - Whether to log errors if a violation is detected
31
+ * @returns Returns true if the joinpoint violates the rule, false otherwise
32
+ */
33
+ match($jp: Joinpoint, logErrors: boolean = false): boolean {
34
+ if (!($jp instanceof FunctionJp && $jp.isImplementation))
35
+ return false;
36
+
37
+ const nonCompliant = $jp.params.some((param) => getParamReferences(param, $jp).length === 0);
38
+ if (logErrors && nonCompliant) {
39
+ this.#unusedParams = this.getUnusedParams($jp);
40
+ this.#unusedParams.forEach(param => this.logMISRAError(param, `Parameter '${param.name}' is unused in function '${$jp.name}'.`));
41
+ }
42
+ return nonCompliant;
43
+ }
44
+
45
+ /**
46
+ * Removes all unused parameters from functions in the program
47
+ *
48
+ * @param $jp - Joinpoint to transform
49
+ * @returns Report detailing the transformation result
50
+ */
51
+ apply($jp: Joinpoint): MISRATransformationReport {
52
+ if (!this.match($jp))
53
+ return new MISRATransformationReport(MISRATransformationType.NoChange);
54
+
55
+ const funcJp = $jp as FunctionJp;
56
+ const usedParams = this.getUsedParams(funcJp);
57
+ const unusedParamsPosition = this.getUnusedParamsPositions(funcJp);
58
+ const calls = Query.search(Call, {function: jp => jp?.astId === funcJp.astId}).get();
59
+
60
+ funcJp.setParams(usedParams);
11
61
 
12
- constructor(context: MISRAContext) {
13
- super("2.7", context);
62
+ for (const call of calls) {
63
+ const unusedArgs = unusedParamsPosition.map(i => call.args[i]);
64
+ unusedArgs.forEach(arg => arg.detach());
65
+ }
66
+ return new MISRATransformationReport(MISRATransformationType.DescendantChange);
14
67
  }
15
68
 
69
+ /**
70
+ * Gets all unused parameters from a function joinpoint
71
+ *
72
+ * @param func - Function joinpoint to analyze
73
+ * @returns Returns a list of all unused parameters from a function.
74
+ */
16
75
  private getUnusedParams(func: FunctionJp): Param[] {
17
- return func.params.filter(param => getParamReferences(param, func).length === 0);
76
+ return func.params.filter((param) => {
77
+ if (getDirectParamReferences(param, func).length > 0) return false;
78
+ return getVLAFieldParamReferences(param, func).length === 0;
79
+ });
18
80
  }
19
81
 
82
+ /**
83
+ * Gets all used parameters from a function joinpoint
84
+ *
85
+ * @param func - Function joinpoint to analyze
86
+ * @returns Returns a list of parameters that are used within a function.
87
+ */
20
88
  private getUsedParams(func: FunctionJp): Param[] {
21
- return func.params.filter(param => getParamReferences(param, func).length > 0);
89
+ return func.params.filter((param) => {
90
+ if (getDirectParamReferences(param, func).length > 0) return true;
91
+ return getVLAFieldParamReferences(param, func).length > 0;
92
+ });
22
93
  }
23
94
 
24
- private getUsedParamsPositions(func: FunctionJp): number[] {
95
+ /**
96
+ * Returns the positions (indexes) of parameters that are used in the given function joinpoint.
97
+ *
98
+ * @param func - Function joinpoint to analyze
99
+ * @returns returns a list of numbers representing the indexes of used parameters.
100
+ */
101
+ private getUnusedParamsPositions(func: FunctionJp): number[] {
25
102
  let result = [];
26
103
  for (let i = 0; i < func.params.length; i++) {
27
104
  const param = func.params[i];
28
- if (Query.searchFrom(func, Varref, { decl: jp => jp?.astId === param.astId }).get().length > 0) {
105
+ const varRefs = Query.searchFrom(func, Varref, { decl: jp => jp?.astId === param.astId }).get();
106
+ if (varRefs.length === 0) {
29
107
  result.push(i);
30
108
  }
31
109
  }
32
110
  return result;
33
- }
34
-
35
- match($jp: Joinpoint, logErrors: boolean = false): boolean {
36
- if (!($jp instanceof FunctionJp && $jp.isImplementation)) return false;
37
-
38
- const unusedParams = this.getUnusedParams($jp);
39
- if (logErrors) {
40
- unusedParams.forEach(param =>
41
- this.logMISRAError(param, `Parameter '${param.name}' is unused in function ${$jp.name}.`)
42
- )
43
- }
44
- return unusedParams.length > 0;
45
- }
46
-
47
- transform($jp: Joinpoint): MISRATransformationReport {
48
- if(!this.match($jp)) return new MISRATransformationReport(MISRATransformationType.NoChange);
49
-
50
- const functionJp = $jp as FunctionJp;
51
- const usedParams = this.getUsedParams(functionJp);
52
- const usedParamsPositions = this.getUsedParamsPositions(functionJp);
53
- const calls = Query.search(Call, {function: jp => jp.astId === $jp.astId}).get();
54
-
55
- functionJp.setParams(usedParams);
56
- for (const funcDecl of functionJp.declarationJps) {
57
- funcDecl.setParams(usedParams);
58
- }
59
-
60
- for (const call of calls) {
61
- const newArgs = usedParamsPositions.map(i => call.args[i]);
62
- const newCall = functionJp.newCall(newArgs)
63
- call.replaceWith(newCall);
64
- }
65
- return new MISRATransformationReport(MISRATransformationType.DescendantChange);
66
111
  }
67
112
  }
@@ -1,18 +1,31 @@
1
- import { Comment, Joinpoint } from "@specs-feup/clava/api/Joinpoints.js";
1
+ import { Joinpoint } from "@specs-feup/clava/api/Joinpoints.js";
2
2
  import MISRARule from "../../MISRARule.js";
3
- import MISRAContext from "../../MISRAContext.js";
4
- import { isInlineComment, getComments } from "../../utils/utils.js";
5
- import { MISRATransformationReport, MISRATransformationType } from "../../MISRA.js";
3
+ import { isInlineComment, getComments } from "../../utils/CommentUtils.js";
4
+ import { AnalysisType, MISRATransformationReport, MISRATransformationType } from "../../MISRA.js";
6
5
 
7
6
  /**
8
- * MISRA Rule 3.1:
7
+ * MISRA-C Rule 3.1: The character sequences /* an d // shall not be used within a comment.
9
8
  */
10
9
  export default class Rule_3_1_CommentSequences extends MISRARule {
10
+ /**
11
+ * Scope of analysis
12
+ */
13
+ readonly analysisType = AnalysisType.SINGLE_TRANSLATION_UNIT;
11
14
 
12
- constructor(context: MISRAContext) {
13
- super("3.1", context);
15
+ /**
16
+ * @returns Rule identifier according to MISRA-C:2012
17
+ */
18
+ override get name(): string {
19
+ return "3.1";
14
20
  }
15
21
 
22
+ /**
23
+ * Checks if given joinpoint contains disallowed character sequences in comments.
24
+ *
25
+ * @param $jp - Joinpoint to analyze
26
+ * @param logErrors - [logErrors=false] - Whether to log errors if a violation is detected
27
+ * @returns Returns true if the joinpoint violates the rule, false otherwise
28
+ */
16
29
  match($jp: Joinpoint, logErrors: boolean = false): boolean {
17
30
  const invalidComments = getComments($jp).filter(comment =>
18
31
  (isInlineComment(comment) && /(\/\*)/g.test(comment.text)) ||
@@ -26,7 +39,13 @@ export default class Rule_3_1_CommentSequences extends MISRARule {
26
39
  return invalidComments.length > 0;
27
40
  }
28
41
 
29
- transform($jp: Joinpoint): MISRATransformationReport {
42
+ /**
43
+ * Transforms the given joinpoint if it represents a joinpoint containing disallowed character sequences in comments.
44
+ *
45
+ * @param $jp - Joinpoint to transform
46
+ * @returns Report detailing the transformation result
47
+ */
48
+ apply($jp: Joinpoint): MISRATransformationReport {
30
49
  if (!this.match($jp))
31
50
  return new MISRATransformationReport(MISRATransformationType.NoChange);
32
51
 
@@ -0,0 +1,63 @@
1
+ import {Joinpoint, Program} from "@specs-feup/clava/api/Joinpoints.js";
2
+ import MISRARule from "../../MISRARule.js";
3
+ import { AnalysisType, MISRATransformationReport, MISRATransformationType } from "../../MISRA.js";
4
+ import { renameIdentifier } from "../../utils/IdentifierUtils.js";
5
+ import Query from "@specs-feup/lara/api/weaver/Query.js";
6
+
7
+ /**
8
+ * Abstract base class for MISRA-C rules that enforce constraints on identifier uniqueness where renaming may be required.
9
+ *
10
+ * Need to implement:
11
+ * - analysisType
12
+ * - name()
13
+ * - match($jp, logErrors)
14
+ */
15
+ export default abstract class IdentifierRenameRule extends MISRARule {
16
+ /**
17
+ * A positive integer starting from 1 that indicates the rule's priority, determining the order in which rules are applied.
18
+ */
19
+ readonly priority = 2;
20
+
21
+ /**
22
+ * Specifies the scope of analysis: single unit or entire system.
23
+ */
24
+ abstract readonly analysisType: AnalysisType;
25
+
26
+ /**
27
+ * @returns Rule identifier according to MISRA-C:2012
28
+ */
29
+ abstract override get name(): string;
30
+
31
+ /**
32
+ * Identifiers with invalid names that require renaming.
33
+ */
34
+ protected invalidIdentifiers: any[] = [];
35
+
36
+ /**
37
+ * Checks if the joinpoint violates the rule
38
+ *
39
+ * @param $jp - Joinpoint to analyze
40
+ * @param logErrors - [logErrors=false] - Whether to log errors if a violation is detected
41
+ * @returns Returns true if the joinpoint violates the rule, false otherwise
42
+ */
43
+ abstract match($jp: Joinpoint, logErrors: boolean): boolean;
44
+
45
+ /**
46
+ * Renames all invalid identifiers found in the program.
47
+ * After renaming, the program is rebuilt to ensure proper linking of functions, variables, and their external declarations.
48
+ *
49
+ * @param $jp - Joinpoint to transform
50
+ * @returns Report detailing the transformation result
51
+ */
52
+ apply($jp: Joinpoint): MISRATransformationReport {
53
+ if (!this.match($jp, false)) {
54
+ return new MISRATransformationReport(MISRATransformationType.NoChange);
55
+ }
56
+
57
+ for (const identifierJp of this.invalidIdentifiers) {
58
+ const newName = this.context.generateIdentifierName(identifierJp)!;
59
+ renameIdentifier(identifierJp, newName);
60
+ }
61
+ return new MISRATransformationReport(MISRATransformationType.Replacement, Query.root() as Program);
62
+ }
63
+ }
@@ -0,0 +1,52 @@
1
+ import {Joinpoint, Program } from "@specs-feup/clava/api/Joinpoints.js";
2
+ import { areDistinctIdentifiers, getIdentifierName } from "../../utils/IdentifierUtils.js";
3
+ import { getExternalLinkageIdentifiers } from "../../utils/ProgramUtils.js";
4
+ import { compareLocation } from "../../utils/JoinpointUtils.js";
5
+ import { isSameVarDecl } from "../../utils/VarUtils.js";
6
+ import IdentifierRenameRule from "./IdentifierRenameRule.js";
7
+ import { AnalysisType } from "../../MISRA.js";
8
+
9
+ /**
10
+ * MISRA-C Rule 5.1 External identifiers shall be distinct.
11
+ */
12
+ export default class Rule_5_1_DistinctExternalIdentifiers extends IdentifierRenameRule {
13
+ /**
14
+ * Scope of analysis
15
+ */
16
+ readonly analysisType = AnalysisType.SYSTEM;
17
+
18
+ /**
19
+ * @returns Rule identifier according to MISRA-C:2012
20
+ */
21
+ override get name(): string {
22
+ return "5.1";
23
+ }
24
+
25
+ /**
26
+ * Checks if the given joinpoint is an external identifier distinct from other external identifiers.
27
+ *
28
+ * @param $jp - Joinpoint to analyze
29
+ * @param logErrors - [logErrors=false] - Whether to log errors if a violation is detected
30
+ * @returns Returns true if the joinpoint violates the rule, false otherwise
31
+ */
32
+ match($jp: Joinpoint, logErrors: boolean = false): boolean {
33
+ if (!($jp instanceof Program)) return false;
34
+
35
+ const externalIdentifiers = getExternalLinkageIdentifiers();
36
+ this.invalidIdentifiers =
37
+ externalIdentifiers.filter(identifier1 =>
38
+ externalIdentifiers.some(identifier2 =>
39
+ !isSameVarDecl(identifier1, identifier2) &&
40
+ !areDistinctIdentifiers(identifier1, identifier2) &&
41
+ compareLocation(identifier2, identifier1) < 0
42
+ )
43
+ );
44
+ const nonCompliant = this.invalidIdentifiers.length > 0;
45
+ if (nonCompliant && logErrors) {
46
+ this.invalidIdentifiers.forEach(identifierJp => {
47
+ this.logMISRAError(identifierJp, `Identifier '${getIdentifierName(identifierJp)}' is not distinct from other external identifier within the first 31 characters.`)
48
+ });
49
+ }
50
+ return nonCompliant;
51
+ }
52
+ }
@@ -0,0 +1,62 @@
1
+ import { Joinpoint, Program, TypedefDecl } from "@specs-feup/clava/api/Joinpoints.js";
2
+ import { getIdentifierName, isIdentifierDuplicated, isIdentifierNameDeclaredBefore } from "../../utils/IdentifierUtils.js";
3
+ import { getTypeDefDecl } from "../../utils/TypeDeclUtils.js";
4
+ import Query from "@specs-feup/lara/api/weaver/Query.js";
5
+ import { isTagDecl } from "../../utils/JoinpointUtils.js";
6
+ import { getIdentifierDecls } from "../../utils/ProgramUtils.js";
7
+ import IdentifierRenameRule from "./IdentifierRenameRule.js";
8
+ import { AnalysisType } from "../../MISRA.js";
9
+
10
+ /**
11
+ * MISRA-C Rule 5.6: A typedef name shall be a unique identifier.
12
+ *
13
+ * Exception: The typedef name may be the same as the structure, union or enumeration tag name associated with the typedef.
14
+ */
15
+ export default class Rule_5_6_UniqueTypedefNames extends IdentifierRenameRule {
16
+ /**
17
+ * Scope of analysis
18
+ */
19
+ readonly analysisType = AnalysisType.SYSTEM;
20
+
21
+ /**
22
+ * @returns Rule identifier according to MISRA-C:2012
23
+ */
24
+ override get name(): string {
25
+ return "5.6";
26
+ }
27
+
28
+ /**
29
+ *
30
+ * @param $jp - Joinpoint to analyze
31
+ * @param logErrors - [logErrors=false] - Whether to log errors if a violation is detected
32
+ * @returns Returns true if the joinpoint violates the rule, false otherwise
33
+ */
34
+ match($jp: Joinpoint, logErrors: boolean = false): boolean {
35
+ if (!($jp instanceof Program)) return false;
36
+
37
+ const typedefDecls = Query.search(TypedefDecl).get();
38
+
39
+ this.invalidIdentifiers = getIdentifierDecls().filter((identifierJp) =>
40
+ {
41
+ if (isTagDecl(identifierJp)) {
42
+ return typedefDecls.filter((decl) =>
43
+ getIdentifierName(identifierJp) === getIdentifierName(decl) &&
44
+ getTypeDefDecl(identifierJp)?.astId !== decl.astId
45
+ ).length > 0
46
+ }
47
+ else if (identifierJp instanceof TypedefDecl) {
48
+ return isIdentifierNameDeclaredBefore(identifierJp, typedefDecls);
49
+ }
50
+ return isIdentifierDuplicated(identifierJp, typedefDecls);
51
+ });
52
+
53
+ const nonCompliant = this.invalidIdentifiers.length > 0;
54
+
55
+ if (nonCompliant && logErrors) {
56
+ this.invalidIdentifiers.forEach(identifierJp => {
57
+ this.logMISRAError(identifierJp, `Identifier '${getIdentifierName(identifierJp)}' is also the name of a typedef. Typedef identifiers must not be reused.`);
58
+ })
59
+ }
60
+ return nonCompliant;
61
+ }
62
+ }
@@ -0,0 +1,61 @@
1
+ import { Joinpoint, Program, TypedefDecl } from "@specs-feup/clava/api/Joinpoints.js";
2
+ import { getIdentifierName, isIdentifierDuplicated, isIdentifierNameDeclaredBefore } from "../../utils/IdentifierUtils.js";
3
+ import { getTypeDefDecl } from "../../utils/TypeDeclUtils.js";
4
+ import Query from "@specs-feup/lara/api/weaver/Query.js";
5
+ import { isTagDecl } from "../../utils/JoinpointUtils.js";
6
+ import { getIdentifierDecls } from "../../utils/ProgramUtils.js";
7
+ import IdentifierRenameRule from "./IdentifierRenameRule.js";
8
+ import { AnalysisType } from "../../MISRA.js";
9
+
10
+ /**
11
+ * MISRA-C Rule 5.7: A tag name shall be a unique identifier.
12
+ *
13
+ * Exception: The tag name may be the same as the typedef name with which it is associated.
14
+ */
15
+ export default class Rule_5_7_UniqueTagNames extends IdentifierRenameRule {
16
+ /**
17
+ * Scope of analysis
18
+ */
19
+ readonly analysisType = AnalysisType.SYSTEM;
20
+
21
+ /**
22
+ * @returns Rule identifier according to MISRA-C:2012
23
+ */
24
+ override get name(): string {
25
+ return "5.7";
26
+ }
27
+
28
+ /**
29
+ * Checks whether the given joinpoint represents an identifier that has the same name as any tag (i.e., enum, union or struct)
30
+ *
31
+ * @param $jp - Joinpoint to analyze
32
+ * @param logErrors - [logErrors=false] - Whether to log errors if a violation is detected
33
+ * @returns Returns true if the joinpoint violates the rule, false otherwise
34
+ */
35
+ match($jp: Joinpoint, logErrors: boolean = false): boolean {
36
+ if (!($jp instanceof Program)) return false;
37
+
38
+ const tagDecls = Query.search(Joinpoint, (jp => {return isTagDecl(jp)})).get();
39
+ this.invalidIdentifiers = getIdentifierDecls().filter((identifierJp) =>
40
+ {
41
+ if (identifierJp instanceof TypedefDecl) {
42
+ return tagDecls.filter((tag) =>
43
+ getIdentifierName(identifierJp) === getIdentifierName(tag) &&
44
+ getTypeDefDecl(tag)?.ast !== identifierJp.ast
45
+ ).length > 0;
46
+ }
47
+ else if (isTagDecl(identifierJp)) {
48
+ return isIdentifierNameDeclaredBefore(identifierJp, tagDecls);
49
+ }
50
+ return isIdentifierDuplicated(identifierJp, tagDecls);
51
+ });
52
+ const nonCompliant = this.invalidIdentifiers.length > 0;
53
+
54
+ if (nonCompliant && logErrors) {
55
+ this.invalidIdentifiers.forEach(identifierJp => {
56
+ this.logMISRAError(identifierJp, `Identifier '${getIdentifierName(identifierJp)}' is also the name of a tag. Tag identifiers must not be reused.`);
57
+ })
58
+ }
59
+ return nonCompliant;
60
+ }
61
+ }
@@ -0,0 +1,47 @@
1
+ import { Joinpoint, Program } from "@specs-feup/clava/api/Joinpoints.js";
2
+ import { getIdentifierName, isExternalLinkageIdentifier, isIdentifierDuplicated, isIdentifierNameDeclaredBefore } from "../../utils/IdentifierUtils.js";
3
+ import { getExternalLinkageIdentifiers, getIdentifierDecls } from "../../utils/ProgramUtils.js";
4
+ import IdentifierRenameRule from "./IdentifierRenameRule.js";
5
+ import { AnalysisType } from "../../MISRA.js";
6
+
7
+ /**
8
+ * MISRA-C Rule 5.8: Identifiers that define objects or functions with external linkage shall be unique
9
+ */
10
+ export default class Rule_5_8_UniqueExternalLinkIdentifiers extends IdentifierRenameRule {
11
+ /**
12
+ * Scope of analysis
13
+ */
14
+ readonly analysisType = AnalysisType.SYSTEM;
15
+
16
+ /**
17
+ * @returns Rule identifier according to MISRA-C:2012
18
+ */
19
+ override get name(): string {
20
+ return "5.8";
21
+ }
22
+
23
+ /**
24
+ *
25
+ * @param $jp - Joinpoint to analyze
26
+ * @param logErrors - [logErrors=false] - Whether to log errors if a violation is detected
27
+ * @returns Returns true if the joinpoint violates the rule, false otherwise
28
+ */
29
+ match($jp: Joinpoint, logErrors: boolean = false): boolean {
30
+ if (!($jp instanceof Program)) return false;
31
+
32
+ const externalLinkageIdentifiers = getExternalLinkageIdentifiers();
33
+ this.invalidIdentifiers = getIdentifierDecls().filter((identifierJp) =>
34
+ isExternalLinkageIdentifier(identifierJp) ?
35
+ isIdentifierNameDeclaredBefore(identifierJp, externalLinkageIdentifiers) :
36
+ isIdentifierDuplicated(identifierJp, externalLinkageIdentifiers)
37
+ );
38
+
39
+ const nonCompliant = this.invalidIdentifiers.length > 0;
40
+ if (nonCompliant && logErrors) {
41
+ this.invalidIdentifiers.forEach(identifierJp => {
42
+ this.logMISRAError(identifierJp, `Identifier '${getIdentifierName(identifierJp)}' is already defined with external linkage in this or other file.`);
43
+ })
44
+ }
45
+ return nonCompliant;
46
+ }
47
+ }