Skip to content

Fix C++ POU local state variables - #934

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

Fix C++ POU local state variables#934
manux81 wants to merge 1 commit 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 include local PLC state variables in generated structures and mappings.
  • Bug Fixes
    • Runtime initialization guards, such as hasBeenInitialized, are no longer emitted as struct fields or user-visible macro tokens.
    • ST code generation now correctly creates assignments for local state variables.
    • Generated C++ and ST interfaces remain aligned after preprocessing.
  • Tests
    • Added regression coverage for local-variable generation and initialization-guard handling.

@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

C++ POU state handling

Layer / File(s) Summary
State variable classification
src/frontend/utils/cpp/cppPouVariables.ts
Adds shared predicates and filtering for C++ POU state variables. The filter excludes 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 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 for struct members, macros, #undef directives, pointer assignments, and compiler 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 that the setup guard is excluded from generated C++ state.

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

Sequence Diagram(s)

sequenceDiagram
  participant preprocessPous
  participant compilerModule
  participant generateSTCode
  participant generateCBlocksHeader
  participant generateCBlocksCode
  preprocessPous->>compilerModule: provide augmented POU variables
  compilerModule->>generateSTCode: generate state pointer assignments
  compilerModule->>generateCBlocksHeader: provide merged POU metadata
  compilerModule->>generateCBlocksCode: provide merged POU metadata
  generateCBlocksHeader->>generateCBlocksCode: generate per-instance state fields and mappings
Loading

Possibly related PRs

Poem

A rabbit stores PrevSeq in a field,
While hasBeenInitialized stays concealed.
Headers and code now agree,
ST pointers hop carefully,
Tests confirm the state is sealed.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the primary change: generating local state variables for C++ POUs.
Description check ✅ Passed The description explains the problem, changes, issue reference, and validation; the missing checklist and Jira link are non-critical.
Linked Issues check ✅ Passed The changes satisfy issue #933 by generating local variables as per-instance state in structs, macros, and ST bridge assignments.
Out of Scope Changes check ✅ Passed All code changes support local C++ POU state generation, metadata alignment, or 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

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.

@manux81
manux81 force-pushed the fix/cpp-pou-local-state branch from dc11b2c to d233b6c Compare August 11, 2026 21:52
@coderabbitai

coderabbitai Bot commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

@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: 2

🤖 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 82-98: Update getCppPousForGeneration to use the inferred type
from projectData.pous.find directly, removing the unchecked cast around
processedPou. Delete the now-unused PLCVariable import while preserving the
existing variables fallback behavior.
- Around line 82-98: Replace the type assertion in getCppPousForGeneration with
an explicit type guard that validates the matched POU has a data object before
accessing data.variables. Preserve the existing fallback to cppPou.variables
when no valid processed POU or variables are available.
🪄 Autofix

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 Plus

Run ID: 595e073a-ac65-4cba-8b6d-195e94471c85

📥 Commits

Reviewing files that changed from the base of the PR and between 32e46b8 and d233b6c.

📒 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 (9)
  • src/frontend/utils/cpp/generateSTCode.ts
  • src/backend/shared/utils/cpp/tests/generateCBlocksHeader.test.ts
  • src/backend/shared/utils/PLC/preprocess-pous.ts
  • src/backend/shared/utils/cpp/generateCBlocksHeader.ts
  • src/frontend/utils/cpp/tests/generateSTCode.test.ts
  • src/backend/shared/utils/cpp/generateCBlocksCode.ts
  • src/backend/shared/utils/PLC/tests/preprocess-pous.test.ts
  • src/backend/shared/utils/cpp/tests/generateCBlocksCode.test.ts
  • src/frontend/utils/cpp/cppPouVariables.ts

