Skip to content

Fix C++ POU local state variables#934

Open
manux81 wants to merge 2 commits into
Autonomy-Logic:developmentfrom
manux81:fix/cpp-pou-local-state
Open

Fix C++ POU local state variables#934
manux81 wants to merge 2 commits into
Autonomy-Logic:developmentfrom
manux81:fix/cpp-pou-local-state

Conversation

@manux81

@manux81 manux81 commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Summary

Fix C/C++ POU local variables so they are generated as per-instance state instead of being omitted from the generated C++ bridge.

Previously, only input and output variables were included in the generated *_VARS structs, ST pointer assignments, and C++ macros. Local variables declared on a C/C++ Function Block POU, such as PrevSeq, were referenced by user code but never declared in the generated C++ scope.

Changes

  • Add a shared helper for selecting C++ POU instance-state variables.
  • Include local variables, along with input, output, and inOut, in generated C++ POU structs and macros.
  • Include those same state variables in the generated ST bridge assignments.
  • Keep the generated hasBeenInitialized setup guard internal to the ST stub so it is not exposed as a user C++ macro or struct field.
  • Update generator tests to cover local state variables such as PrevSeq.

Closes #933

Validation

  • npx jest src/frontend/utils/cpp/__tests__/generateSTCode.test.ts src/backend/shared/utils/cpp/__tests__/generateCBlocksCode.test.ts src/backend/shared/utils/cpp/__tests__/generateCBlocksHeader.test.ts src/backend/shared/utils/PLC/__tests__/preprocess-pous.test.ts --runInBand
  • npx eslint src/frontend/utils/cpp/cppPouVariables.ts src/frontend/utils/cpp/generateSTCode.ts src/backend/shared/utils/cpp/generateCBlocksCode.ts src/backend/shared/utils/cpp/generateCBlocksHeader.ts

Summary by CodeRabbit

  • New Features
    • C++ header/code generation and ST bridge logic now consistently derive emitted POU state fields and variable mappings from the unified set of PLC state variables (including locals).
  • Bug Fixes
    • Runtime initialization guards (e.g., the hasBeenInitialized symbol) are no longer emitted as struct fields or user-visible macro tokens.
    • ST code generation correctly creates pointer/member assignments for local state variables instead of filtering them out.
    • Preprocessing keeps C/C++ sidecar variables aligned with the generated ST bridge output.
  • Tests
    • Updated and added regression coverage for local-variable struct emission and guard/macro behavior.

@coderabbitai

coderabbitai Bot commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

C++ POU processing and generation now preserve local variables, emit them as per-instance state, and exclude the runtime-only hasBeenInitialized guard. Tests cover preprocessing metadata, generated C++ headers and code, and ST pointer assignments.

Changes

CPP POU state handling

