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 | 69x 69x 64x 64x 64x 45x 19x 13x 6x 5x 5x 60x 60x 69x 69x 69x 60x 561x 561x 561x 561x 561x 561x 561x 561x 561x 561x 561x 561x 561x 561x 60x 561x 379x 561x | /**
* VariableCollector - Extracts variable declarations from parse trees.
* Handles types, const modifier, arrays, and initial values.
*/
import * as Parser from "../../../parser/grammar/CNextParser";
import ESourceLanguage from "../../../../../utils/types/ESourceLanguage";
import ESymbolKind from "../../../../../utils/types/ESymbolKind";
import IVariableSymbol from "../../types/IVariableSymbol";
import ArrayInitializerUtils from "../utils/ArrayInitializerUtils";
import TypeUtils from "../utils/TypeUtils";
class VariableCollector {
/**
* Resolve a single array dimension to a number or string.
* Returns undefined if the dimension cannot be resolved.
*/
private static resolveDimension(
dim: Parser.ArrayDimensionContext,
constValues: Map<string, number> | undefined,
initExpr: Parser.ExpressionContext | null,
): number | string | undefined {
const sizeExpr = dim.expression();
if (sizeExpr) {
const dimText = sizeExpr.getText();
// Try parsing as literal number first
const literalSize = Number.parseInt(dimText, 10);
if (!Number.isNaN(literalSize)) {
return literalSize;
}
// Issue #455: Resolve constant reference to its value
if (constValues?.has(dimText)) {
return constValues.get(dimText)!;
}
// Issue #455: Store original text for unresolved dimensions
// This handles C macros from included headers (e.g., DEVICE_COUNT)
return dimText;
}
// Issue #636: Empty dimension [] - infer size from array initializer
Eif (initExpr) {
return ArrayInitializerUtils.getInferredSize(initExpr);
}
return undefined;
}
/**
* Collect array dimensions from a variable declaration.
*/
private static collectArrayDimensions(
arrayDims: Parser.ArrayDimensionContext[],
constValues: Map<string, number> | undefined,
initExpr: Parser.ExpressionContext | null,
): (number | string)[] {
const dimensions: (number | string)[] = [];
for (const dim of arrayDims) {
const resolved = VariableCollector.resolveDimension(
dim,
constValues,
initExpr,
);
Eif (resolved !== undefined) {
dimensions.push(resolved);
}
}
return dimensions;
}
/**
* Collect a variable declaration and return an IVariableSymbol.
*
* @param ctx The variable declaration context
* @param sourceFile Source file path
* @param scopeName Optional scope name for scoped variables
* @param isPublic Whether this variable is public (default true for top-level)
* @param constValues Map of constant names to their numeric values (for resolving array dimensions)
* @returns The variable symbol
*/
static collect(
ctx: Parser.VariableDeclarationContext,
sourceFile: string,
scopeName?: string,
isPublic: boolean = true,
constValues?: Map<string, number>,
): IVariableSymbol {
const name = ctx.IDENTIFIER().getText();
const fullName = scopeName ? `${scopeName}_${name}` : name;
const line = ctx.start?.line ?? 0;
// Get type
const typeCtx = ctx.type();
const type = TypeUtils.getTypeName(typeCtx, scopeName);
// Check for const modifier
const isConst = ctx.constModifier() !== null;
// Issue #468: Check for atomic modifier
const isAtomic = ctx.atomicModifier() !== null;
// Check for array dimensions
const arrayDims = ctx.arrayDimension();
const isArray = arrayDims.length > 0;
const initExpr = ctx.expression();
const arrayDimensions = isArray
? VariableCollector.collectArrayDimensions(
arrayDims,
constValues,
initExpr,
)
: [];
// Issue #282: Capture initial value for const inlining
const initialValue = initExpr?.getText();
const symbol: IVariableSymbol = {
name: fullName,
parent: scopeName,
sourceFile,
sourceLine: line,
sourceLanguage: ESourceLanguage.CNext,
isExported: isPublic,
kind: ESymbolKind.Variable,
type,
isConst,
isAtomic,
isArray,
};
if (arrayDimensions.length > 0) {
symbol.arrayDimensions = arrayDimensions;
}
if (initialValue !== undefined) {
symbol.initialValue = initialValue;
}
return symbol;
}
}
export default VariableCollector;
|