Comment on lines +82 to +98
/**
* Keep generated C++ metadata aligned with the POU interfaces used to build
* the ST bridge. The sidecar carries the original C++ source, while the
* preprocessed POU is the authoritative source for its current variables.
*/
const getCppPousForGeneration = (projectData: ProjectDataWithCppPous): CppPouDataCode[] => {
return (projectData.originalCppPous ?? []).map((cppPou) => {
const processedPou = projectData.pous.find((pou) => pou.data.name === cppPou.name) as
| { data?: { variables?: PLCVariable[] } }
| undefined
return {
...cppPou,
variables: processedPou?.data?.variables ?? cppPou.variables,
}
})
}

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.

📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail
rg -n -C 5 'PLCProjectData|originalCppPous|toIpcProjectData|pou\.data\.variables' src

Repository: Autonomy-Logic/openplc-editor

Length of output: 50386


🏁 Script executed:

#!/bin/bash
set -euo pipefail

printf '%s\n' '--- compiler module ---'
sed -n '1,120p' src/backend/editor/compiler/compiler-module.ts
printf '%s\n' '--- shared type declarations ---'
rg -n -C 6 'export (type|interface) (PLCProjectData|PLCPou|PLCVariable)|type (PLCProjectData|PLCPou|PLCVariable)' \
  src/backend/shared/types/PLC src/middleware/shared/ports/types.ts
printf '%s\n' '--- preprocess and metadata flow ---'
rg -n -C 8 'function preprocessPous|const preprocessPous|preprocessPous\(|originalCppPous|getCppPousForGeneration|generateCBlocks(Header|Code)' \
  src/backend src/middleware src/main

Repository: Autonomy-Logic/openplc-editor

Length of output: 50386


🏁 Script executed:

#!/bin/bash
set -euo pipefail

printf '%s\n' '--- imports and exact helper context ---'
sed -n '116,145p' src/backend/editor/compiler/compiler-module.ts
sed -n '82,100p' src/backend/editor/compiler/compiler-module.ts

printf '%s\n' '--- POU schema definitions ---'
sed -n '80,175p' src/backend/shared/types/PLC/open-plc.ts
sed -n '210,275p' src/backend/shared/types/PLC/open-plc.ts
sed -n '820,865p' src/backend/shared/types/PLC/open-plc.ts

printf '%s\n' '--- C++ sidecar types and generator contracts ---'
sed -n '1,180p' src/backend/shared/utils/cpp/generateCBlocksCode.ts
sed -n '1,140p' src/backend/shared/utils/cpp/generateCBlocksHeader.ts

printf '%s\n' '--- helper call sites ---'
rg -n -C 4 'getCppPousForGeneration' src/backend/editor/compiler/compiler-module.ts

Repository: Autonomy-Logic/openplc-editor

Length of output: 18915


🏁 Script executed:

#!/bin/bash
set -euo pipefail

python3 - <<'PY'
from pathlib import Path
import re

compiler = Path("src/backend/editor/compiler/compiler-module.ts").read_text()
schema = Path("src/backend/shared/types/PLC/open-plc.ts").read_text()

assert "projectData.pous.find((pou) => pou.data.name === cppPou.name) as" in compiler
assert re.search(
    r"import type \{ PlatformOption, PLCVariable \} from .*ports/types",
    compiler,
)
assert len(re.findall(r"\bPLCVariable\b", compiler)) == 1, "PLCVariable has other uses"

pou_schema = schema[schema.index("const PLCPouSchema"):schema.index("type PLCPou =")]
variants = re.findall(
    r"z\.object\(\{\s*type: z\.literal\('[^']+'\),\s*data: (PLC\w+Schema)",
    pou_schema,
)
assert variants == ["PLCProgramSchema", "PLCFunctionSchema", "PLCFunctionBlockSchema"]

for name in variants:
    match = re.search(
        rf"const {name} = z\.object\(\{{(.*?)\n\}}\)",
        schema,
        re.S,
    )
    assert match and re.search(r"\bvariables:\s*z\.array\(PLCVariableSchema\)", match.group(1)), name

print("The assertion is present.")
print("PLCVariable is imported only for the assertion.")
print("Every PLCPou variant exposes data.variables as PLCVariable[].")
print("The inferred backend POU type is sufficient for the proposed assertion removal.")
PY

