ceedling 1.2.2 → 1.2.3

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 (390) hide show
  1. checksums.yaml +4 -4
  2. data/GIT_COMMIT_SHA +1 -1
  3. data/README.md +1 -1
  4. data/assets/fixtures/tests_with_partials_coverage/src/private_macro.c +7 -0
  5. data/assets/fixtures/tests_with_partials_coverage/src/private_macro.h +5 -0
  6. data/assets/fixtures/tests_with_partials_coverage/src/tally.c +13 -0
  7. data/assets/fixtures/tests_with_partials_coverage/src/tally.h +4 -0
  8. data/assets/fixtures/tests_with_partials_coverage/test/test_private_macro.c +10 -0
  9. data/assets/fixtures/tests_with_partials_coverage/test/test_tally.c +9 -0
  10. data/docs/BreakingChanges.md +16 -0
  11. data/docs/Changelog.md +71 -0
  12. data/docs/KnownIssues.md +91 -4
  13. data/examples/wondrous_forest/README.md +29 -1
  14. data/examples/wondrous_forest/src/ForestUnits.h +23 -0
  15. data/examples/wondrous_forest/src/RainGauge.c +59 -0
  16. data/examples/wondrous_forest/src/RainGauge.h +39 -0
  17. data/examples/wondrous_forest/src/RainTipIsr.c +17 -0
  18. data/examples/wondrous_forest/src/RainTipIsr.h +14 -0
  19. data/examples/wondrous_forest/test/TestRainGauge.c +126 -0
  20. data/lib/ceedling/c_extractor/README.md +15 -7
  21. data/lib/ceedling/c_extractor/c_extractor.rb +25 -14
  22. data/lib/ceedling/c_extractor/c_extractor_code_text.rb +84 -54
  23. data/lib/ceedling/c_extractor/c_extractor_constants.rb +18 -7
  24. data/lib/ceedling/c_extractor/c_extractor_declarations.rb +319 -283
  25. data/lib/ceedling/c_extractor/c_extractor_declarator.rb +285 -0
  26. data/lib/ceedling/c_extractor/c_extractor_defined_type.rb +111 -0
  27. data/lib/ceedling/c_extractor/c_extractor_definitions.rb +174 -121
  28. data/lib/ceedling/c_extractor/c_extractor_functions.rb +191 -162
  29. data/lib/ceedling/c_extractor/c_extractor_preprocessing.rb +46 -55
  30. data/lib/ceedling/c_extractor/c_extractor_statement.rb +30 -0
  31. data/lib/ceedling/c_extractor/c_extractor_types.rb +63 -41
  32. data/lib/ceedling/c_extractor/c_module_edits.rb +48 -0
  33. data/lib/ceedling/c_identifier.rb +21 -0
  34. data/lib/ceedling/ceedling.h +8 -0
  35. data/lib/ceedling/config/configurator.rb +4 -3
  36. data/lib/ceedling/config/configurator_builder.rb +17 -0
  37. data/lib/ceedling/config/configurator_setup.rb +29 -5
  38. data/lib/ceedling/constants.rb +5 -1
  39. data/lib/ceedling/defaults.rb +31 -1
  40. data/lib/ceedling/dependencies/CACHE_SCHEMA.md +118 -0
  41. data/lib/ceedling/dependencies/README.md +1 -1
  42. data/lib/ceedling/dependencies/dependency_cache_store.rb +30 -25
  43. data/lib/ceedling/dependencies/dependency_tracker.rb +9 -3
  44. data/lib/ceedling/dependencies/dependinator.rb +13 -1
  45. data/lib/ceedling/diagnostics/DOCTOR.md +142 -0
  46. data/lib/ceedling/diagnostics/README.md +5 -0
  47. data/lib/ceedling/diagnostics/build_step_doctor.rb +110 -0
  48. data/lib/ceedling/diagnostics/duplicate_symbol_specialist.rb +108 -0
  49. data/lib/ceedling/diagnostics/guidance_phrases.rb +30 -0
  50. data/lib/ceedling/diagnostics/header_collision_guidance.rb +56 -0
  51. data/lib/ceedling/diagnostics/header_collision_specialist.rb +152 -0
  52. data/lib/ceedling/diagnostics/include_depth_specialist.rb +132 -0
  53. data/lib/ceedling/diagnostics/missing_symbol_specialist.rb +111 -0
  54. data/lib/ceedling/diagnostics/partial_name_specialist.rb +134 -0
  55. data/lib/ceedling/diagnostics/toolchain_output.rb +44 -0
  56. data/lib/ceedling/filesystem/PATH_HANDLING.md +83 -27
  57. data/lib/ceedling/filesystem/README.md +6 -1
  58. data/lib/ceedling/filesystem/build_roots.rb +105 -0
  59. data/lib/ceedling/filesystem/build_subpath.rb +110 -25
  60. data/lib/ceedling/filesystem/file_finder.rb +76 -110
  61. data/lib/ceedling/filesystem/file_finder_helper.rb +33 -11
  62. data/lib/ceedling/filesystem/file_path_utils.rb +113 -91
  63. data/lib/ceedling/filesystem/file_wrapper.rb +105 -28
  64. data/lib/ceedling/filesystem/path_matcher.rb +21 -0
  65. data/lib/ceedling/filesystem/test_build_layout.rb +114 -0
  66. data/lib/ceedling/filesystem/win_long_paths.rb +153 -0
  67. data/lib/ceedling/filesystem/win_path_limit.rb +83 -0
  68. data/lib/ceedling/filesystem/win_path_prober.rb +52 -0
  69. data/lib/ceedling/generators/generator.rb +27 -64
  70. data/lib/ceedling/generators/generator_helper.rb +0 -44
  71. data/lib/ceedling/generators/generator_mocks.rb +5 -0
  72. data/lib/ceedling/generators/generator_partials.rb +305 -243
  73. data/lib/ceedling/generators/generator_test_runner.rb +5 -8
  74. data/lib/ceedling/generators/partial_header_writer.rb +95 -0
  75. data/lib/ceedling/generators/partial_source_writer.rb +137 -0
  76. data/lib/ceedling/includes/header_search.rb +225 -0
  77. data/lib/ceedling/includes/include_factory.rb +9 -12
  78. data/lib/ceedling/includes/include_pathinator.rb +53 -31
  79. data/lib/ceedling/includes/includes.rb +149 -37
  80. data/lib/ceedling/objects.yml +89 -4
  81. data/lib/ceedling/partials/README.md +110 -16
  82. data/lib/ceedling/partials/carry_plan.rb +71 -0
  83. data/lib/ceedling/partials/definition_head.rb +74 -0
  84. data/lib/ceedling/partials/include_groups.rb +37 -0
  85. data/lib/ceedling/partials/interface_extras.rb +25 -0
  86. data/lib/ceedling/partials/partializer.rb +599 -120
  87. data/lib/ceedling/partials/partializer_config.rb +5 -0
  88. data/lib/ceedling/partials/partializer_helper.rb +851 -145
  89. data/lib/ceedling/partials/partializer_reconciler.rb +174 -0
  90. data/lib/ceedling/partials/partializer_utils.rb +89 -109
  91. data/lib/ceedling/partials/partials.rb +153 -10
  92. data/lib/ceedling/partials/type_macros.rb +126 -0
  93. data/lib/ceedling/preprocess/README.md +43 -33
  94. data/lib/ceedling/preprocess/preprocessinator.rb +100 -186
  95. data/lib/ceedling/preprocess/preprocessinator_code_finder.rb +65 -72
  96. data/lib/ceedling/preprocess/preprocessinator_file_assembler.rb +31 -75
  97. data/lib/ceedling/preprocess/preprocessinator_reconstructor.rb +51 -14
  98. data/lib/ceedling/setupinator.rb +16 -3
  99. data/lib/ceedling/test_invoker/HEADER_COLLISIONS.md +21 -7
  100. data/lib/ceedling/test_invoker/PIPELINE.md +57 -16
  101. data/lib/ceedling/test_invoker/README.md +1 -1
  102. data/lib/ceedling/test_invoker/derived_search_path_notices.rb +45 -0
  103. data/lib/ceedling/test_invoker/mock_locator.rb +116 -9
  104. data/lib/ceedling/test_invoker/partial_files_preprocessinator.rb +181 -0
  105. data/lib/ceedling/test_invoker/partials_manager.rb +265 -308
  106. data/lib/ceedling/test_invoker/standin_headerinator.rb +8 -15
  107. data/lib/ceedling/test_invoker/test_bare_includes_extractinator.rb +26 -3
  108. data/lib/ceedling/test_invoker/test_build_executor.rb +338 -198
  109. data/lib/ceedling/test_invoker/test_build_planner.rb +13 -19
  110. data/lib/ceedling/test_invoker/test_build_setup.rb +26 -6
  111. data/lib/ceedling/test_invoker/test_build_validations.rb +23 -20
  112. data/lib/ceedling/test_invoker/test_derived_search_paths.rb +189 -0
  113. data/lib/ceedling/test_invoker/test_file_preprocessinator.rb +1 -2
  114. data/lib/ceedling/test_invoker/test_includes_reconcilinator.rb +1 -1
  115. data/lib/ceedling/test_invoker/test_invoker_types.rb +28 -20
  116. data/lib/ceedling/test_invoker/test_pipeline_helpers.rb +5 -3
  117. data/lib/ceedling/test_invoker/test_pipeline_manager.rb +25 -8
  118. data/lib/ceedling/test_invoker/test_runner_generatinator.rb +31 -3
  119. data/lib/ceedling/test_invoker/test_search_paths.rb +30 -8
  120. data/lib/ceedling/test_invoker/test_substitutions.rb +72 -0
  121. data/lib/ceedling/tools/shell_quoting.rb +45 -0
  122. data/lib/ceedling/tools/tool_quoting.rb +79 -0
  123. data/lib/ceedling/{tool_validator.rb → tools/tool_validator.rb} +18 -7
  124. data/lib/version.rb +1 -1
  125. data/plugins/bullseye/bullseye.rake +1 -3
  126. data/plugins/bullseye/lib/bullseye.rb +15 -2
  127. data/plugins/command_hooks/lib/command_hooks.rb +1 -1
  128. data/plugins/fff/lib/fff.rb +42 -15
  129. data/plugins/fff/lib/fff_mock_generator.rb +12 -9
  130. data/plugins/fff/spec/fff_cmock_wrapper_spec.rb +134 -0
  131. data/plugins/fff/spec/fff_mock_header_generator_spec.rb +9 -0
  132. data/plugins/fff/spec/fff_mock_source_generator_spec.rb +20 -0
  133. data/plugins/gcov/lib/console_reportinator.rb +25 -19
  134. data/plugins/gcov/lib/reportgenerator_reportinator.rb +2 -1
  135. data/site-local/404.html +2 -2
  136. data/site-local/assets/javascripts/{bundle.c21346df.min.js → bundle.a5f9d56c.min.js} +1 -1
  137. data/site-local/assets/javascripts/{bundle.c21346df.min.js.map → bundle.a5f9d56c.min.js.map} +2 -2
  138. data/site-local/configuration/environment-vars.html +2 -2
  139. data/site-local/configuration/global-collections.html +3 -3
  140. data/site-local/configuration/index.html +2 -2
  141. data/site-local/configuration/loading.html +2 -2
  142. data/site-local/configuration/mixins.html +2 -2
  143. data/site-local/configuration/parallel-builds.html +2 -2
  144. data/site-local/configuration/project-file.html +2 -2
  145. data/site-local/configuration/reference/cexception.html +2 -2
  146. data/site-local/configuration/reference/cmock.html +5 -2
  147. data/site-local/configuration/reference/defines.html +2 -2
  148. data/site-local/configuration/reference/environment.html +2 -2
  149. data/site-local/configuration/reference/extension.html +2 -2
  150. data/site-local/configuration/reference/files.html +56 -9
  151. data/site-local/configuration/reference/flags.html +2 -2
  152. data/site-local/configuration/reference/index.html +2 -2
  153. data/site-local/configuration/reference/libraries.html +2 -2
  154. data/site-local/configuration/reference/mixins.html +2 -2
  155. data/site-local/configuration/reference/partials.html +84 -2
  156. data/site-local/configuration/reference/paths.html +7 -2
  157. data/site-local/configuration/reference/plugins.html +2 -2
  158. data/site-local/configuration/reference/project.html +247 -5
  159. data/site-local/configuration/reference/release-build.html +2 -2
  160. data/site-local/configuration/reference/test-build.html +2 -2
  161. data/site-local/configuration/reference/test-runner.html +2 -2
  162. data/site-local/configuration/reference/tools.html +49 -4
  163. data/site-local/configuration/reference/unity.html +2 -2
  164. data/site-local/configuration/which-ceedling.html +2 -2
  165. data/site-local/development/index.html +2 -2
  166. data/site-local/development/plugins/configuration.html +2 -2
  167. data/site-local/development/plugins/index.html +2 -2
  168. data/site-local/development/plugins/plugin-subclass.html +5 -4
  169. data/site-local/development/plugins/rake-tasks.html +2 -2
  170. data/site-local/development/releases.html +2 -2
  171. data/site-local/development/workflow.html +2 -2
  172. data/site-local/getting-started/builds.html +2 -2
  173. data/site-local/getting-started/command-line.html +2 -2
  174. data/site-local/getting-started/example-projects/cipher-quest.html +2 -2
  175. data/site-local/getting-started/example-projects/index.html +2 -2
  176. data/site-local/getting-started/example-projects/temp-sensor.html +2 -2
  177. data/site-local/getting-started/example-projects/wondrous-forest.html +10 -2
  178. data/site-local/getting-started/index.html +2 -2
  179. data/site-local/getting-started/installation.html +2 -2
  180. data/site-local/getting-started/quick-start.html +2 -2
  181. data/site-local/help.html +2 -2
  182. data/site-local/index.html +2 -2
  183. data/site-local/overview/build-system.html +2 -2
  184. data/site-local/overview/index.html +2 -2
  185. data/site-local/overview/test-environments.html +2 -2
  186. data/site-local/overview/tools-and-frameworks.html +2 -2
  187. data/site-local/plugins/beep.html +2 -2
  188. data/site-local/plugins/bullseye/index.html +45 -38
  189. data/site-local/plugins/bullseye/licensing.html +2 -2
  190. data/site-local/plugins/command-hooks.html +4 -4
  191. data/site-local/plugins/compile-commands-json-db.html +2 -2
  192. data/site-local/plugins/cppcheck.html +2 -2
  193. data/site-local/plugins/dependencies.html +2 -2
  194. data/site-local/plugins/fff.html +16 -2
  195. data/site-local/plugins/gcov/examples.html +2 -2
  196. data/site-local/plugins/gcov/gcovr.html +8 -2
  197. data/site-local/plugins/gcov/index.html +2 -2
  198. data/site-local/plugins/gcov/overview.html +2 -2
  199. data/site-local/plugins/gcov/reportgenerator.html +18 -14
  200. data/site-local/plugins/gcov/reporting.html +2 -2
  201. data/site-local/plugins/gcov/setup.html +2 -2
  202. data/site-local/plugins/gcov/tool-versions.html +2 -2
  203. data/site-local/plugins/gcov/troubleshooting.html +2 -2
  204. data/site-local/plugins/index.html +2 -2
  205. data/site-local/plugins/module-generator.html +2 -2
  206. data/site-local/plugins/report-build-warnings-log.html +2 -2
  207. data/site-local/plugins/report-tests-gtestlike-stdout.html +2 -2
  208. data/site-local/plugins/report-tests-ide-stdout.html +2 -2
  209. data/site-local/plugins/report-tests-log-factory.html +2 -2
  210. data/site-local/plugins/report-tests-pretty-stdout.html +2 -2
  211. data/site-local/plugins/report-tests-raw-output-log.html +2 -2
  212. data/site-local/plugins/report-tests-teamcity-stdout.html +2 -2
  213. data/site-local/plugins/sanitize.html +2 -2
  214. data/site-local/plugins/valgrind.html +2 -2
  215. data/site-local/project/index.html +2 -2
  216. data/site-local/project/release-history.html +2 -2
  217. data/site-local/project/sbom.html +2 -2
  218. data/site-local/project/upgrade.html +2 -2
  219. data/site-local/reference/build-directives.html +2 -2
  220. data/site-local/reference/command-line.html +2 -2
  221. data/site-local/reference/environment-vars.html +2 -2
  222. data/site-local/reference/gcov-plugin.html +2 -2
  223. data/site-local/reference/global-collections.html +12 -12
  224. data/site-local/reference/index.html +2 -2
  225. data/site-local/reference/partials-macros.html +47 -2
  226. data/site-local/reference/project-configuration.html +2 -2
  227. data/site-local/sitemap.xml.gz +0 -0
  228. data/site-local/snapshot/examples/cipher_quest/index.html +2 -2
  229. data/site-local/snapshot/examples/temp_sensor/index.html +2 -2
  230. data/site-local/snapshot/examples/wondrous_forest/index.html +72 -15
  231. data/site-local/testing-guide/build-directives.html +2 -2
  232. data/site-local/testing-guide/conventions.html +67 -22
  233. data/site-local/testing-guide/frameworks.html +2 -2
  234. data/site-local/testing-guide/index.html +2 -2
  235. data/site-local/testing-guide/partials/configuration.html +31 -4
  236. data/site-local/testing-guide/partials/conventions.html +509 -8
  237. data/site-local/testing-guide/partials/directives.html +4 -3
  238. data/site-local/testing-guide/partials/example.html +2 -2
  239. data/site-local/testing-guide/partials/index.html +2 -2
  240. data/site-local/testing-guide/partials/overview.html +30 -12
  241. data/site-local/testing-guide/partials/variables.html +169 -4
  242. data/site-local/testing-guide/test-cases.html +2 -2
  243. data/site-local/testing-guide/test-sample.html +2 -2
  244. data/site-local/testing-guide/test-suite-anatomy.html +2 -2
  245. data/spec/integration/build_step_doctor_spec.rb +270 -0
  246. data/spec/integration/c_extractor_composition_spec.rb +254 -4
  247. data/spec/integration/includes_extraction_spec.rb +27 -14
  248. data/spec/integration/nested_test_artifacts_spec.rb +1 -1
  249. data/spec/integration/partial_files_preprocessing_spec.rb +128 -0
  250. data/spec/integration/partials_carry_spec.rb +520 -0
  251. data/spec/integration/partials_declarations_spec.rb +655 -0
  252. data/spec/integration/partials_fallback_preprocessing_spec.rb +65 -0
  253. data/spec/integration/partials_function_pointers_spec.rb +277 -0
  254. data/spec/integration/partials_header_composition_spec.rb +268 -35
  255. data/spec/integration/partials_macro_decorators_spec.rb +267 -0
  256. data/spec/integration/partials_module_resolution_spec.rb +26 -19
  257. data/spec/integration/partials_symbol_linkage_spec.rb +13 -0
  258. data/spec/integration/path_length_reporting_spec.rb +64 -0
  259. data/spec/integration/path_scenarios_spec.rb +80 -3
  260. data/spec/integration/quote_include_isolator_spec.rb +56 -0
  261. data/spec/integration/test_build_parallel_barrier_spec.rb +1 -1
  262. data/spec/integration/test_includes_extractinator_spec.rb +70 -5
  263. data/spec/integration/test_naming_spec.rb +6 -3
  264. data/spec/integration/test_source_file_checks_spec.rb +5 -3
  265. data/spec/integration/tool_executor_spec.rb +1 -1
  266. data/spec/integration/tool_validator_spec.rb +2 -2
  267. data/spec/integration/win_long_paths_probe_spec.rb +46 -0
  268. data/spec/integration/win_path_limit_spec.rb +53 -0
  269. data/spec/integration/windows_registry_query_tool_spec.rb +41 -0
  270. data/spec/support/integration/configurator_integration_helper.rb +8 -3
  271. data/spec/support/integration/partial_files_harness.rb +181 -0
  272. data/spec/support/integration/partials_generation_integration_helper.rb +237 -141
  273. data/spec/support/integration/path_scenario_harness.rb +4 -2
  274. data/spec/support/integration/spec_integration_helper.rb +8 -3
  275. data/spec/support/integration/test_includes_harness.rb +56 -35
  276. data/spec/support/integration/tool_executor_integration_helper.rb +14 -2
  277. data/spec/system/bullseye_deployment_spec.rb +2 -0
  278. data/spec/system/delta_builds_spec.rb +26 -12
  279. data/spec/system/encoding_stress_spec.rb +4 -4
  280. data/spec/system/example_wondrous_forest_spec.rb +2 -2
  281. data/spec/system/gcov_deployment_spec.rb +2 -0
  282. data/spec/system/header_include_path_validation_spec.rb +4 -4
  283. data/spec/system/header_search_paths_spec.rb +1083 -0
  284. data/spec/system/mock_include_folders_spec.rb +121 -0
  285. data/spec/system/mockable_header_reassembly_spec.rb +184 -0
  286. data/spec/system/partials_carry_spec.rb +581 -0
  287. data/spec/system/partials_delta_builds_spec.rb +280 -0
  288. data/spec/system/partials_function_pointers_spec.rb +106 -0
  289. data/spec/system/partials_macro_decorators_spec.rb +127 -0
  290. data/spec/system/partials_module_disambiguation_spec.rb +125 -0
  291. data/spec/system/path_handling_probes_spec.rb +16 -12
  292. data/spec/system/pinned_thread_count_spec.rb +4 -4
  293. data/spec/system/preprocessing_fallback_conditionals_spec.rb +1 -1
  294. data/spec/system/short_build_paths_spec.rb +87 -0
  295. data/spec/system/sibling_header_collision_spec.rb +30 -6
  296. data/spec/system/support/bullseye_common_test_cases.rb +42 -0
  297. data/spec/system/support/common_test_cases.rb +2 -2
  298. data/spec/system/support/gcov_partials_test_cases.rb +48 -0
  299. data/spec/system/test_build_layout_spec.rb +209 -0
  300. data/spec/system/test_source_file_directive_spec.rb +35 -5
  301. data/spec/units/c_extractor/c_extractor_code_text_spec.rb +67 -10
  302. data/spec/units/c_extractor/c_extractor_declarations_spec.rb +501 -4
  303. data/spec/units/c_extractor/c_extractor_declarator_spec.rb +245 -0
  304. data/spec/units/c_extractor/c_extractor_defined_type_spec.rb +45 -0
  305. data/spec/units/c_extractor/c_extractor_definitions_spec.rb +163 -3
  306. data/spec/units/c_extractor/c_extractor_functions_spec.rb +462 -21
  307. data/spec/units/c_extractor/c_extractor_preprocessing_spec.rb +39 -0
  308. data/spec/units/c_extractor/c_extractor_spec.rb +4 -4
  309. data/spec/units/c_extractor/c_extractor_types_spec.rb +163 -0
  310. data/spec/units/c_identifier_spec.rb +35 -0
  311. data/spec/units/config/configurator_builder_spec.rb +24 -0
  312. data/spec/units/config/configurator_setup_spec.rb +72 -4
  313. data/spec/units/config/configurator_spec.rb +27 -1
  314. data/spec/units/dependencies/dependency_cache_store_spec.rb +68 -3
  315. data/spec/units/dependencies/dependency_tracker_spec.rb +25 -0
  316. data/spec/units/dependencies/dependinator_spec.rb +17 -2
  317. data/spec/units/diagnostics/build_step_doctor_spec.rb +170 -0
  318. data/spec/units/diagnostics/duplicate_symbol_specialist_spec.rb +117 -0
  319. data/spec/units/diagnostics/header_collision_specialist_spec.rb +267 -0
  320. data/spec/units/diagnostics/include_depth_specialist_spec.rb +137 -0
  321. data/spec/units/diagnostics/missing_symbol_specialist_spec.rb +154 -0
  322. data/spec/units/diagnostics/partial_name_specialist_spec.rb +129 -0
  323. data/spec/units/diagnostics/toolchain_output_spec.rb +86 -0
  324. data/spec/units/encodinator_spec.rb +3 -4
  325. data/spec/units/filesystem/build_roots_spec.rb +131 -0
  326. data/spec/units/filesystem/build_subpath_spec.rb +113 -0
  327. data/spec/units/filesystem/file_finder_helper_spec.rb +80 -0
  328. data/spec/units/filesystem/file_finder_spec.rb +242 -58
  329. data/spec/units/filesystem/file_path_utils_spec.rb +251 -118
  330. data/spec/units/filesystem/file_wrapper_spec.rb +191 -6
  331. data/spec/units/filesystem/path_matcher_spec.rb +36 -0
  332. data/spec/units/filesystem/test_build_layout_spec.rb +216 -0
  333. data/spec/units/filesystem/win_long_paths_spec.rb +204 -0
  334. data/spec/units/filesystem/win_path_prober_spec.rb +158 -0
  335. data/spec/units/generators/generator_helper_spec.rb +0 -60
  336. data/spec/units/generators/generator_mocks_spec.rb +13 -0
  337. data/spec/units/generators/generator_partials_spec.rb +656 -55
  338. data/spec/units/generators/generator_spec.rb +26 -0
  339. data/spec/units/generators/generator_test_runner_spec.rb +9 -5
  340. data/spec/units/generators/partial_source_writer_spec.rb +129 -0
  341. data/spec/units/includes/header_search_spec.rb +406 -0
  342. data/spec/units/includes/include_factory_spec.rb +22 -5
  343. data/spec/units/includes/include_pathinator_spec.rb +77 -13
  344. data/spec/units/includes/include_spec.rb +14 -0
  345. data/spec/units/includes/includes_spec.rb +176 -19
  346. data/spec/units/loginator_spec.rb +31 -0
  347. data/spec/units/mock_locator_spec.rb +199 -5
  348. data/spec/units/partials/include_groups_spec.rb +42 -0
  349. data/spec/units/partials/partializer_config_spec.rb +25 -0
  350. data/spec/units/partials/partializer_helper_carry_spec.rb +661 -0
  351. data/spec/units/partials/partializer_helper_interface_extras_spec.rb +396 -0
  352. data/spec/units/partials/partializer_helper_spec.rb +529 -277
  353. data/spec/units/partials/partializer_reconciler_spec.rb +661 -0
  354. data/spec/units/partials/partializer_spec.rb +1578 -270
  355. data/spec/units/partials/partializer_utils_spec.rb +269 -232
  356. data/spec/units/partials/partials_rename_spec.rb +160 -0
  357. data/spec/units/partials_manager_spec.rb +554 -261
  358. data/spec/units/plugins/bullseye_spec.rb +34 -0
  359. data/spec/units/plugins/console_reportinator_spec.rb +58 -9
  360. data/spec/units/plugins/report_tests_teamcity_stdout_spec.rb +1 -1
  361. data/spec/units/plugins/reportgenerator_reportinator_spec.rb +51 -3
  362. data/spec/units/preprocess/preprocessinator_code_finder_spec.rb +73 -10
  363. data/spec/units/preprocess/preprocessinator_file_assembler_spec.rb +125 -37
  364. data/spec/units/preprocess/preprocessinator_includes_handler_spec.rb +15 -0
  365. data/spec/units/preprocess/preprocessinator_reconstructor_spec.rb +197 -0
  366. data/spec/units/preprocess/preprocessinator_spec.rb +153 -95
  367. data/spec/units/setupinator_spec.rb +51 -0
  368. data/spec/units/standin_headerinator_spec.rb +27 -11
  369. data/spec/units/test_build_executor_spec.rb +780 -166
  370. data/spec/units/test_build_planner_spec.rb +116 -28
  371. data/spec/units/test_build_setup_spec.rb +83 -18
  372. data/spec/units/test_context_extractor_spec.rb +11 -2
  373. data/spec/units/test_derived_search_paths_spec.rb +259 -0
  374. data/spec/units/test_includes_extractinator_spec.rb +56 -8
  375. data/spec/units/test_invoker/objects_wiring_spec.rb +20 -3
  376. data/spec/units/test_invoker/partial_files_preprocessinator_spec.rb +264 -0
  377. data/spec/units/test_invoker/test_substitutions_spec.rb +122 -0
  378. data/spec/units/test_invoker_types_spec.rb +8 -7
  379. data/spec/units/test_pipeline_manager_spec.rb +54 -13
  380. data/spec/units/test_runner_generatinator_spec.rb +93 -1
  381. data/spec/units/test_search_paths_spec.rb +73 -5
  382. data/spec/units/{default_tools_quoting_spec.rb → tools/default_tools_quoting_spec.rb} +39 -5
  383. data/spec/units/tools/shell_quoting_spec.rb +76 -0
  384. data/spec/units/{tool_executor_helper_spec.rb → tools/tool_executor_helper_spec.rb} +1 -1
  385. data/spec/units/{tool_executor_spec.rb → tools/tool_executor_spec.rb} +1 -1
  386. data/spec/units/{tool_validator_spec.rb → tools/tool_validator_spec.rb} +110 -1
  387. data/spec/units/windows_registry_query_tool_spec.rb +53 -0
  388. metadata +161 -16
  389. /data/lib/ceedling/{tool_executor.rb → tools/tool_executor.rb} +0 -0
  390. /data/lib/ceedling/{tool_executor_helper.rb → tools/tool_executor_helper.rb} +0 -0