Layer / File(s) Summary
State variable classification
src/frontend/utils/cpp/cppPouVariables.ts
Adds shared predicates and filtering for CPP POU state variables while excluding hasBeenInitialized.
Preprocessing variable alignment
src/backend/shared/utils/PLC/preprocess-pous.ts, src/backend/shared/utils/PLC/__tests__/preprocess-pous.test.ts
Preserves original C++ source and rebuilds POU metadata from interfaces after local-variable augmentation.
Generator state integration
src/backend/shared/utils/cpp/..., src/frontend/utils/cpp/generateSTCode.ts, src/backend/editor/compiler/compiler-module.ts
Uses the shared state-variable set to generate struct members, macros, undefinitions, pointer assignments, and compiler output metadata.
Local state regression coverage
src/backend/shared/utils/cpp/__tests__/*, src/frontend/utils/cpp/__tests__/generateSTCode.test.ts
Covers local-variable generation and confirms the setup guard is not emitted as user-visible state.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant preprocessPous
  participant cppPouVariables
  participant CppGenerators
  participant Tests
  preprocessPous->>cppPouVariables: augment and classify POU variables
  cppPouVariables->>CppGenerators: provide CPP POU state variables
  CppGenerators->>Tests: emit and validate local struct state and assignments
Loading

Suggested reviewers: dcoutinho1328

Poem

I’m a bunny with state in my burrow tonight,
PrevSeq stays local, tucked safe and tight.
Guards hide softly, macros behave,
Tests hop along the code path they pave.
C++ blooms with fields just right!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title is concise and clearly summarizes the main change: fixing C++ POU local state variables.
Description check ✅ Passed The description covers summary, changes, and validation; only the template's references and DOD checklist are partially incomplete.
Linked Issues check ✅ Passed The changes satisfy #933 by including local POU variables in instance state while keeping hasBeenInitialized internal.
Out of Scope Changes check ✅ Passed The code and tests stay focused on C/C++ POU state-variable generation and related regression coverage.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint install failed. For unrecoverable errors, disable the tool in CodeRabbit configuration.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@manux81
manux81 force-pushed the fix/cpp-pou-local-state branch 3 times, most recently from e87be2b to 0553dc9 Compare July 13, 2026 20:21
@manux81

manux81 commented Jul 13, 2026

Copy link
Copy Markdown
Contributor Author

Updated in commit 0553dc9. The desktop compiler now keeps the generated ST bridge, c_blocks.h, and c_blocks_code.cpp aligned. This fixes the PREVSEQ/PREV_SEND missing-member errors during Arduino pre-compilation.

@manux81
manux81 force-pushed the fix/cpp-pou-local-state branch from 0553dc9 to f97cd28 Compare July 13, 2026 20:26

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/backend/editor/compiler/compiler-module.ts (1)

2805-2806: 🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

Adapt PLCProjectData before calling fromSchemaShape.

PLCProjectData defines configurations, but fromSchemaShape reads data.configuration?.resource. The cast suppresses the mismatch, causing debug-generated ST to omit tasks, instances, and global variables.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/backend/editor/compiler/compiler-module.ts` around lines 2805 - 2806,
Update the compiler flow around fromSchemaShape and runJsonTranspiler to adapt
PLCProjectData’s configurations field into the configuration shape expected by
fromSchemaShape, rather than suppressing the mismatch with a cast. Ensure the
adapted data preserves configuration resources so generated ST includes tasks,
instances, and global variables.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/backend/editor/compiler/compiler-module.ts`:
- Around line 89-94: Update the processed POU lookup in the compiler module to
match `projectData.pous` entries by their top-level `name` field instead of
`pou.data.name`, and read variables from `interface.variables` rather than
`data.variables`. Preserve the fallback to `cppPou.variables` when no matching
variables are available.

---

Outside diff comments:
In `@src/backend/editor/compiler/compiler-module.ts`:
- Around line 2805-2806: Update the compiler flow around fromSchemaShape and
runJsonTranspiler to adapt PLCProjectData’s configurations field into the
configuration shape expected by fromSchemaShape, rather than suppressing the
mismatch with a cast. Ensure the adapted data preserves configuration resources
so generated ST includes tasks, instances, and global variables.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 1902709b-3b82-49c5-9857-ae6e3d52f15b

📥 Commits

Reviewing files that changed from the base of the PR and between 97a7956 and dc11b2c.

📒 Files selected for processing (10)
  • src/backend/editor/compiler/compiler-module.ts
  • src/backend/shared/utils/PLC/__tests__/preprocess-pous.test.ts
  • src/backend/shared/utils/PLC/preprocess-pous.ts
  • src/backend/shared/utils/cpp/__tests__/generateCBlocksCode.test.ts
  • src/backend/shared/utils/cpp/__tests__/generateCBlocksHeader.test.ts
  • src/backend/shared/utils/cpp/generateCBlocksCode.ts
  • src/backend/shared/utils/cpp/generateCBlocksHeader.ts
  • src/frontend/utils/cpp/__tests__/generateSTCode.test.ts
  • src/frontend/utils/cpp/cppPouVariables.ts
  • src/frontend/utils/cpp/generateSTCode.ts
🚧 Files skipped from review as they are similar to previous changes (8)
  • src/backend/shared/utils/cpp/tests/generateCBlocksHeader.test.ts
  • src/backend/shared/utils/cpp/generateCBlocksHeader.ts
  • src/frontend/utils/cpp/generateSTCode.ts
  • src/frontend/utils/cpp/cppPouVariables.ts
  • src/backend/shared/utils/PLC/preprocess-pous.ts
  • src/frontend/utils/cpp/tests/generateSTCode.test.ts
  • src/backend/shared/utils/cpp/generateCBlocksCode.ts
  • src/backend/shared/utils/cpp/tests/generateCBlocksCode.test.ts

Comment thread src/backend/editor/compiler/compiler-module.ts
@manux81

manux81 commented Jul 21, 2026

Copy link
Copy Markdown
Contributor Author

Follow-up on the outside-diff CodeRabbit finding about fromSchemaShape: this is also a false positive for the same IPC-shape reason. createEditorCompilerAdapter.toIpcProjectData converts the port field configurations to the schema field configuration before sending the payload to CompilerModule. The main process therefore passes the exact schema shape expected by fromSchemaShape, preserving tasks, instances, and global variables. No code change is needed for that finding.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

C/C++ Function Block local state variables are not generated

1 participant