Repository: Autonomy-Logic/openplc-editor

Length of output: 284


🏁 Script executed:

#!/bin/bash
set -euo pipefail

rg -n '\bPLCVariable\b' src/backend/editor/compiler/compiler-module.ts

Repository: Autonomy-Logic/openplc-editor

Length of output: 313


Remove the unchecked POU type assertion.

PLCProjectData already exposes pou.data.variables as PLCVariable[] for every POU variant. Remove the assertion and the unused PLCVariable import.

Proposed fix
-import type { PlatformOption, PLCVariable } from '../../../middleware/shared/ports/types'
+import type { PlatformOption } from '../../../middleware/shared/ports/types'

-    const processedPou = projectData.pous.find((pou) => pou.data.name === cppPou.name) as
-      | { data?: { variables?: PLCVariable[] } }
-      | undefined
+    const processedPou = projectData.pous.find((pou) => pou.data.name === cppPou.name)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
/**
* Keep generated C++ metadata aligned with the POU interfaces used to build
* the ST bridge. The sidecar carries the original C++ source, while the
* preprocessed POU is the authoritative source for its current variables.
*/
const getCppPousForGeneration = (projectData: ProjectDataWithCppPous): CppPouDataCode[] => {
return (projectData.originalCppPous ?? []).map((cppPou) => {
const processedPou = projectData.pous.find((pou) => pou.data.name === cppPou.name) as
| { data?: { variables?: PLCVariable[] } }
| undefined
return {
...cppPou,
variables: processedPou?.data?.variables ?? cppPou.variables,
}
})
}
/**
* Keep generated C++ metadata aligned with the POU interfaces used to build
* the ST bridge. The sidecar carries the original C++ source, while the
* preprocessed POU is the authoritative source for its current variables.
*/
const getCppPousForGeneration = (projectData: ProjectDataWithCppPous): CppPouDataCode[] => {
return (projectData.originalCppPous ?? []).map((cppPou) => {
const processedPou = projectData.pous.find((pou) => pou.data.name === cppPou.name)
return {
...cppPou,
variables: processedPou?.data?.variables ?? cppPou.variables,
}
})
}
🧰 Tools
🪛 ast-grep (0.45.1)

[warning] Importing child_process exposes a command-execution surface; ensure any command/argument built from input is validated, and prefer execFile/spawn with an argument array over exec.
Context: import { spawn } from 'node:child_process'
Note: [CWE-78] Improper Neutralization of Special Elements used in an OS Command ('OS Command Injection').

(detect-child-process-typescript)

🤖 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 82 - 98, Update
getCppPousForGeneration to use the inferred type from projectData.pous.find
directly, removing the unchecked cast around processedPou. Delete the now-unused
PLCVariable import while preserving the existing variables fallback behavior.

Source: Coding guidelines


🗄️ Data Integrity & Integration | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail
rg -n -C 8 'originalCppPous|preprocessPous|getCppPousForGeneration|handleGenerateCBlocks(Header|Code)|compileForDebugger|toIpcProjectData' src

Repository: Autonomy-Logic/openplc-editor

Length of output: 50386


🏁 Script executed:

#!/bin/bash
set -euo pipefail

printf '%s\n' '--- compiler-module symbols ---'
rg -n -C 10 'getCppPousForGeneration|compileProgram|compileForDebugger|handleGenerateCBlocksHeader|handleGenerateCBlocksCode|originalCppPous|projectData\.pous' src/backend/editor/compiler/compiler-module.ts

printf '%s\n' '--- preprocessing and conversion ---'
sed -n '1,210p' src/backend/shared/utils/PLC/preprocess-pous.ts
sed -n '1,90p' src/middleware/adapters/editor/compiler-adapter.ts
sed -n '160,325p' src/middleware/adapters/editor/compiler-adapter.ts

printf '%s\n' '--- compiler module entry-point declarations and call sites ---'
rg -n -C 12 'compileProgram|compileForDebugger|handleGenerateCBlocks(Header|Code)' src/backend/editor/compiler/compiler-module.ts src/main/modules/ipc/main.ts

