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 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 | 13x 899x 834x 834x 766x 68x 68x 63x 63x 5x 63x 63x 14x 49x 6x 43x 43x 43x 7x 36x 36x 9x 27x 27x 27x 27x 25x 25x 1x 26x 26x 1x 14x 14x 14x 4x 4x 4x 10x 10x 10x 10x 14x 1x 9x 9x 9x 2x 7x 7x 7x 7x 2x 2x 1x 1x 1x 1x 7x 7x 6x 6x 6x 1x 6x 7x 7x 5x 2x 1x 1x 1x 1x 1x 8x 8x 8x 8x 1x 7x 7x 1x 6x 6x 1x 5x 5x 5x 5x 9x 1x 8x 8x 8x 6x 1x 7x 1x 6x 6x 6x 6x 5x 1x 4x 1x 3x 3x 1x 2x 2x 2x 2x 2x | /**
* StringDeclHelper - Generates string variable declarations
*
* Issue #644: Extracted from CodeGenerator to reduce file size.
*
* Handles all string-related declaration patterns:
* - Bounded strings: string<64> name
* - String arrays: string<64> arr[4]
* - String concatenation: string<64> result <- str1 + str2
* - Substring extraction: string<64> sub <- str.substring(0, 5)
* - Unsized const strings: const string name <- "literal"
*/
import * as Parser from "../../../logic/parser/grammar/CNextParser.js";
import FormatUtils from "../../../../utils/FormatUtils.js";
import TTypeInfo from "../types/TTypeInfo.js";
/** C null terminator character literal for generated code */
const C_NULL_CHAR = String.raw`'\0'`;
/**
* String concatenation operands extracted from expression.
*/
interface IStringConcatOps {
left: string;
right: string;
leftCapacity: number;
rightCapacity: number;
}
/**
* Substring extraction operands extracted from expression.
*/
interface ISubstringOps {
source: string;
start: string;
length: string;
sourceCapacity: number;
}
/**
* Array initialization tracking state.
*/
interface IArrayInitState {
lastArrayInitCount: number;
lastArrayFillValue: string | undefined;
}
/**
* Dependencies required for string declaration generation.
*/
interface IStringDeclHelperDeps {
/** Type registry for looking up variable types */
typeRegistry: Map<string, TTypeInfo>;
/** Get whether currently inside a function body */
getInFunctionBody: () => boolean;
/** Get current indentation level */
getIndentLevel: () => number;
/** Array initialization tracking */
arrayInitState: IArrayInitState;
/** Local arrays set */
localArrays: Set<string>;
/** Generate expression code */
generateExpression: (ctx: Parser.ExpressionContext) => string;
/** Generate array dimensions */
generateArrayDimensions: (dims: Parser.ArrayDimensionContext[]) => string;
/** Get string concatenation operands */
getStringConcatOperands: (
ctx: Parser.ExpressionContext,
) => IStringConcatOps | null;
/** Get substring extraction operands */
getSubstringOperands: (ctx: Parser.ExpressionContext) => ISubstringOps | null;
/** Get string literal length */
getStringLiteralLength: (literal: string) => number;
/** Get string expression capacity */
getStringExprCapacity: (exprCode: string) => number | null;
/** Request string include */
requireStringInclude: () => void;
}
/**
* Declaration modifiers for string variable declarations.
*/
interface IStringDeclModifiers {
extern: string;
const: string;
atomic: string;
volatile: string;
}
/**
* Result from generating a string declaration.
*/
interface IStringDeclResult {
/** The generated C code */
code: string;
/** Whether the declaration was handled (false = not a string type) */
handled: boolean;
}
/**
* Generates string variable declarations in C.
*/
class StringDeclHelper {
private readonly deps: IStringDeclHelperDeps;
constructor(deps: IStringDeclHelperDeps) {
this.deps = deps;
}
/**
* Generate string declaration if the type is a string type.
* Returns { handled: false } if not a string type.
*/
generateStringDecl(
typeCtx: Parser.TypeContext,
name: string,
expression: Parser.ExpressionContext | null,
arrayDims: Parser.ArrayDimensionContext[],
modifiers: IStringDeclModifiers,
isConst: boolean,
): IStringDeclResult {
const stringCtx = typeCtx.stringType();
if (!stringCtx) {
return { code: "", handled: false };
}
const intLiteral = stringCtx.INTEGER_LITERAL();
if (intLiteral) {
// Bounded string with explicit capacity
const capacity = Number.parseInt(intLiteral.getText(), 10);
return this.generateBoundedStringDecl(
name,
capacity,
expression,
arrayDims,
modifiers,
isConst,
);
} else {
// Unsized string - requires const and literal
return this.generateUnsizedStringDecl(
name,
expression,
modifiers,
isConst,
);
}
}
/**
* Generate bounded string declaration (string<N>).
*/
private generateBoundedStringDecl(
name: string,
capacity: number,
expression: Parser.ExpressionContext | null,
arrayDims: Parser.ArrayDimensionContext[],
modifiers: IStringDeclModifiers,
isConst: boolean,
): IStringDeclResult {
const { extern, const: constMod } = modifiers;
// String arrays: string<64> arr[4] -> char arr[4][65] = {0};
if (arrayDims.length > 0) {
return this.generateStringArrayDecl(
name,
capacity,
expression,
arrayDims,
modifiers,
isConst,
);
}
// Simple bounded string without initializer
if (!expression) {
return {
code: `${extern}${constMod}char ${name}[${capacity + 1}] = "";`,
handled: true,
};
}
return this._generateBoundedStringWithInit(
name,
capacity,
expression,
extern,
constMod,
);
}
/**
* Generate bounded string with initializer expression
*/
private _generateBoundedStringWithInit(
name: string,
capacity: number,
expression: Parser.ExpressionContext,
extern: string,
constMod: string,
): IStringDeclResult {
// Check for string concatenation
const concatOps = this.deps.getStringConcatOperands(expression);
if (concatOps) {
return this.generateConcatDecl(name, capacity, concatOps, constMod);
}
// Check for substring extraction
const substringOps = this.deps.getSubstringOperands(expression);
if (substringOps) {
return this.generateSubstringDecl(name, capacity, substringOps, constMod);
}
// Validate and generate simple assignment
this._validateStringInit(expression.getText(), capacity);
const code = `${extern}${constMod}char ${name}[${capacity + 1}] = ${this.deps.generateExpression(expression)};`;
return { code, handled: true };
}
/**
* Validate string initialization (literal length and variable capacity)
*/
private _validateStringInit(exprText: string, capacity: number): void {
// Validate string literal fits capacity
if (exprText.startsWith('"') && exprText.endsWith('"')) {
const content = this.deps.getStringLiteralLength(exprText);
if (content > capacity) {
throw new Error(
`Error: String literal (${content} chars) exceeds string<${capacity}> capacity`,
);
}
}
// Check for string variable assignment
const srcCapacity = this.deps.getStringExprCapacity(exprText);
if (srcCapacity !== null && srcCapacity > capacity) {
throw new Error(
`Error: Cannot assign string<${srcCapacity}> to string<${capacity}> (potential truncation)`,
);
}
}
/**
* Generate string array declaration.
*/
private generateStringArrayDecl(
name: string,
capacity: number,
expression: Parser.ExpressionContext | null,
arrayDims: Parser.ArrayDimensionContext[],
modifiers: IStringDeclModifiers,
isConst: boolean,
): IStringDeclResult {
const {
extern,
const: constMod,
atomic,
volatile: volatileMod,
} = modifiers;
let decl = `${extern}${constMod}${atomic}${volatileMod}char ${name}`;
// No initializer - zero-initialize
if (!expression) {
decl += this.deps.generateArrayDimensions(arrayDims);
decl += `[${capacity + 1}]`;
return { code: `${decl} = {0};`, handled: true };
}
// Reset array init tracking and generate initializer
this.deps.arrayInitState.lastArrayInitCount = 0;
this.deps.arrayInitState.lastArrayFillValue = undefined;
const initValue = this.deps.generateExpression(expression);
// Check if it was an array initializer
const isArrayInit =
this.deps.arrayInitState.lastArrayInitCount > 0 ||
this.deps.arrayInitState.lastArrayFillValue !== undefined;
if (!isArrayInit) {
throw new Error(
`Error: String array initialization from variables not supported`,
);
}
// Track as local array
this.deps.localArrays.add(name);
const hasEmptyArrayDim = arrayDims.some((dim) => !dim.expression());
if (hasEmptyArrayDim) {
decl += this._handleSizeInference(name, capacity, isConst);
} else {
decl += this._handleExplicitSize(arrayDims);
}
decl += `[${capacity + 1}]`; // String capacity + null terminator
const finalInitValue = this._expandFillAllIfNeeded(initValue, arrayDims);
return { code: `${decl} = ${finalInitValue};`, handled: true };
}
/**
* Handle size inference for empty array dimension.
* Returns the dimension string to append to declaration.
*/
private _handleSizeInference(
name: string,
capacity: number,
isConst: boolean,
): string {
const fillValue = this.deps.arrayInitState.lastArrayFillValue;
if (fillValue !== undefined) {
throw new Error(
`Error: Fill-all syntax [${fillValue}*] requires explicit array size`,
);
}
const arraySize = this.deps.arrayInitState.lastArrayInitCount;
// Update type registry with inferred size
this.deps.typeRegistry.set(name, {
baseType: "char",
bitWidth: 8,
isArray: true,
arrayDimensions: [arraySize, capacity + 1],
isConst,
isString: true,
stringCapacity: capacity,
});
return `[${arraySize}]`;
}
/**
* Handle explicit array size with validation.
* Returns the dimension string to append to declaration.
*/
private _handleExplicitSize(
arrayDims: Parser.ArrayDimensionContext[],
): string {
const declaredSize = this._getFirstDimNumericSize(arrayDims);
// Validate element count matches declared size (only for non-fill-all)
if (declaredSize !== null) {
const isFillAll =
this.deps.arrayInitState.lastArrayFillValue !== undefined;
const elementCount = this.deps.arrayInitState.lastArrayInitCount;
if (!isFillAll && elementCount !== declaredSize) {
throw new Error(
`Error: Array size mismatch - declared [${declaredSize}] but got ${elementCount} elements`,
);
}
}
return this.deps.generateArrayDimensions(arrayDims);
}
/**
* Expand fill-all syntax if needed.
* ["Hello"*] with size 3 -> {"Hello", "Hello", "Hello"}
*/
private _expandFillAllIfNeeded(
initValue: string,
arrayDims: Parser.ArrayDimensionContext[],
): string {
const fillVal = this.deps.arrayInitState.lastArrayFillValue;
if (fillVal === undefined) {
return initValue;
}
// Empty string fill doesn't need expansion (C handles {""} correctly)
if (fillVal === '""') {
return initValue;
}
const declaredSize = this._getFirstDimNumericSize(arrayDims);
Iif (declaredSize === null) {
return initValue;
}
const elements = new Array<string>(declaredSize).fill(fillVal);
return `{${elements.join(", ")}}`;
}
/**
* Get the numeric size from the first array dimension, or null if not numeric.
*/
private _getFirstDimNumericSize(
arrayDims: Parser.ArrayDimensionContext[],
): number | null {
const firstDimExpr = arrayDims[0]?.expression();
Iif (!firstDimExpr) {
return null;
}
const sizeText = firstDimExpr.getText();
if (!/^\d+$/.exec(sizeText)) {
return null;
}
return Number.parseInt(sizeText, 10);
}
/**
* Generate string concatenation declaration.
*/
private generateConcatDecl(
name: string,
capacity: number,
concatOps: IStringConcatOps,
constMod: string,
): IStringDeclResult {
// String concatenation requires runtime function calls (strncpy, strncat)
// which cannot exist at global scope in C
if (!this.deps.getInFunctionBody()) {
throw new Error(
`Error: String concatenation cannot be used at global scope. ` +
`Move the declaration inside a function.`,
);
}
// Validate capacity: dest >= left + right
const requiredCapacity = concatOps.leftCapacity + concatOps.rightCapacity;
if (requiredCapacity > capacity) {
throw new Error(
`Error: String concatenation requires capacity ${requiredCapacity}, but string<${capacity}> only has ${capacity}`,
);
}
// Generate safe concatenation code
const indent = FormatUtils.indent(this.deps.getIndentLevel());
const lines: string[] = [];
lines.push(
`${constMod}char ${name}[${capacity + 1}] = "";`,
`${indent}strncpy(${name}, ${concatOps.left}, ${capacity});`,
`${indent}strncat(${name}, ${concatOps.right}, ${capacity} - strlen(${name}));`,
`${indent}${name}[${capacity}] = ${C_NULL_CHAR};`,
);
return { code: lines.join("\n"), handled: true };
}
/**
* Generate substring extraction declaration.
*/
private generateSubstringDecl(
name: string,
capacity: number,
substringOps: ISubstringOps,
constMod: string,
): IStringDeclResult {
// Substring extraction requires runtime function calls (strncpy)
// which cannot exist at global scope in C
if (!this.deps.getInFunctionBody()) {
throw new Error(
`Error: Substring extraction cannot be used at global scope. ` +
`Move the declaration inside a function.`,
);
}
// For compile-time validation, we need numeric literals
const startNum = Number.parseInt(substringOps.start, 10);
const lengthNum = Number.parseInt(substringOps.length, 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) {
throw new Error(
`Error: Substring bounds [${startNum}, ${lengthNum}] exceed source string<${substringOps.sourceCapacity}> capacity`,
);
}
}
// Validate destination capacity can hold the substring
if (!Number.isNaN(lengthNum) && lengthNum > capacity) {
throw new Error(
`Error: Substring length ${lengthNum} exceeds destination string<${capacity}> capacity`,
);
}
// Generate safe substring extraction code
const indent = FormatUtils.indent(this.deps.getIndentLevel());
const lines: string[] = [];
lines.push(
`${constMod}char ${name}[${capacity + 1}] = "";`,
`${indent}strncpy(${name}, ${substringOps.source} + ${substringOps.start}, ${substringOps.length});`,
`${indent}${name}[${substringOps.length}] = ${C_NULL_CHAR};`,
);
return { code: lines.join("\n"), handled: true };
}
/**
* Generate unsized const string declaration.
*/
private generateUnsizedStringDecl(
name: string,
expression: Parser.ExpressionContext | null,
modifiers: IStringDeclModifiers,
isConst: boolean,
): IStringDeclResult {
if (!isConst) {
throw new Error(
"Error: Non-const string requires explicit capacity, e.g., string<64>",
);
}
if (!expression) {
throw new Error(
"Error: const string requires initializer for capacity inference",
);
}
const exprText = expression.getText();
if (!exprText.startsWith('"') || !exprText.endsWith('"')) {
throw new Error(
"Error: const string requires string literal for capacity inference",
);
}
// Infer capacity from literal length
const inferredCapacity = this.deps.getStringLiteralLength(exprText);
this.deps.requireStringInclude();
// Register in type registry with inferred capacity
this.deps.typeRegistry.set(name, {
baseType: "char",
bitWidth: 8,
isArray: true,
arrayDimensions: [inferredCapacity + 1],
isConst: true,
isString: true,
stringCapacity: inferredCapacity,
});
const code = `${modifiers.extern}const char ${name}[${inferredCapacity + 1}] = ${exprText};`;
return { code, handled: true };
}
}
export default StringDeclHelper;
|