All files / transpiler/logic/analysis FloatModuloAnalyzer.ts

93.33% Statements 42/45
90% Branches 18/20
83.33% Functions 5/6
97.43% Lines 38/39

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                                                                                323x 323x 323x             1336x     1336x 1336x     40x 40x 40x   40x 40x   23x 23x   23x 23x   23x 13x 13x                 46x 46x   44x 44x     44x 44x 19x           25x 25x 22x       22x     3x               323x           323x     323x 323x     323x       323x   323x             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. Build lexical scope frames (DeclarationScopeCollector)
 * 2. Detect modulo operations using float variables or literals
 *
 * Issue #1220: pass 1 used to be a private Set of float variable names built
 * from this file's parse tree alone, so an `f32` arriving through an #include
 * was invisible and `floatValue % 2` compiled to C that gcc then rejects with
 * "invalid operands to binary %". Resolution now goes through
 * ScopeFrameResolver, which searches the lexical frames and falls back to the
 * symbol table -- one cross-file-aware answer shared with the other
 * essential-type analyzers instead of a per-analyzer cache.
 */
 
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";
import DeclarationScopeCollector from "./DeclarationScopeCollector";
import ScopeFrameResolver from "./ScopeFrameResolver";
 
/**
 * 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 scopes: ScopeFrameResolver;
 
  constructor(analyzer: FloatModuloAnalyzer, scopes: ScopeFrameResolver) {
    super();
    this.analyzer = analyzer;
    this.scopes = scopes;
  }
 
  /**
   * 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. Resolved against the
    // lexical frames first, then the symbol table, so an included declaration
    // counts and a same-named local in another function does not (#1220).
    const identifier = primaryExpr.IDENTIFIER();
    if (identifier) {
      const typeName = this.scopes.typeOfName(
        identifier.getText(),
        this.scopes.frameFor(ctx),
      );
      return typeName !== null && TypeConstants.FLOAT_TYPES.includes(typeName);
    }
 
    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: build the lexical scope frames
    const declarations = new DeclarationScopeCollector();
    ParseTreeWalker.DEFAULT.walk(declarations, tree);
 
    // Second pass: detect modulo with floats
    const listener = new FloatModuloListener(
      this,
      new ScopeFrameResolver(declarations),
    );
    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;