All files / transpiler/logic/symbols/c/collectors VariableCollector.ts

100% Statements 5/5
87.5% Branches 7/8
100% Functions 2/2
100% Lines 5/5

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                                                            7x               7x 7x   7x                                                                     8x                              
/**
 * VariableCollector - Collects variable symbols from C parse trees.
 */
 
/* eslint-disable @typescript-eslint/no-explicit-any */
 
import type ICVariableSymbol from "../../../../types/symbols/c/ICVariableSymbol";
import ESourceLanguage from "../../../../../utils/types/ESourceLanguage";
import DeclaratorUtils from "../utils/DeclaratorUtils";
 
class VariableCollector {
  /**
   * Collect a variable symbol from a declarator.
   *
   * @param name Variable name
   * @param baseType Variable type
   * @param declarator The declarator context (for array dimensions)
   * @param sourceFile Source file path
   * @param line Source line number
   * @param isExtern Whether the variable is extern
   */
  static collect(
    name: string,
    baseType: string,
    declarator: any,
    sourceFile: string,
    line: number,
    isExtern: boolean,
  ): ICVariableSymbol {
    // Extract array dimensions if present
    const arrayDimensions = declarator
      ? DeclaratorUtils.extractArrayDimensions(declarator)
      : [];
 
    // Issue #978: Detect pointer variables (e.g., `font_t *ptr`).
    // C grammar puts `*` in the declarator, not the type specifier.
    // Same pattern as FunctionCollector._resolveReturnType().
    const hasPointer =
      declarator?.pointer?.() !== null && declarator?.pointer?.() !== undefined;
    const resolvedType = hasPointer ? `${baseType}*` : baseType;
 
    return {
      kind: "variable",
      name,
      sourceFile,
      sourceLine: line,
      sourceLanguage: ESourceLanguage.C,
      isExported: !isExtern,
      type: resolvedType,
      isArray: arrayDimensions.length > 0,
      arrayDimensions: arrayDimensions.length > 0 ? arrayDimensions : undefined,
      isExtern,
    };
  }
 
  /**
   * Collect a variable from declaration specifiers (when identifier appears as typedefName).
   * This handles the C grammar ambiguity where variable names can be parsed as typedef names.
   *
   * Note: No pointer detection here — this path handles declarations without an
   * initDeclaratorList. Pointer declarations (e.g., `font_t *ptr`) always produce
   * an initDeclaratorList (the `*` creates a declarator), so they go through collect().
   *
   * @param name Variable name
   * @param baseType Variable type
   * @param sourceFile Source file path
   * @param line Source line number
   * @param isExtern Whether the variable is extern
   */
  static collectFromDeclSpecs(
    name: string,
    baseType: string,
    sourceFile: string,
    line: number,
    isExtern: boolean,
  ): ICVariableSymbol {
    return {
      kind: "variable",
      name,
      sourceFile,
      sourceLine: line,
      sourceLanguage: ESourceLanguage.C,
      isExported: !isExtern,
      type: baseType,
      isArray: false,
      isExtern,
    };
  }
}
 
export default VariableCollector;