-
Notifications
You must be signed in to change notification settings - Fork 91
Fix C++ POU local state variables #934
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: development
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -79,6 +79,23 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| originalCppPous?: CppPouDataCode[] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * 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, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+82
to
+98
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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' srcRepository: 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/mainRepository: 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.tsRepository: 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.")
PYRepository: 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.tsRepository: Autonomy-Logic/openplc-editor Length of output: 313 Remove the unchecked POU type assertion.
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
Suggested change
🧰 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. (detect-child-process-typescript) 🤖 Prompt for AI AgentsSource: 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' srcRepository: 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.tsRepository: 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.tsRepository: 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")
PYRepository: 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")
PYRepository: 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 🧰 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. (detect-child-process-typescript) 🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Post-build PLC start retry loop bounds. Why these numbers: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * - 5000 ms total: longer than the slowest STOP transition observed | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -118,7 +135,7 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import type { MessagePortMain } from 'electron/main' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import JSZip from 'jszip' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import type { PlatformOption } from '../../../middleware/shared/ports/types' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import type { PlatformOption, PLCVariable } from '../../../middleware/shared/ports/types' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { BoardInfoResolver } from '../../shared/hardware/board-info-resolver' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { formatPackageIntegrityError, PackageManagerModule } from '../package-manager' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { CreateXMLFile } from '../utils' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -491,8 +508,8 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| checkStrucppAvailability(): MethodsResult<string> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { getVersion } = loadStrucpp() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return { success: true, data: getVersion() } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Check warning on line 512 in src/backend/editor/compiler/compiler-module.ts
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new Error('STruC++ not available. Run "npm run setup:binaries" to install it.') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -1242,7 +1259,7 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| sourceTargetFolderPath: string, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| handleOutputData: HandleOutputDataCallback, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const originalCppPous = projectData.originalCppPous || [] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const originalCppPous = getCppPousForGeneration(projectData) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (originalCppPous.length === 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| handleOutputData('No C/C++ blocks found, skipping c_blocks.h generation', 'info') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -1275,7 +1292,7 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _boardRuntime: string, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| handleOutputData: HandleOutputDataCallback, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const originalCppPous = projectData.originalCppPous || [] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const originalCppPous = getCppPousForGeneration(projectData) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (originalCppPous.length === 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| handleOutputData('No C/C++ blocks found, skipping c_blocks_code.cpp generation', 'info') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -3037,7 +3054,7 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _mainProcessPort.postMessage({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| logLevel, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| message: data, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ...(compileError ? { compileError } : {}), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { hasCBlocks, pous: knownPous, libraries, missingLibraries }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.