Repository: Autonomy-Logic/openplc-editor

Length of output: 50385


🏁 Script executed:

#!/bin/bash
set -euo pipefail

printf '%s\n' '--- compileProgram data flow ---'
sed -n '2408,2575p' src/backend/editor/compiler/compiler-module.ts
sed -n '2575,2924p' src/backend/editor/compiler/compiler-module.ts

printf '%s\n' '--- compileForDebugger data flow ---'
sed -n '2924,3122p' src/backend/editor/compiler/compiler-module.ts

printf '%s\n' '--- exact adapter conversion and preprocess call sites ---'
sed -n '176,215p' src/middleware/adapters/editor/compiler-adapter.ts
sed -n '286,320p' src/middleware/adapters/editor/compiler-adapter.ts

printf '%s\n' '--- pipeline C++ consumers ---'
rg -n -C 6 'buildCBlocksFromPous|handleGenerateCBlocks(Header|Code)|originalCppPous|runCompilePipeline' src/backend/shared/compile src/backend/editor/compiler/compiler-module.ts

printf '%s\n' '--- type and shape declarations ---'
rg -n -C 5 'interface .*Pou|type .*Pou|PLCProjectData|IpcProjectData|CppPouDataCode|PLCVariable' src/middleware/shared/ports/types.ts src/backend/editor/compiler/compiler-module.ts

Repository: Autonomy-Logic/openplc-editor

Length of output: 50386


🏁 Script executed:

#!/bin/bash
set -euo pipefail

python3 - <<'PY'
from pathlib import Path

adapter = Path("src/middleware/adapters/editor/compiler-adapter.ts").read_text()
preprocess = Path("src/backend/shared/utils/PLC/preprocess-pous.ts").read_text()
compiler = Path("src/backend/editor/compiler/compiler-module.ts").read_text()
pipeline = Path("src/backend/shared/compile/pipeline.ts").read_text()

def require(text, needle, label):
    if needle not in text:
        raise AssertionError(f"missing {label}: {needle}")

def ordered(text, needles, label):
    positions = [text.index(needle) for needle in needles]
    if positions != sorted(positions):
        raise AssertionError(f"wrong order in {label}: {positions}")

# Build and debugger both preprocess, then convert the processed result.
require(adapter, "const { projectData: processedData, validationFailed } = preprocessPous(", "build preprocessing")
require(adapter, "const ipcData = toIpcProjectData(processedData)", "build IPC conversion")
require(adapter, "async compileForDebug(", "debug entry point")
debug = adapter[adapter.index("async compileForDebug("):]
require(debug, "const { projectData: processedData, validationFailed } = preprocessPous(", "debug preprocessing")
require(debug, "const ipcData = toIpcProjectData(processedData)", "debug IPC conversion")

# The converter forwards the sidecar when it exists.
require(adapter, "...(data.originalCppPous ? { originalCppPous:", "sidecar forwarding")

# C++ preprocessing records variables after local-variable augmentation and
# replaces the C++ POU body only after the sidecar is assembled.
ordered(
    preprocess,
    [
        "processedProjectData = addCppLocalVariables(processedProjectData)",
        "const originalCppPousData = processedProjectData.pous",
        "processedProjectData.pous = processedProjectData.pous.map",
        "projectDataWithCpp.originalCppPous = originalCppPousData",
    ],
    "C++ preprocessing",
)

# compileProgram passes the received projectData to the shared pipeline.
require(compiler, "const result = await runCompilePipeline(", "compileProgram pipeline call")
compile_call = compiler[compiler.index("const result = await runCompilePipeline("):]
require(compile_call, "projectData,", "compileProgram projectData handoff")

# The debugger uses the sidecar and generated header/code helpers.
debug_backend = compiler[compiler.index("async compileForDebugger("):]
for needle in (
    "originalCppPous?.length",
    "await this.handleGenerateCBlocksHeader(projectData",
    "await this.handleGenerateCBlocksCode(projectData",
):
    require(debug_backend, needle, f"debug backend consumer {needle}")

