Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 | 91x 14x 67x 10x 14x 14x 14x 7x 7x 7x 7x 1x 6x 5x 5x 5x 1x 5x 5x 5x 5x 5x 2x 3x 1x 2x 1x 1x 1x 67x 67x 67x 11x 56x 9x 47x 47x 11x 36x 36x 30x 4x 1x 3x 3x 3x 3x 36x 31x 31x 1x 30x 5x 5x 1x 4x 9x 1x 8x 8x 1x 7x 7x 7x 11x 1x 10x 10x 10x 8x 1x 9x 1x 8x 8x 8x 10x 1x 9x 1x 8x 1x 7x 7x 7x 7x 7x | /**
* StringDeclHelper - Generates string variable declarations
*
* Issue #644: Extracted from CodeGenerator to reduce file size.
* Migrated to use CodeGenState instead of constructor DI.
*
* Handles all string-related declaration patterns (ADR-045):
* - Bounded strings: string<64> name
* - String arrays: string<64>[4] items
* - String concatenation: string<64> result <- str1 + str2
* - Substring extraction: string<64> sub <- str[0, 5]
* - Unsized const strings: const string name <- "literal"
*
* ## It renders a plan; it does not read a tree (#1445 box 3)
*
* Every question this file used to ask of a parse node -- which of the three
* string forms is this, what capacity does it declare, is the initializer a
* concatenation -- is answered by `VariableDeclHelper._planStringDecl` and
* arrives as `TPlannedStringDecl`. What is left here is the part that is
* genuinely rendering: the NUL-terminated `[capacity + 1]` convention, the
* bounded copy sequences, and the capacity diagnostics.
*
* The plan is built at the call site rather than in `CodeGenerator`, where
* every other planner on this branch sits. That placement once carried an
* ordering contract with a per-file type registry the render walk filled as
* it went; #1668 (C8) deleted the registry. A name binds through the lexical
* frames 1.4 settled before rendering, so `string<32> s <- s + "x"` reads
* `s`'s own declaration -- detected as a concatenation and rejected with
* E0864 for "capacity 33" -- wherever the plan is built. (That the name
* resolves at all is a separate defect, filed as #1643; this comment records
* why the placement cannot be changed while it holds.)
*/
import IPlannedStringInit from "../types/IPlannedStringInit";
import VariableModifierBuilder from "./VariableModifierBuilder";
import IRenderedModifiers from "../types/IRenderedModifiers";
import IStringConcatOps from "../types/IStringConcatOps";
import ISubstringOps from "../types/ISubstringOps";
import TPlannedStringDecl from "../types/TPlannedStringDecl";
import StringUtils from "../../../../utils/StringUtils";
import invariant from "../../../../utils/invariant";
import type TranspileState from "../../../TranspileState";
/**
* Generates string variable declarations in C.
*/
class StringDeclHelper {
/**
* Generate the declaration a string plan describes.
*
* @param name - the EMITTED identifier (ADR-057), not the source spelling
* @param isConst - whether the declaration carries `const`, which only the
* unsized form consults: it is the difference between an inferred capacity
* and E0862
*/
static generateStringDecl(
plan: TPlannedStringDecl,
name: string,
modifiers: IRenderedModifiers,
isConst: boolean,
state: TranspileState,
): string {
switch (plan.kind) {
case "array":
return StringDeclHelper._generateStringArray(
plan,
name,
modifiers,
state,
);
case "bounded":
return StringDeclHelper._generateBoundedStringDecl(
plan.capacity,
plan.init,
name,
modifiers,
state,
);
case "unsized":
return StringDeclHelper._generateUnsizedStringDecl(
plan.initText,
plan.declaredCapacity,
name,
modifiers,
isConst,
);
}
}
/**
* Issue #1029: Generate string array declaration.
* Handles: string<32>[4] items -> char items[4][33] = {0};
*/
private static _generateStringArray(
plan: Extract<TPlannedStringDecl, { kind: "array" }>,
name: string,
modifiers: IRenderedModifiers,
state: TranspileState,
): string {
const {
extern,
const: constMod,
atomic,
volatile: volatileMod,
} = modifiers;
const decl = `${extern}${constMod}${atomic}${volatileMod}char ${name}${plan.dimensions}[${plan.elementCapacity + 1}]`;
// No initializer - zero-initialize
if (!plan.renderInit) {
return `${decl} = {0};`;
}
// The array-initializer bookkeeping is written BY the render below and read
// immediately after, so the reset, the render and the reads are one window.
state.resetArrayInitTracking();
const initValue = plan.renderInit();
// Check if it was an array initializer
if (!state.wasArrayInit()) {
invariant(
false,
`a string array is initialized from literals -- E0866 rejects a variable initializer in pass 2.1`,
);
}
// Validate element count if declared size is available
if (plan.declaredSize !== null) {
const isFillAll = state.lastArrayFillValue !== undefined;
const elementCount = state.lastArrayInitCount;
if (!isFillAll && elementCount !== plan.declaredSize) {
invariant(
false,
`a string array initializer matches its declared size -- E0866 rejects [${plan.declaredSize}] against ${elementCount} element(s) in pass 2.1`,
);
}
}
// Handle fill-all expansion if needed
const finalInitValue = StringDeclHelper._expandFillAll(
initValue,
plan.declaredSize,
state,
);
// MISRA C:2012 Rules 9.3/9.4 - String literals don't fill all inner array bytes,
// but C standard guarantees zero-initialization of remaining elements
const suppression =
"// cppcheck-suppress misra-c2012-9.3\n// cppcheck-suppress misra-c2012-9.4\n";
return `${suppression}${decl} = ${finalInitValue};`;
}
/**
* Expand fill-all syntax (`{= value}`) to one element per declared slot.
*/
private static _expandFillAll(
initValue: string,
declaredSize: number | null,
state: TranspileState,
): string {
const fillVal = state.lastArrayFillValue;
if (fillVal === undefined) {
return initValue;
}
// Empty string fill doesn't need expansion (C handles {""} correctly)
if (fillVal === '""') {
return initValue;
}
if (declaredSize === null) {
return initValue;
}
const elements = new Array<string>(declaredSize).fill(fillVal);
return `{${elements.join(", ")}}`;
}
/**
* Generate bounded string declaration (string<N>).
*/
private static _generateBoundedStringDecl(
capacity: number,
init: IPlannedStringInit | null,
name: string,
modifiers: IRenderedModifiers,
state: TranspileState,
): string {
const {
extern,
const: constMod,
atomic,
volatile: volatileMod,
} = modifiers;
// #1164 / #1642: `atomic`/`volatile` belong on EVERY bounded arm. They were
// dropped on the no-initializer arm first and on the with-initializer arms
// second, each time producing a definition that conflicted with its own
// header while the transpiler exited 0.
const qualifiers = `${atomic}${volatileMod}`;
// Simple bounded string without initializer
if (!init) {
return `${extern}${constMod}${qualifiers}char ${name}[${capacity + 1}] = "";`;
}
// ADR-045 asks the four initializer forms in a fixed order, and the order is
// load-bearing: `renderSubstring` generates the index expressions when it
// answers, so it is asked only after `concat` has declined.
if (init.concat) {
return StringDeclHelper._generateConcatDecl(
name,
capacity,
init.concat,
constMod,
qualifiers,
state,
);
}
const substringOps = init.renderSubstring();
if (substringOps) {
return StringDeclHelper._generateSubstringDecl(
name,
capacity,
substringOps,
constMod,
qualifiers,
state,
);
}
// Validate and check if it's a literal or variable
const isLiteral = StringDeclHelper._validateStringInit(
init.text,
init.sourceCapacity,
capacity,
);
if (isLiteral) {
// String literal: can use direct initialization
return `${extern}${constMod}${qualifiers}char ${name}[${capacity + 1}] = ${init.render()};`;
}
// String variable: cannot use C array initialization, so declare empty and
// copy. Issue #1044: use the same bounded copy as the reassignment path
// (strncpy + explicit null terminator via StringUtils.copyWithNull) rather
// than an unbounded strcpy, which flawfinder flags as CWE-120.
// Issue #1030: string-to-string initialization
if (!state.inFunctionBody) {
invariant(
false,
`a string at file scope is initialized by a literal -- E0863 rejects a copy from a variable in pass 2.1`,
);
}
const srcExpr = init.render();
// Issue #1037: continuation lines carry no indent of their own — the block
// emitter (CodeGenerator.generateBlock) prefixes every line.
const lines: string[] = [];
lines.push(
`${constMod}${qualifiers}char ${name}[${capacity + 1}] = "";`,
StringUtils.copyWithNull(name, srcExpr, capacity),
);
return lines.join("\n");
}
/**
* Validate string initialization (literal length and variable capacity)
* Returns true if the expression is a string literal, false if it's a variable.
*/
private static _validateStringInit(
exprText: string,
sourceCapacity: number | null,
capacity: number,
): boolean {
// Validate string literal fits capacity
if (exprText.startsWith('"') && exprText.endsWith('"')) {
const content = StringUtils.literalLength(exprText);
if (content > capacity) {
invariant(
false,
`a string literal fits its declared capacity -- E0864 rejects ${content} chars in string<${capacity}> in pass 2.1`,
);
}
return true; // Is a literal
}
// A string variable's capacity, as the plan bound it (#1668)
const srcCapacity = sourceCapacity;
if (srcCapacity !== null && srcCapacity > capacity) {
invariant(
false,
`a string source fits its destination -- E0864 rejects string<${srcCapacity}> into string<${capacity}> in pass 2.1`,
);
}
return false; // Is a variable (not a literal)
}
/**
* Generate string concatenation declaration.
*/
private static _generateConcatDecl(
name: string,
capacity: number,
concatOps: IStringConcatOps,
constMod: string,
qualifiers: string,
state: TranspileState,
): string {
// String concatenation requires runtime function calls (strncpy, strncat)
// which cannot exist at global scope in C
if (!state.inFunctionBody) {
invariant(
false,
`a string at file scope is initialized by a literal -- E0863 rejects a concatenation in pass 2.1`,
);
}
// Validate capacity: dest >= left + right
const requiredCapacity = concatOps.leftCapacity + concatOps.rightCapacity;
if (requiredCapacity > capacity) {
invariant(
false,
`a concatenation fits its destination -- E0864 rejects ${requiredCapacity} into string<${capacity}> in pass 2.1`,
);
}
// Generate safe concatenation code. Issue #1037: continuation lines carry
// no indent of their own — the block emitter prefixes every line.
// The copy sequence itself is owned by StringUtils.concat -- including the
// MISRA 17.7 `(void)` casts (ADR-070 Case 1). Rebuilding it here would be a
// second path that has to be kept in step by hand.
const lines: string[] = [];
lines.push(
`${constMod}${qualifiers}char ${name}[${capacity + 1}] = "";`,
...StringUtils.concat(name, concatOps.left, concatOps.right, capacity),
);
return lines.join("\n");
}
/**
* Generate substring extraction declaration.
*/
private static _generateSubstringDecl(
name: string,
capacity: number,
substringOps: ISubstringOps,
constMod: string,
qualifiers: string,
state: TranspileState,
): string {
// Substring extraction requires runtime function calls (strncpy)
// which cannot exist at global scope in C
if (!state.inFunctionBody) {
invariant(
false,
`a string at file scope is initialized by a literal -- E0863 rejects a substring in pass 2.1`,
);
}
// For compile-time validation, we need numeric literals
const startNum = Number.parseInt(substringOps.start, 10);
const lengthNum = Number.parseInt(substringOps.lengthExpression, 10);
// Only validate bounds if both start and length are compile-time constants
if (!Number.isNaN(startNum) && !Number.isNaN(lengthNum)) {
// Bounds check: start + length <= sourceCapacity
if (startNum + lengthNum > substringOps.sourceCapacity) {
invariant(
false,
`substring bounds stay within the source -- E0865 rejects [${startNum}, ${lengthNum}] against string<${substringOps.sourceCapacity}> in pass 2.1`,
);
}
}
// Validate destination capacity can hold the substring
if (!Number.isNaN(lengthNum) && lengthNum > capacity) {
invariant(
false,
`a substring fits its destination -- E0864 rejects ${lengthNum} into string<${capacity}> in pass 2.1`,
);
}
// Generate safe substring extraction code. Issue #1037: continuation lines
// carry no indent of their own — the block emitter prefixes every line.
// Extraction sequence owned by StringUtils.substring (see _generateConcatDecl).
const lines: string[] = [];
lines.push(
`${constMod}${qualifiers}char ${name}[${capacity + 1}] = "";`,
...StringUtils.substring(
name,
substringOps.source,
substringOps.start,
substringOps.lengthExpression,
),
);
return lines.join("\n");
}
/**
* Generate unsized const string declaration.
*/
private static _generateUnsizedStringDecl(
initText: string | null,
declaredCapacity: number | null,
name: string,
modifiers: IRenderedModifiers,
isConst: boolean,
): string {
if (!isConst) {
invariant(
false,
"a non-const string states its capacity -- E0862 rejects an unsized one in pass 2.1",
);
}
if (initText === null) {
invariant(
false,
"an unsized const string has an initializer to infer from -- E0862 rejects one without in pass 2.1",
);
}
if (!initText.startsWith('"') || !initText.endsWith('"')) {
invariant(
false,
"an unsized const string infers from a LITERAL -- E0862 rejects any other initializer in pass 2.1",
);
}
// #1664 box 3: the capacity is the declaration's -- 1.3 counted it from
// this literal and the `.h` states it. Counting the literal again here was
// a second derivation; it survives as the check that the two agree.
const literalLength = StringUtils.literalLength(initText);
invariant(
declaredCapacity === literalLength,
`an unsized string's capacity is its declaration's -- 1.3 counted ` +
`${declaredCapacity} for '${name}' but the literal has ${literalLength} character(s)`,
);
// #1642's open box. This arm hand-assembled `${extern}const `, dropping
// `atomic`/`volatile` and hardcoding the `const` rather than reading the
// one the caller resolved -- so the `.h`, which derives the qualifier from
// the SYMBOL, emitted `extern volatile const char x[2];` against this
// file's `const char x[2] = "v";` and the translation unit did not
// compile. It was unreachable until the E0862 predicate beside it started
// asking the grammar instead of the declaration's text, which is why the
// bounded arms were fixed in #1642 and this one was not.
//
// `toPrefix` is the single encoder the bounded arms above spell out by
// hand; using it here is what makes a fifth arm impossible to forget.
//
// The hardcoded `const` was also STATING an invariant -- an unsized string
// is const, which E0862 enforces in 2.1 -- so dropping it silently would
// have traded one masked bug for another. Asserted instead, which is the
// same fact without the mask: if the invariant ever breaks, this says so
// rather than quietly emitting a non-const definition against a `const`
// declaration in the header.
invariant(
modifiers.const !== "",
"an unsized string is const -- E0862 rejects a non-const one in pass 2.1, before this runs",
);
const prefix = VariableModifierBuilder.toPrefix(modifiers);
return `${prefix}char ${name}[${declaredCapacity + 1}] = ${initText};`;
}
}
export default StringDeclHelper;
|