All files / transpiler/logic/analysis ReturnPathAnalyzer.ts

100% Statements 47/47
100% Branches 26/26
100% Functions 9/9
100% Lines 47/47

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                                                91x 91x     46x     45x 45x 9x     36x 36x 3x     33x 33x   1x     32x                 2x     30x 30x 7x     23x 23x 1x         22x               62x               9x 9x 5x   4x                     3x 3x 1x   2x   3x 2x                   178x 178x     234x       234x 186x     48x 40x     8x 8x                       178x     178x 178x 178x 178x       8x                          
/**
 * Return-Path Analyzer
 * ADR-112 / Issue #1040: reject a non-void function that can reach the end of
 * its body without returning a value (undefined behavior in C).
 *
 * The analysis is intentionally strict and conservative (sound): it never
 * accepts a function that might fall through, and it does not attempt to prove
 * that loops are infinite or that a switch over an enum is exhaustive. Where it
 * cannot prove a return, an explicit `return <value>` is required.
 *
 * C-Next has no break/continue (ADR-026), so loop bodies have no early
 * structural exits to reason about.
 */
 
import { ParseTreeWalker } from "antlr4ng";
import { CNextListener } from "../parser/grammar/CNextListener";
import * as Parser from "../parser/grammar/CNextParser";
import IReturnPathError from "./types/IReturnPathError";
 
/**
 * Does executing this statement guarantee that the enclosing function returns a
 * value before control passes beyond it?
 */
function statementDefinitelyReturns(ctx: Parser.StatementContext): boolean {
  const returnStmt = ctx.returnStatement();
  if (returnStmt) {
    // A bare `return;` (no expression) returns no value, so it does not satisfy
    // a non-void function.
    return returnStmt.expression() !== null;
  }
 
  const ifStmt = ctx.ifStatement();
  if (ifStmt) {
    return ifDefinitelyReturns(ifStmt);
  }
 
  const switchStmt = ctx.switchStatement();
  if (switchStmt) {
    return switchDefinitelyReturns(switchStmt);
  }
 
  const doWhileStmt = ctx.doWhileStatement();
  if (doWhileStmt) {
    // The body always executes at least once.
    return blockDefinitelyReturns(doWhileStmt.block());
  }
 
  if (ctx.foreverStatement()) {
    // ADR-113: a `forever` loop is divergent — C-Next has no break/continue
    // (ADR-026), so control never passes beyond it. It is therefore a terminal
    // path, like an unconditional return, and the function never falls through
    // here. This is the shared "divergence" primitive ADR-114 (#849) reuses.
    //
    // `forever` is void-only (E0705, enforced in codegen). Marking it terminal
    // here keeps a non-void function containing a `forever` loop from emitting a
    // misleading E0704 ("must return a value") instead of the precise E0705.
    return true;
  }
 
  const block = ctx.block();
  if (block) {
    return blockDefinitelyReturns(block);
  }
 
  const criticalStmt = ctx.criticalStatement();
  if (criticalStmt) {
    return blockDefinitelyReturns(criticalStmt.block());
  }
 
  // while / for (the body may not execute), variable declarations, assignments,
  // and expression statements never guarantee a return on their own.
  return false;
}
 
/**
 * A block guarantees a return iff any of its statements does: statements after
 * an unconditional return are unreachable.
 */
function blockDefinitelyReturns(ctx: Parser.BlockContext): boolean {
  return ctx.statement().some(statementDefinitelyReturns);
}
 
/**
 * An if guarantees a return only when an `else` is present and both branches
 * guarantee a return. `else if` chains recurse through the else branch.
 */
function ifDefinitelyReturns(ctx: Parser.IfStatementContext): boolean {
  const branches = ctx.statement();
  if (branches.length < 2) {
    return false;
  }
  return (
    statementDefinitelyReturns(branches[0]) &&
    statementDefinitelyReturns(branches[1])
  );
}
 
/**
 * A switch guarantees a return only when a `default` is present and every case
 * block and the default block guarantee a return.
 */
function switchDefinitelyReturns(ctx: Parser.SwitchStatementContext): boolean {
  const defaultCase = ctx.defaultCase();
  if (!defaultCase) {
    return false;
  }
  const everyCaseReturns = ctx
    .switchCase()
    .every((switchCase) => blockDefinitelyReturns(switchCase.block()));
  return everyCaseReturns && blockDefinitelyReturns(defaultCase.block());
}
 
/**
 * Listener that flags non-void functions whose body can fall through.
 */
class ReturnPathListener extends CNextListener {
  private readonly analyzer: ReturnPathAnalyzer;
 
  constructor(analyzer: ReturnPathAnalyzer) {
    super();
    this.analyzer = analyzer;
  }
 
  override enterFunctionDeclaration = (
    ctx: Parser.FunctionDeclarationContext,
  ): void => {
    // Void functions are allowed to fall off the end.
    if (ctx.type().getText() === "void") {
      return;
    }
 
    if (blockDefinitelyReturns(ctx.block())) {
      return;
    }
 
    const identifier = ctx.IDENTIFIER();
    this.analyzer.addError(
      identifier.getText(),
      identifier.symbol.line,
      identifier.symbol.column,
    );
  };
}
 
/**
 * Analyzer for missing return paths (ADR-112).
 */
class ReturnPathAnalyzer {
  private errors: IReturnPathError[] = [];
 
  public analyze(tree: Parser.ProgramContext): IReturnPathError[] {
    this.errors = [];
    const listener = new ReturnPathListener(this);
    ParseTreeWalker.DEFAULT.walk(listener, tree);
    return this.errors;
  }
 
  public addError(functionName: string, line: number, column: number): void {
    this.errors.push({
      code: "E0704",
      functionName,
      line,
      column,
      message: `Non-void function '${functionName}' must return a value on all paths`,
      helpText:
        "Add an explicit 'return <value>;' so every control-flow path returns a value",
    });
  }
}
 
export default ReturnPathAnalyzer;