@@ -5,9 +5,12 @@
5
5
  # SPDX-License-Identifier: MIT
6
6
  # =========================================================================
7
7
 
8
+ require 'set'
8
9
  require 'ceedling/exceptions'
9
10
  require 'ceedling/c_extractor/c_extractor_constants'
10
11
  require 'ceedling/c_extractor/c_extractor_types'
12
+ require 'ceedling/c_extractor/c_extractor_declarator'
13
+ require 'ceedling/c_extractor/c_extractor_defined_type'
11
14
 
12
15
  class CExtractorDeclarations
13
16
 
@@ -21,27 +24,69 @@ class CExtractorDeclarations
21
24
  @code_text = @c_extractor_code_text
22
25
  end
23
26
 
24
- # Attempts to extract a complete variable declaration from the scanner
27
+ # Text without a quote, a '/', a delimiter or a ';', which the scan passes in one step. A run
28
+ # stops at every '/', since one may start a comment.
29
+ PLAIN_RUN = %r{[^"'/()\[\]{};]+} unless const_defined?(:PLAIN_RUN, false)
30
+
31
+ # One item of a declaration's leading run. That is a decorator keyword, read whole, an
32
+ # alignment specifier or attribute with its arguments, or a C23 attribute in double brackets.
33
+ # Literals among the arguments may hold parentheses or brackets.
34
+ LEADING_RUN_ITEM = /\A(?:(?<keyword>#{DECORATOR_KEYWORDS.map { |kw| Regexp.escape( kw ) }.join( '|' )})(?![\w$])|(?:_Alignas|alignas|__attribute__|__attribute|__declspec)\s*(?<group>\((?>[^()"']+|"(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|\g<group>)*\))|(?=\[\s*\[)(?<brackets>\[(?>[^\[\]"']+|"(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|\g<brackets>)*\]))\s*/ unless const_defined?(:LEADING_RUN_ITEM, false)
35
+
36
+ # Each delimiter's own kind of depth, and how the delimiter changes it
37
+ DELIMITER_DEPTH = {
38
+ '(' => [:paren, 1], ')' => [:paren, -1], '[' => [:bracket, 1], ']' => [:bracket, -1],
39
+ '{' => [:brace, 1], '}' => [:brace, -1]
40
+ }.freeze unless const_defined?(:DELIMITER_DEPTH, false)
41
+
42
+ # How a declaration's text looks once its comments are blanked. A type and a name come first,
43
+ # and the last character before the ';' closes a name, a suffix, an initializer or a literal.
44
+ # A multiline initializer still matches.
45
+ DECLARATION_SHAPE = /[\w$]+.*[\w$\]\)\}"']\s*;$/m unless const_defined?(:DECLARATION_SHAPE, false)
46
+
47
+ # A declaration's last identifier, which names its variable once suffixes are gone
48
+ LAST_IDENTIFIER = /(?<![\w$])(#{IDENTIFIER.source})\s*\z/ unless const_defined?(:LAST_IDENTIFIER, false)
49
+
50
+ # A parenthesis or a bracket, or a literal or comment whose brackets don't count
51
+ SIZE_TOKEN = Regexp.union( /[()\[]/, CExtractorDeclarator::LITERAL_OR_COMMENT ) unless const_defined?(:SIZE_TOKEN, false)
52
+
53
+ # How each parenthesis changes the depth
54
+ PAREN_CHANGE = { '(' => 1, ')' => -1 }.freeze unless const_defined?(:PAREN_CHANGE, false)
55
+
56
+ # A declaration's array sizes, which follow its name
57
+ ARRAY_SUFFIX = /[\w$](\s*(?:\[[^\]]*\])+)\s*\z/ unless const_defined?(:ARRAY_SUFFIX, false)
58
+
59
+ # A comma, or a bracket or literal whose commas don't split a compound declaration
60
+ SPLIT_TOKEN = Regexp.union( /,/, CExtractorDeclarator::BALANCE_TOKEN ) unless const_defined?(:SPLIT_TOKEN, false)
61
+
62
+ # A bracket, a literal, a star or a word, which locate where a declarator starts
63
+ DECLARATOR_TOKEN = Regexp.union( CExtractorDeclarator::BALANCE_TOKEN, /\*/, CExtractorDeclarator::WORD ) unless const_defined?(:DECLARATOR_TOKEN, false)
64
+
65
+ # A base type's last two words, which may be a type and a name misread as part of it
66
+ LAST_TWO_WORDS = /(?<![\w$])(?<type>[A-Za-z_$][\w$]*)\s+(?<word>[A-Za-z_$][\w$]*)\s*\z/ unless const_defined?(:LAST_TWO_WORDS, false)
67
+
68
+ # Words that are no type, so the word after one is no name
69
+ NOT_A_TYPE = (DECORATOR_KEYWORDS + CExtractorDeclarator::QUALIFIERS + %w[struct union enum _Thread_local thread_local __thread]).to_set.freeze unless const_defined?(:NOT_A_TYPE, false)
70
+
71
+ # Words that may follow a type ahead of a name: qualifiers, type and storage keywords, and the
72
+ # memory-space keywords some compilers add, such as Keil's `xdata`
73
+ TYPE_WORDS = (CExtractorDeclarator::QUALIFIERS + %w[
74
+ void char short int long float double signed unsigned _Bool _Complex _Imaginary _Atomic _Thread_local thread_local
75
+ data idata xdata pdata bdata code far near huge rom ram eeprom
76
+ ]).to_set.freeze unless const_defined?(:TYPE_WORDS, false)
77
+
78
+ # A macro's spelling, all capitals
79
+ MACRO_CASE = /\A[A-Z_][A-Z0-9_$]*\z/ unless const_defined?(:MACRO_CASE, false)
80
+
81
+ # Extracts a complete variable declaration from the scanner, through its terminating ';'.
25
82
  #
26
- # Scans forward from the current scanner position looking for a complete C variable
27
- # declaration terminated by a semicolon. Handles complex declaration syntax including:
28
- # - Simple variables: `int x;`
29
- # - Pointers: `char* ptr;`, `int** buffer;`
30
- # - Arrays: `int arr[10];`, `char matrix[3][4];`
31
- # - Initializers: `int x = 5;`, `int arr[] = {1, 2, 3};`
32
- # - String literals: `char* str = "hello";`
33
- # - Qualifiers: `const int MAX;`, `static volatile int flag;`
34
- # - Function pointers: `void (*callback)(int);`
35
- # - Complex nested structures with balanced parentheses, brackets, and braces
36
- # - Compound declarations: `int x, y;` expanded to one struct per variable
83
+ # The scan counts parentheses, brackets and braces apart, and the declaration ends at the
84
+ # first ';' outside all three. Literals and comments are passed whole, so nothing inside one
85
+ # counts. A compound declaration, such as `int x, y;`, yields one record per variable.
37
86
  #
38
- # The extraction process:
39
- # 1. Tracks nesting depth of (), [], and {} to handle complex declarations
40
- # 2. Properly handles string literals (both " and ') including escape sequences
41
- # 3. Skips comments (both // line comments and /* block comments */)
42
- # 4. Stops at the first semicolon found at depth 0 (outside all nesting)
43
- # 5. Validates the extracted text looks like a valid declaration
44
- # 6. Expands compound declarations and parses each into a CVariableDeclaration struct
87
+ # Fallback preprocessing leaves comments in place. So each comment is blanked to a space
88
+ # before the text is checked, split and parsed, and a `//` can't swallow the rest of a joined
89
+ # line. Each record's `original` keeps the text as the scan read it.
45
90
  #
46
91
  # Parameters:
47
92
  # scanner: StringScanner positioned at potential start of variable declaration
@@ -55,235 +100,206 @@ class CExtractorDeclarations
55
100
  # On failure: Resets scanner position to starting position
56
101
  #
57
102
  # Safety:
58
- # A declaration with no terminating semicolon runs the scan to the end of
59
- # whatever buffer the caller (CExtractor#extract_next_feature) has grown so
60
- # far; that caller is the one place enforcing an overall length ceiling,
61
- # since only it knows how much more of the file remains to try growing into.
103
+ # A declaration with no terminating ';', or with a block comment that never closes, runs
104
+ # the scan to the end of the buffer. The caller, CExtractor#extract_next_feature, grows the
105
+ # buffer and alone enforces the overall length ceiling, since only it knows how much of the
106
+ # file remains.
62
107
  def try_extract_variable(scanner)
63
108
  start_pos = scanner.pos
109
+ depths = Hash.new( 0 )
64
110
 
65
- # Tracks paren, bracket, and brace depth simultaneously with an inline string state machine — not suitable for collect_balanced()
66
- paren_depth = 0
67
- bracket_depth = 0
68
- brace_depth = 0
69
- in_string = false
70
- string_char = nil
71
-
72
- # Scan until we find a semicolon at depth 0.
73
- # If we reach the end of string scanner, we failed to find something.
74
111
  until scanner.eos?
75
- char = scanner.peek(1)
76
-
77
- # Handle string literals
78
- if in_string
79
- if char == '\\'
80
- scanner.getch
81
- scanner.getch unless scanner.eos?
82
- next
83
- elsif char == string_char
84
- scanner.getch
85
- in_string = false
86
- string_char = nil
87
- next
88
- else
89
- scanner.getch
90
- next
91
- end
92
- end
93
-
94
- case char
95
- when '"', "'"
96
- in_string = true
97
- string_char = char
98
- scanner.getch
99
- when '/'
100
- # Handle comments -- peek(2) starting with '/' is only ever exactly '//' or '/*'
101
- # when it's a comment opener at all; anything else (a lone '/' or '/' followed by
102
- # some other character) falls through to the plain-character else below.
103
- if scanner.peek(2) == '//'
104
- # Line comment -- skip to end of line
105
- scanner.scan_until(/\n/) || scanner.terminate
106
- elsif scanner.peek(2) == '/*'
107
- # Block comment -- skip to closing */
108
- scanner.pos += 2
109
- scanner.scan_until(%r{\*/})
110
- else
111
- scanner.getch
112
- end
113
- when '='
114
- # Track assignment for initializer detection
115
- scanner.getch
116
- when '('
117
- paren_depth += 1
118
- scanner.getch
119
- when ')'
120
- paren_depth -= 1
121
- scanner.getch
122
- # Unbalanced parentheses -- not a valid declaration
123
- if paren_depth < 0
124
- scanner.pos = start_pos
125
- return [false, nil]
126
- end
127
- when '['
128
- bracket_depth += 1
129
- scanner.getch
130
- when ']'
131
- bracket_depth -= 1
132
- scanner.getch
133
- # Unbalanced brackets -- not a valid declaration
134
- if bracket_depth < 0
135
- scanner.pos = start_pos
136
- return [false, nil]
137
- end
138
- when '{'
139
- # Braces after '=' are initializers, not code blocks
140
- brace_depth += 1
141
- scanner.getch
142
- when '}'
143
- brace_depth -= 1
144
- scanner.getch
145
- # Unbalanced braces -- not a valid declaration
146
- if brace_depth < 0
147
- scanner.pos = start_pos
148
- return [false, nil]
149
- end
150
- when ';'
151
- # Found semicolon - check if it's at depth 0
152
- if paren_depth == 0 && bracket_depth == 0 && brace_depth == 0
153
- # This is the end of a declaration
154
- scanner.getch # Consume the semicolon
155
-
156
- # Extract the declaration
157
- declaration = scanner.string[start_pos...scanner.pos]
158
-
159
- # Verify this looks like a valid declaration
160
- # Must have at least a type and identifier
161
- # Can end with: word character, ], ), }, or " (for string initializers)
162
- # `/m` flag: declaration may span multiple lines (e.g. a multiline brace
163
- # initializer), so '.' must match embedded newlines too
164
- if declaration.match?(/\w+.*[\w\]\)\}"']\s*;$/m)
165
- return [true, expand_and_parse(declaration)]
166
- else
167
- scanner.pos = start_pos
168
- return [false, nil]
169
- end
170
- else
171
- # Semicolon inside parens, brackets, or braces -- keep scanning
172
- scanner.getch
173
- end
174
- else
175
- scanner.getch
176
- end
112
+ token = _pass_token( scanner )
113
+ break unless _track_depth( depths, token )
114
+ return _variable_found( scanner, start_pos ) if token == ';' and depths.values.all?( &:zero? )
177
115
  end
178
116
 
179
- # Reached end without finding a complete declaration
180
117
  scanner.pos = start_pos
181
- [false, nil]
118
+ return [false, nil]
182
119
  end
183
120
 
184
121
  private
185
122
 
186
- # Expand compound declarations and parse each into CVariableDeclaration structs
123
+ # Moves the scanner past one token. Returns it when it's a delimiter, a ';' or a lone '/', and
124
+ # nil when it's a literal, a comment or a run of plain text.
125
+ def _pass_token(scanner)
126
+ char = scanner.peek( 1 )
127
+ if char == '"' or char == "'"
128
+ @code_text.skip_c_string( scanner, char )
129
+ elsif scanner.check( %r{/[/*]} )
130
+ @code_text.skip_comment( scanner )
131
+ elsif !scanner.skip( PLAIN_RUN )
132
+ return scanner.getch
133
+ end
134
+
135
+ return nil
136
+ end
137
+
138
+ # Counts a delimiter into its own kind's depth. False once any kind closes more than it opened.
139
+ def _track_depth(depths, token)
140
+ kind, change = DELIMITER_DEPTH[token]
141
+ depths[kind] += change if kind
142
+ return depths.values.none?( &:negative? )
143
+ end
144
+
145
+ # The declaration ending at the scanner, split and parsed, or a failure when its text doesn't
146
+ # look like a declaration. The check and the parse read the text with its comments blanked.
147
+ def _variable_found(scanner, start_pos)
148
+ raw = scanner.string.byteslice( start_pos, scanner.pos - start_pos )
149
+ clean = _blank_comments( raw )
150
+ return [true, expand_and_parse( clean, raw )] if clean.match?( DECLARATION_SHAPE )
151
+
152
+ scanner.pos = start_pos
153
+ return [false, nil]
154
+ end
155
+
156
+ # The text with each comment replaced by a space. A literal stays whole, so `"//"` survives.
157
+ def _blank_comments(text)
158
+ return text.gsub( CExtractorDeclarator::LITERAL_OR_COMMENT ) { |match| match.start_with?( '/' ) ? ' ' : match }
159
+ end
160
+
161
+ # Expand compound declarations and parse each into CVariableDeclaration structs. A compound
162
+ # declaration that can't be split stays one record with no name, like any declaration whose
163
+ # name can't be read.
164
+ #
165
+ # A declaration that defines its type, as `static struct { int count; } s_ctx;` does, is read
166
+ # without that type. Each of its records names the definition, one CStatement they share, in
167
+ # `defined_type`. Its `type` and `text` hold only what follows the type, such as the `*` of
168
+ # `struct { int n; } *p;`.
187
169
  #
188
170
  # Parameters:
189
- # raw_text: String containing a complete C declaration (e.g., "static int x, y;")
171
+ # text: A complete declaration with its comments blanked (e.g., "static int x, y;")
172
+ # raw_text: The same declaration as written, which each record keeps as its `original`
190
173
  #
191
174
  # Returns: Array<CVariableDeclaration>
192
- def expand_and_parse(raw_text)
193
- expand_compound(raw_text).map { |individual| parse_declaration(individual, raw_text) }
175
+ def expand_and_parse(text, raw_text)
176
+ split = _defined_type( text )
177
+ return _expand_and_parse( text, raw_text ) if split.nil?
178
+
179
+ defined = CStatement.new( text: "#{split.type};", kind: :aggregate, tag: split.tag )
180
+ return _expand_and_parse( split.declarators, raw_text ).each { |var| var.defined_type = defined }
194
181
  end
195
182
 
196
- # Detect and split compound declarations (e.g., "int x, y;" => ["int x;", "int y;"])
197
- #
198
- # Splits at depth-0 commas that appear before any initializer (=).
199
- # Preserves pointer specifiers (*) from the base type prefix.
183
+ # The type the declaration defines ahead of its declarators, or nil. The type's keyword
184
+ # follows the declaration's leading run of decorators and attributes.
185
+ def _defined_type(text)
186
+ rest = text.lstrip
187
+ rest = rest.match( LEADING_RUN_ITEM ).post_match while rest.match?( LEADING_RUN_ITEM )
188
+ return CExtractorDefinedType.split( text, text.bytesize - rest.bytesize )
189
+ end
190
+
191
+ def _expand_and_parse(text, raw_text)
192
+ pieces = expand_compound(text)
193
+ return [parse_declaration(text, raw_text).tap { |var| var.name = var.type = var.declarator = nil }] if pieces.nil?
194
+
195
+ pieces.map { |individual| parse_declaration(individual, raw_text) }
196
+ end
197
+
198
+ # Splits a compound declaration into one per variable, as `int x = 0, y;` into `int x = 0;`
199
+ # and `int y;`. A comma outside parentheses, brackets, braces and literals separates two
200
+ # declarators, wherever the initializers sit. Each declarator after the first takes the first
201
+ # one's base type, the text ahead of its declarator. A misread base type means a macro after
202
+ # the first name was read as the name, so the statement stays whole (see _misread_base?).
200
203
  #
201
204
  # Parameters:
202
- # raw_text: Complete declaration string including trailing semicolon
205
+ # text: A complete declaration with its comments blanked, its trailing semicolon included
203
206
  #
204
- # Returns: Array<String> of individual declaration strings
205
- def expand_compound(raw_text)
206
- # Determine the text before any initializer to look for commas
207
- pre_init = raw_text.split('=', 2).first || raw_text
208
-
209
- # Combined depth across (), [], {} for comma-splitting — not suitable for collect_balanced()
210
- depth = 0
211
- comma_positions = []
212
- pre_init.each_char.with_index do |ch, i|
213
- case ch
214
- when '(', '[', '{' then depth += 1
215
- when ')', ']', '}' then depth -= 1
216
- when ',' then comma_positions << i if depth == 0
217
- end
218
- end
207
+ # Returns: Array<String> of individual declaration strings, or nil for a compound declaration
208
+ # whose first name can't be read, which leaves its base type unknown
209
+ def expand_compound(text)
210
+ first, *rest = _top_level_pieces( text.rstrip.chomp( ';' ).strip )
211
+ return [text] if rest.empty?
212
+
213
+ head = CExtractorDeclarator::AsmLabel.without( CExtractorDeclarator.without_initializer( first ) ).gsub( /\s+/, ' ' )
214
+ base_type, name = _base_type( head )
215
+ return nil if base_type.nil?
216
+ return [text] if _misread_base?( base_type, head, name )
217
+
218
+ return ["#{first};"] + rest.map { |declarator| "#{base_type} #{declarator};" }
219
+ end
219
220
 
220
- return [raw_text] if comma_positions.empty?
221
-
222
- # Extract the base type prefix (everything before the first declarator)
223
- # The prefix is everything up to the first declarator token after the last type keyword
224
- first_comma = comma_positions.first
225
- prefix_with_first = raw_text[0...first_comma].strip
226
-
227
- # Determine the type prefix: everything before the last identifier/pointer declarator
228
- # Split the pre-comma text to identify the base type
229
- base_type = extract_base_type_prefix(prefix_with_first)
230
-
231
- # Collect all declarators: split by depth-0 commas, then handle the last one (before ;)
232
- # Build the full raw text without the semicolon
233
- text_body = raw_text.rstrip.chomp(';').strip
234
-
235
- declarators = []
236
- depth = 0
237
- current = ''
238
- text_body.each_char do |ch|
239
- case ch
240
- when '(', '[', '{' then depth += 1; current << ch
241
- when ')', ']', '}' then depth -= 1; current << ch
242
- when ','
243
- if depth == 0
244
- declarators << current.strip
245
- current = ''
246
- else
247
- current << ch
248
- end
249
- else
250
- current << ch
251
- end
252
- end
253
- declarators << current.strip unless current.strip.empty?
254
-
255
- # Reconstruct individual declarations
256
- declarators.map do |declarator|
257
- # Strip leading type tokens from subsequent declarators (they already have the type from base_type)
258
- # The first declarator already includes the full type; subsequent ones just have the name/pointer
259
- if declarator == declarators.first
260
- "#{declarator};"
261
- else
262
- # Subsequent declarators: prepend base type (without trailing pointer specifiers)
263
- clean_base = base_type.gsub(/\*+\s*$/, '').strip
264
- # Preserve pointer specifiers on the declarator name itself
265
- "#{clean_base} #{declarator};"
266
- end
221
+ # The text's pieces between commas outside parentheses, brackets, braces and literals
222
+ def _top_level_pieces(body)
223
+ scanner = StringScanner.new( body )
224
+ depth = 0
225
+ cuts = [0]
226
+ while scanner.skip_until( SPLIT_TOKEN )
227
+ depth += CExtractorDeclarator::DEPTH_CHANGE.fetch( scanner.matched, 0 )
228
+ cuts << scanner.pos if scanner.matched == ',' and depth.zero?
267
229
  end
230
+
231
+ return _between( body, cuts << (body.bytesize + 1) )
232
+ end
233
+
234
+ # The text between each pair of cuts, stripped and less the comma that ends each piece. An
235
+ # empty piece is dropped.
236
+ def _between(body, cuts)
237
+ return cuts.each_cons( 2 ).map { |from, to| body.byteslice( from, to - from - 1 ).strip }.reject( &:empty? )
238
+ end
239
+
240
+ # The first declarator's base type and name. The base type is nil when the name can't be read.
241
+ # The head is that declarator without its initializer and asm label, its spaces collapsed. A
242
+ # star and the qualifiers after it belong to the declarator, as does an attribute after the
243
+ # name, so the base type ends at the first star outside brackets or else at the name. The name
244
+ # is read as the declaration's own record reads it.
245
+ def _base_type(head)
246
+ grouped = CExtractorDeclarator.parenthesized( head )
247
+ return [grouped.base_type, grouped.name] if grouped
248
+
249
+ name = extract_name( @code_text.strip_compiler_extensions( head ) )
250
+ start = name && _declarator_start( head, name )
251
+ return [start && head.byteslice( 0, start ).strip, name]
252
+ end
253
+
254
+ # Whether the base type shows that a macro after the first name, such as an attribute, was
255
+ # read as the name. A word ahead of a star is never the name, so a base type ending at a star
256
+ # stands. An array size outside parentheses follows a name, though a C23 attribute's brackets
257
+ # are no size. A word after a type follows one
258
+ # too when the name read is spelled as a macro is, in capitals or with a leading `__`, as in
259
+ # `const u8 MAX_A UNUSED = 3, MAX_B = 1;` or `int a1 __maybe_unused, b1;`. A keyword or a
260
+ # compiler's `__` extension there is part of the type, as in `int __far a, b;`. Any other word
261
+ # is taken for a qualifier, as in `int MYQ a, b;` or `unsigned char xdata a, b;`. Without
262
+ # expanding the macros, the two readings can't be told apart.
263
+ def _misread_base?(base_type, head, name)
264
+ return false if head.byteslice( base_type.bytesize, head.bytesize ).lstrip.start_with?( '*' )
265
+ bare = @code_text.strip_c23_attributes( base_type )
266
+ return true unless _without_array_sizes( bare ) == bare
267
+ return false unless _type_then_name?( base_type.match( LAST_TWO_WORDS ) )
268
+
269
+ return (name.to_s.match?( MACRO_CASE ) or name.to_s.start_with?( '__' ))
270
+ end
271
+
272
+ # Whether a base type's last two words may be a type and a name. The first is no storage
273
+ # class, qualifier, keyword ahead of a tag or `__` word, and the second no type keyword or
274
+ # `__` word. A macro in capitals ahead of a lowercase word qualifies a type, as `__IO` and
275
+ # `MYQ` do in `MYQ u32_t TICK_A, TICK_B;`.
276
+ def _type_then_name?(words)
277
+ return false if words.nil? or _qualifier?( words[:type] ) or _qualifier?( words[:word] ) or TYPE_WORDS.include?( words[:word] )
278
+
279
+ return !(words[:type].match?( MACRO_CASE ) and !words[:word].match?( MACRO_CASE ))
280
+ end
281
+
282
+ # Whether a word qualifies a type rather than naming one
283
+ def _qualifier?(word)
284
+ return (NOT_A_TYPE.include?( word ) or word.start_with?( '__' ))
268
285
  end
269
286
 
270
- # Extract the base type prefix from the first declarator in a compound declaration
271
- # e.g., "static int *p" => "static int"
272
- # "const char *s1" => "const char"
273
- # "unsigned long x" => "unsigned long"
274
- def extract_base_type_prefix(first_declarator_text)
275
- # Remove leading/trailing whitespace
276
- text = first_declarator_text.strip
277
-
278
- # Remove pointer specifiers and the identifier at the end
279
- # The identifier is the last word token; pointers are * before it
280
- text_no_ptr = text.gsub(/\*+\s*\w+\s*$/, '').strip
281
- if text_no_ptr.empty? || text_no_ptr == text
282
- # No pointer -- remove just the trailing identifier
283
- text.gsub(/\s*\w+\s*$/, '').strip
284
- else
285
- text_no_ptr
287
+ # Where a declarator starts in the text ahead of any initializer. That is its first star or
288
+ # its name, whichever comes first, outside brackets. The name is its last appearance there, as
289
+ # a tag may share it, as in `struct count count`.
290
+ def _declarator_start(head, name)
291
+ scanner = StringScanner.new( head )
292
+ depth = 0
293
+ while scanner.skip_until( DECLARATOR_TOKEN )
294
+ token = scanner.matched
295
+ depth += CExtractorDeclarator::DEPTH_CHANGE.fetch( token, 0 )
296
+ next unless depth.zero?
297
+
298
+ star ||= scanner.pos - 1 if token == '*'
299
+ at = scanner.pos - name.bytesize if token == name
286
300
  end
301
+
302
+ return [star, at].compact.min
287
303
  end
288
304
 
289
305
  # Parse a single (non-compound) declaration string into a CVariableDeclaration struct
@@ -295,24 +311,38 @@ class CExtractorDeclarations
295
311
  # Returns: CVariableDeclaration
296
312
  def parse_declaration(individual_text, original_text)
297
313
  decorators, remaining = extract_decorators(individual_text)
298
- clean_text = normalize_declaration_whitespace(remaining)
299
- # Strip compiler extensions for name/type/array_suffix extraction only;
300
- # clean_text (stored as .text) retains attributes for correct compilation.
301
- extraction_text = @code_text.strip_compiler_extensions(clean_text)
302
- name = extract_name(extraction_text)
303
- type = extract_type(extraction_text, name)
304
- array_suffix = extract_array_suffix(extraction_text)
305
-
306
- CVariableDeclaration.new(
307
- original: original_text,
308
- name: name,
309
- type: type,
310
- array_suffix: array_suffix,
311
- decorators: decorators,
312
- text: clean_text
314
+ clean_text = normalize_declaration_whitespace(remaining)
315
+
316
+ return CVariableDeclaration.new(
317
+ original: original_text, decorators: decorators, text: clean_text,
318
+ asm_label: CExtractorDeclarator::AsmLabel.of( clean_text ),
319
+ **_declared( CExtractorDeclarator::AsmLabel.without( clean_text ) )
313
320
  )
314
321
  end
315
322
 
323
+ # The name, type, array size and declarator of a declaration without its decorators and asm
324
+ # label. They are read with compiler extensions stripped. The record's text keeps the
325
+ # extensions and the label, so the definition compiles as the module's does.
326
+ def _declared(unlabeled)
327
+ extraction_text = @code_text.strip_compiler_extensions( unlabeled )
328
+ # A function pointer or a pointer to an array names its variable inside parentheses
329
+ grouped = CExtractorDeclarator.parenthesized( CExtractorDeclarator.without_initializer( extraction_text ) )
330
+ return { name: grouped.name, type: grouped.base_type, array_suffix: '', declarator: written_declarator( unlabeled, grouped ) } if grouped
331
+
332
+ name = extract_name( extraction_text )
333
+ return { name: name, type: extract_type( extraction_text, name ), array_suffix: extract_array_suffix( extraction_text ) }
334
+ end
335
+
336
+ # The declarator as written. An extern repeats it, so a calling convention or attribute among
337
+ # its stars must stay to agree with the definition. Stripping extensions removes them. The
338
+ # written text stands only when it opens the same group, which the text ahead of each group
339
+ # shows once extensions are stripped. Otherwise the stripped declarator stands.
340
+ def written_declarator(clean_text, grouped)
341
+ written = CExtractorDeclarator.parenthesized( CExtractorDeclarator.without_initializer( clean_text ) )
342
+ same = (written and @code_text.strip_compiler_extensions( written.base_type ) == grouped.base_type)
343
+ return same ? written.declarator : grouped.declarator
344
+ end
345
+
316
346
  # Storage-class specifiers and base-type qualifiers precede a C declaration's type as a
317
347
  # contiguous run of keywords -- `static`, `extern`, and a `const`/`volatile` that qualifies
318
348
  # the type itself all appear here (e.g. `static const int MAX;`). A qualifier bound to a
@@ -322,24 +352,20 @@ class CExtractorDeclarations
322
352
  # stopping at the first non-keyword token, naturally separates the two: the same keyword
323
353
  # (e.g. `const`) can appear once here and again later in the declarator without the two
324
354
  # occurrences being confused with each other, since only the leading run is ever consumed.
355
+ # An alignment specifier or attribute may sit in that run, as in `_Alignas(8) static int x;`.
356
+ # It is no decorator, so it keeps its place ahead of the rest of the text.
325
357
  #
326
358
  # Returns: [Array<String> ordered decorator keywords found at start of text, String remaining text after that run]
327
359
  def extract_decorators(text)
328
360
  decorators = []
329
- remaining = text.strip
330
- loop do
331
- matched = false
332
- DECORATOR_KEYWORDS.each do |kw|
333
- if remaining =~ /\A#{Regexp.escape(kw)}\b(.*)/m
334
- decorators << kw
335
- remaining = $1.strip
336
- matched = true
337
- break
338
- end
339
- end
340
- break unless matched
361
+ kept = []
362
+ remaining = text.strip
363
+ while (found = remaining.match( LEADING_RUN_ITEM ))
364
+ found[:keyword] ? decorators << found[:keyword] : kept << found[0].strip
365
+ remaining = found.post_match
341
366
  end
342
- [decorators, remaining]
367
+
368
+ return [decorators, [*kept, remaining].join( ' ' )]
343
369
  end
344
370
 
345
371
  # A declaration with its leading decorator run already removed (see extract_decorators)
@@ -362,29 +388,47 @@ class CExtractorDeclarations
362
388
  #
363
389
  # Returns: String or nil
364
390
  def extract_name(clean_text)
365
- # Function pointers: void (*callback)(int);
366
- if clean_text =~ /\(\s*\*(\w+)\s*\)/
367
- return $1
391
+ # Strip the trailing semicolon and any initializer. Only an '=' outside brackets starts one,
392
+ # so an array size such as `[N == 2 ? 2 : 3]` stays.
393
+ text = CExtractorDeclarator.without_initializer( clean_text )
394
+ # Strip array subscripts
395
+ text = _without_array_sizes( text )
396
+ # Strip trailing whitespace and return the last identifier
397
+ text.strip[LAST_IDENTIFIER, 1]
398
+ end
399
+
400
+ # The text without its array sizes. A bracket inside parentheses belongs to a type a specifier
401
+ # spells, as in `typeof(int[3])`, so it stays.
402
+ def _without_array_sizes(text)
403
+ scanner = StringScanner.new( text )
404
+ kept = +''
405
+ parens = 0
406
+ while (before = scanner.scan_until( SIZE_TOKEN ))
407
+ parens += PAREN_CHANGE.fetch( scanner.matched, 0 )
408
+ next kept << before unless scanner.matched == '[' and parens.zero?
409
+
410
+ kept << (_skip_size( scanner ) ? before.byteslice( 0, before.bytesize - 1 ) : before)
368
411
  end
369
412
 
370
- # Strip only the trailing semicolon (not inner semicolons in e.g. struct bodies)
371
- text = clean_text.sub(/\s*;\s*$/, '')
372
- # Strip initializer (from first = onward)
373
- text = text.sub(/\s*=.*/, '')
374
- # Strip array subscripts
375
- text = text.gsub(/\[.*?\]/, '')
376
- # Strip trailing whitespace and return last word token
377
- text.strip =~ /(\w+)\s*$/ ? $1 : nil
413
+ return kept << scanner.rest
414
+ end
415
+
416
+ # Moves the scanner, just past an array size's '[', past its matching ']'. When none follows,
417
+ # the scanner stays, and the '[' is text.
418
+ def _skip_size(scanner)
419
+ after = scanner.pos
420
+ scanner.pos = after - 1
421
+ return true if CExtractorDeclarator._skip_balanced( scanner )
422
+
423
+ scanner.pos = after
424
+ return false
378
425
  end
379
426
 
380
427
  # Extract array subscript suffix from a clean (decorator/extension-stripped) declaration.
381
428
  # Returns the complete subscript string (e.g., "[8]", "[M][N]") or "" for scalars.
382
429
  def extract_array_suffix(clean_text)
383
- text = clean_text.sub(/\s*;\s*$/, '').sub(/\s*=.*/, '')
384
- # `match?` leaves $~ alone, which is safe because the line below sets it
385
- # with its own match before reading $1.
386
- return '' if text.match?(/\(\s*\*/) # function pointer -- no array suffix
387
- text =~ /\w+(\s*(?:\[[^\]]*\])+)\s*$/ ? $1.strip : ''
430
+ text = CExtractorDeclarator.without_initializer( clean_text )
431
+ text =~ ARRAY_SUFFIX ? $1.strip : ''
388
432
  end
389
433
 
390
434
  # Extract the type from a clean (decorator-stripped) declaration
@@ -393,23 +437,15 @@ class CExtractorDeclarations
393
437
  def extract_type(clean_text, name)
394
438
  return nil if name.nil?
395
439
 
396
- # Find last occurrence of name in the text (before ; or [)
397
- # Strip only trailing semicolon to avoid matching inner semicolons (e.g., in struct bodies)
398
- text = clean_text.sub(/\s*;\s*$/, '').sub(/\s*=.*/, '').gsub(/\[.*?\]/, '').strip
399
-
400
- # For function pointers like "void (*callback)(int)", type is everything before (*name)
401
- if text =~ /^(.*?)\(\s*\*#{Regexp.escape(name)}\s*\)/
402
- return $1.strip
403
- end
440
+ # Find last occurrence of name in the text, once its semicolon, initializer and array
441
+ # subscripts are gone
442
+ text = _without_array_sizes( CExtractorDeclarator.without_initializer( clean_text ) ).strip
404
443
 
405
- # Find the name in the text and take everything before it (including pointer specifiers).
406
- # A plain rindex(name) would find the last substring occurrence of `name` anywhere in
407
- # text, even inside an unrelated longer identifier that merely ends with it (e.g. `name`
408
- # "count" inside a type-side identifier like "recount") -- \b anchors this to a whole
409
- # identifier occurrence. Greedy `.*` in a single match (not scan) naturally finds the
410
- # rightmost such occurrence, since it consumes as much as possible before backtracking
411
- # just enough to let the trailing \bname\b still match.
412
- match = text.match(/\A(.*)\b#{Regexp.escape(name)}\b/m)
444
+ # Find the name in the text and take everything before it, pointer specifiers included. The
445
+ # name must be a whole identifier, so `count` isn't found inside `recount` or `count$2`.
446
+ # Greedy `.*` in a single match finds its rightmost such occurrence, since it consumes as
447
+ # much as it can before backtracking just enough for the name to match.
448
+ match = text.match(/\A(.*)(?<![\w$])#{Regexp.escape(name)}(?![\w$])/m)
413
449
  return nil if match.nil?
414
450
 
415
451
  type_part = match[1]