All files / transpiler/logic/analysis FloatModuloAnalyzer.ts

91.52% Statements 54/59
83.33% Branches 20/24
90% Functions 9/10
98% Lines 49/50

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                                                167x     167x                   195x   195x 195x   39x   39x           155x     155x           40x 40x                           167x 167x 167x             318x     318x 318x     23x 23x 23x   23x 23x   20x 20x   20x 20x   20x 13x 13x                 40x 40x   38x 38x     38x 38x 19x       19x 19x 18x     1x               167x           167x     167x 167x 167x     167x 167x   167x             13x                                      
/**
 * Float Modulo Analyzer
 * Detects modulo operator usage with floating-point types at compile time
 *
 * The modulo operator (%) is only valid for integer types in C.
 * C-Next catches this early with a clear error message.
 *
 * Two-pass analysis:
 * 1. Collect variable declarations with f32/f64 types
 * 2. Detect modulo operations using float variables or literals
 */
 
import { ParseTreeWalker } from "antlr4ng";
import { CNextListener } from "../parser/grammar/CNextListener";
import * as Parser from "../parser/grammar/CNextParser";
import IFloatModuloError from "./types/IFloatModuloError";
import LiteralUtils from "../../../utils/LiteralUtils";
import ParserUtils from "../../../utils/ParserUtils";
import TypeConstants from "../../../utils/constants/TypeConstants";
 
/**
 * First pass: Collect variable declarations with float types
 */
class FloatVariableCollector extends CNextListener {
  private readonly floatVars: Set<string> = new Set();
 
  public getFloatVars(): Set<string> {
    return this.floatVars;
  }
 
  /**
   * Track a typed identifier if it has a float type
   */
  private trackIfFloat(
    typeCtx: Parser.TypeContext | null,
    identifier: { getText(): string } | null,
  ): void {
    Iif (!typeCtx) return;
 
    const typeName = typeCtx.getText();
    if (!TypeConstants.FLOAT_TYPES.includes(typeName)) return;
 
    Iif (!identifier) return;
 
    this.floatVars.add(identifier.getText());
  }
 
  /**
   * Track variable declarations with f32/f64 types
   */
  override enterVariableDeclaration = (
    ctx: Parser.VariableDeclarationContext,
  ): void => {
    this.trackIfFloat(ctx.type(), ctx.IDENTIFIER());
  };
 
  /**
   * Track function parameters with f32/f64 types
   */
  override enterParameter = (ctx: Parser.ParameterContext): void => {
    this.trackIfFloat(ctx.type(), ctx.IDENTIFIER());
  };
}
 
/**
 * Second pass: Detect modulo operations with float operands
 */
class FloatModuloListener extends CNextListener {
  private readonly analyzer: FloatModuloAnalyzer;
 
  // eslint-disable-next-line @typescript-eslint/lines-between-class-members
  private readonly floatVars: Set<string>;
 
  constructor(analyzer: FloatModuloAnalyzer, floatVars: Set<string>) {
    super();
    this.analyzer = analyzer;
    this.floatVars = floatVars;
  }
 
  /**
   * Check multiplicative expressions for modulo with float operands
   * multiplicativeExpression: unaryExpression (('*' | '/' | '%') unaryExpression)*
   */
  override enterMultiplicativeExpression = (
    ctx: Parser.MultiplicativeExpressionContext,
  ): void => {
    const operands = ctx.unaryExpression();
    if (operands.length < 2) return;
 
    // Check each operator
    for (let i = 0; i < operands.length - 1; i++) {
      const operatorToken = ctx.getChild(i * 2 + 1);
      Iif (!operatorToken) continue;
 
      const operator = operatorToken.getText();
      if (operator !== "%") continue;
 
      const leftOperand = operands[i];
      const rightOperand = operands[i + 1];
 
      const leftIsFloat = this.isFloatOperand(leftOperand);
      const rightIsFloat = this.isFloatOperand(rightOperand);
 
      if (leftIsFloat || rightIsFloat) {
        const { line, column } = ParserUtils.getPosition(leftOperand);
        this.analyzer.addError(line, column);
      }
    }
  };
 
  /**
   * Check if a unary expression is a float type
   */
  private isFloatOperand(ctx: Parser.UnaryExpressionContext): boolean {
    const postfixExpr = ctx.postfixExpression();
    if (!postfixExpr) return false;
 
    const primaryExpr = postfixExpr.primaryExpression();
    Iif (!primaryExpr) return false;
 
    // Check for float literal
    const literal = primaryExpr.literal();
    if (literal) {
      return LiteralUtils.isFloat(literal);
    }
 
    // Check for identifier that's a float variable
    const identifier = primaryExpr.IDENTIFIER();
    if (identifier) {
      return this.floatVars.has(identifier.getText());
    }
 
    return false;
  }
}
 
/**
 * Analyzer that detects modulo operations with floating-point types
 */
class FloatModuloAnalyzer {
  private errors: IFloatModuloError[] = [];
 
  /**
   * Analyze the parse tree for float modulo operations
   */
  public analyze(tree: Parser.ProgramContext): IFloatModuloError[] {
    this.errors = [];
 
    // First pass: collect float variables
    const collector = new FloatVariableCollector();
    ParseTreeWalker.DEFAULT.walk(collector, tree);
    const floatVars = collector.getFloatVars();
 
    // Second pass: detect modulo with floats
    const listener = new FloatModuloListener(this, floatVars);
    ParseTreeWalker.DEFAULT.walk(listener, tree);
 
    return this.errors;
  }
 
  /**
   * Add a float modulo error
   */
  public addError(line: number, column: number): void {
    this.errors.push({
      code: "E0804",
      line,
      column,
      message: "Modulo operator not supported for floating-point types",
      helpText:
        "The % operator only works with integer types. Use fmod() from <math.h> for floating-point remainder.",
    });
  }
 
  /**
   * Get all detected errors
   */
  public getErrors(): IFloatModuloError[] {
    return this.errors;
  }
}
 
export default FloatModuloAnalyzer;