# The shared pipeline consumes the sidecar for C-block generation.
require(pipeline, "const originalCppPous = processedData.originalCppPous ?? []", "pipeline sidecar read")
require(pipeline, "buildCBlocksFromPous(originalCppPous as never)", "pipeline C-block generation")

print("metadata flow checks passed")
PY

Repository: Autonomy-Logic/openplc-editor

Length of output: 354


🏁 Script executed:

#!/bin/bash
set -euo pipefail

python3 - <<'PY'
from pathlib import Path

adapter = Path("src/middleware/adapters/editor/compiler-adapter.ts").read_text()
preprocess = Path("src/backend/shared/utils/PLC/preprocess-pous.ts").read_text()
compiler = Path("src/backend/editor/compiler/compiler-module.ts").read_text()
pipeline = Path("src/backend/shared/compile/pipeline.ts").read_text()

def require(text, needle, label):
    if needle not in text:
        raise AssertionError(f"missing {label}: {needle}")

def ordered(text, needles, label):
    positions = [text.index(needle) for needle in needles]
    if positions != sorted(positions):
        raise AssertionError(f"wrong order in {label}: {positions}")

build = adapter[:adapter.index("async compileForDebug(")]
debug = adapter[adapter.index("async compileForDebug("):]
require(build, "const { projectData: processedData, validationFailed } = preprocessPous(", "build preprocessing")
require(build, "const ipcData = toIpcProjectData(processedData)", "build IPC conversion")
require(debug, "const { projectData: processedData, validationFailed } = preprocessPous(", "debug preprocessing")
require(debug, "const ipcData = toIpcProjectData(processedData)", "debug IPC conversion")
require(adapter, "...(data.originalCppPous ? { originalCppPous:", "sidecar forwarding")

cpp_start = preprocess.index("// --- C++ processing ---")
cpp_end = preprocess.index("// --- SoftMotion:", cpp_start)
cpp = preprocess[cpp_start:cpp_end]
ordered(
    cpp,
    [
        "processedProjectData = addCppLocalVariables(processedProjectData)",
        "const originalCppPousData = processedProjectData.pous",
        "processedProjectData.pous = processedProjectData.pous.map",
        "projectDataWithCpp.originalCppPous = originalCppPousData",
    ],
    "C++ preprocessing",
)

compile_start = compiler.index("const result = await runCompilePipeline(")
compile_call = compiler[compile_start:]
require(compile_call, "projectData,", "compileProgram projectData handoff")

debug_backend = compiler[compiler.index("async compileForDebugger("):]
for needle in (
    "originalCppPous?.length",
    "await this.handleGenerateCBlocksHeader(projectData",
    "await this.handleGenerateCBlocksCode(projectData",
):
    require(debug_backend, needle, f"debug backend consumer {needle}")

require(pipeline, "const originalCppPous = processedData.originalCppPous ?? []", "pipeline sidecar read")
require(pipeline, "buildCBlocksFromPous(originalCppPous as never)", "pipeline C-block generation")

print("metadata flow checks passed")
PY

Repository: Autonomy-Logic/openplc-editor

Length of output: 195


Replace the type assertion with explicit narrowing. The assertion violates the TypeScript guidelines and bypasses validation of the IPC POU shape. Use a type guard before reading data.variables.

🧰 Tools
🪛 ast-grep (0.45.1)

[warning] Importing child_process exposes a command-execution surface; ensure any command/argument built from input is validated, and prefer execFile/spawn with an argument array over exec.
Context: import { spawn } from 'node:child_process'
Note: [CWE-78] Improper Neutralization of Special Elements used in an OS Command ('OS Command Injection').

(detect-child-process-typescript)

🤖 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 82 - 98, Replace
the type assertion in getCppPousForGeneration with an explicit type guard that
validates the matched POU has a data object before accessing data.variables.
Preserve the existing fallback to cppPou.variables when no valid processed POU
or variables are available.

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