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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 | 13x 13x 198x 198x 198x 211x 161x 161x 161x 9x 50x 50x 50x 1x 230x 230x 230x 40x 190x 230x 230x 230x 230x 35x 35x 35x 35x 397x 397x 397x 397x 397x 189x 189x 66x 66x 397x 185x 212x 4x 208x 189x 189x 110x 40x 40x 24x 16x 16x 70x 66x 66x 66x 99x 40x 40x 4x 36x 12x 24x 199x 199x 199x 199x 199x 199x 199x 199x 199x 198x 198x 198x 198x 198x 198x 198x 198x 198x 198x 198x 198x 198x 198x 198x 198x 198x 198x 312x 35x 35x 198x 39x 39x 39x 39x 31x 31x 15x 198x 312x 190x 190x 198x 312x 190x 190x 312x 35x 35x 35x 48x 40x 40x 208x 311x 262x 262x 20x 199x 198x 312x 312x 190x 190x 122x 122x 35x 190x 190x 190x 190x 35x 35x 48x 35x 48x 48x 40x 40x 40x 40x 238x 223x 231x 231x 94x 94x 137x 137x 57x 57x 80x 80x 3x 4x 3x 77x 77x 1x 1x 76x 76x 3x 3x 73x 73x 73x 73x 73x 73x 73x 73x 8x 94x 94x 7x 7x 7x 7x 7x 94x 7x 10x 10x 10x 10x 1x 1x 9x 9x 9x 1x 8x 8x 8x 57x 57x 57x 57x 55x 57x 3x 6x 6x 6x 3x 3x 3x 6x 3x 57x 57x 57x 57x 57x 57x 2x 55x 57x 57x 67x 12x 12x 55x 55x 55x 55x 55x 55x 57x 10x 230x 230x 230x 36x 66x 1x 1x 65x 32x 33x 2x 31x 4x 27x 8x 19x 3x 16x 3x 3x 1x 15x 15x 15x 3x 15x 27x 2x 25x 16x 9x 9x 8x 8x 1x | /**
* Function Call Analyzer
* Enforces define-before-use for functions (ADR-030)
*
* C-Next requires functions to be defined before they can be called.
* This catches errors at C-Next compile time rather than deferring to
* the C compiler or runtime.
*/
import { ParseTreeWalker } from "antlr4ng";
import { CNextListener } from "../parser/grammar/CNextListener";
import * as Parser from "../parser/grammar/CNextParser";
import SymbolTable from "../symbols/SymbolTable";
import IFunctionCallError from "./types/IFunctionCallError";
import ParserUtils from "../../../utils/ParserUtils";
import CodeGenState from "../../state/CodeGenState";
import ExpressionUnwrapper from "../../../utils/ExpressionUnwrapper";
/**
* C-Next built-in functions
* These are compiler intrinsics that don't need to be defined by the user
*/
const CNEXT_BUILTINS: Set<string> = new Set([
"safe_div", // ADR-051: Safe division with default value
"safe_mod", // ADR-051: Safe modulo with default value
]);
/**
* Standard library functions mapped to their header files.
* Flat structure: function name → header file.
* These are considered "external" and don't need to be defined in C-Next.
*/
const STDLIB_FUNCTION_HEADERS: Record<string, string> = {
// stdio.h
printf: "stdio.h",
fprintf: "stdio.h",
sprintf: "stdio.h",
snprintf: "stdio.h",
scanf: "stdio.h",
fscanf: "stdio.h",
sscanf: "stdio.h",
fopen: "stdio.h",
fclose: "stdio.h",
fread: "stdio.h",
fwrite: "stdio.h",
fgets: "stdio.h",
fputs: "stdio.h",
fgetc: "stdio.h",
fputc: "stdio.h",
puts: "stdio.h",
putchar: "stdio.h",
getchar: "stdio.h",
gets: "stdio.h",
perror: "stdio.h",
fflush: "stdio.h",
fseek: "stdio.h",
ftell: "stdio.h",
rewind: "stdio.h",
feof: "stdio.h",
ferror: "stdio.h",
clearerr: "stdio.h",
remove: "stdio.h",
rename: "stdio.h",
tmpfile: "stdio.h",
tmpnam: "stdio.h",
setbuf: "stdio.h",
setvbuf: "stdio.h",
// stdlib.h
malloc: "stdlib.h",
calloc: "stdlib.h",
realloc: "stdlib.h",
free: "stdlib.h",
atoi: "stdlib.h",
atof: "stdlib.h",
atol: "stdlib.h",
atoll: "stdlib.h",
strtol: "stdlib.h",
strtoul: "stdlib.h",
strtoll: "stdlib.h",
strtoull: "stdlib.h",
strtof: "stdlib.h",
strtod: "stdlib.h",
strtold: "stdlib.h",
rand: "stdlib.h",
srand: "stdlib.h",
exit: "stdlib.h",
abort: "stdlib.h",
atexit: "stdlib.h",
system: "stdlib.h",
getenv: "stdlib.h",
abs: "stdlib.h",
labs: "stdlib.h",
llabs: "stdlib.h",
div: "stdlib.h",
ldiv: "stdlib.h",
lldiv: "stdlib.h",
qsort: "stdlib.h",
bsearch: "stdlib.h",
// string.h
strlen: "string.h",
strcpy: "string.h",
strncpy: "string.h",
strcat: "string.h",
strncat: "string.h",
strcmp: "string.h",
strncmp: "string.h",
strchr: "string.h",
strrchr: "string.h",
strstr: "string.h",
strtok: "string.h",
memcpy: "string.h",
memmove: "string.h",
memset: "string.h",
memcmp: "string.h",
memchr: "string.h",
// math.h
sin: "math.h",
cos: "math.h",
tan: "math.h",
asin: "math.h",
acos: "math.h",
atan: "math.h",
atan2: "math.h",
sinh: "math.h",
cosh: "math.h",
tanh: "math.h",
exp: "math.h",
log: "math.h",
log10: "math.h",
log2: "math.h",
pow: "math.h",
sqrt: "math.h",
cbrt: "math.h",
ceil: "math.h",
floor: "math.h",
round: "math.h",
trunc: "math.h",
fabs: "math.h",
fmod: "math.h",
remainder: "math.h",
fmax: "math.h",
fmin: "math.h",
hypot: "math.h",
ldexp: "math.h",
frexp: "math.h",
modf: "math.h",
// C99 classification macros (also functions in C++)
isnan: "math.h",
isinf: "math.h",
isfinite: "math.h",
isnormal: "math.h",
signbit: "math.h",
fpclassify: "math.h",
nan: "math.h",
nanf: "math.h",
nanl: "math.h",
// ctype.h
isalnum: "ctype.h",
isalpha: "ctype.h",
isdigit: "ctype.h",
isxdigit: "ctype.h",
islower: "ctype.h",
isupper: "ctype.h",
isspace: "ctype.h",
ispunct: "ctype.h",
isprint: "ctype.h",
isgraph: "ctype.h",
iscntrl: "ctype.h",
tolower: "ctype.h",
toupper: "ctype.h",
// time.h
time: "time.h",
clock: "time.h",
difftime: "time.h",
mktime: "time.h",
strftime: "time.h",
localtime: "time.h",
gmtime: "time.h",
asctime: "time.h",
ctime: "time.h",
// assert.h
assert: "assert.h",
// Arduino framework
pinMode: "Arduino.h",
digitalWrite: "Arduino.h",
digitalRead: "Arduino.h",
analogRead: "Arduino.h",
analogWrite: "Arduino.h",
delay: "Arduino.h",
delayMicroseconds: "Arduino.h",
millis: "Arduino.h",
micros: "Arduino.h",
attachInterrupt: "Arduino.h",
detachInterrupt: "Arduino.h",
noInterrupts: "Arduino.h",
interrupts: "Arduino.h",
Serial: "Arduino.h",
Wire: "Arduino.h",
SPI: "Arduino.h",
};
/**
* Listener that walks the parse tree and checks function calls
*/
class FunctionCallListener extends CNextListener {
private readonly analyzer: FunctionCallAnalyzer;
/** Current scope name (for member function resolution) */
private currentScope: string | null = null;
constructor(analyzer: FunctionCallAnalyzer) {
super();
this.analyzer = analyzer;
}
// ========================================================================
// ISR/Callback Variable Tracking (ADR-040)
// ========================================================================
/**
* Check if a type name represents a callable type (ISR, callback, or C function pointer typedef).
*/
private isCallableType(typeName: string): boolean {
return (
typeName === "ISR" ||
this.analyzer.isCallbackType(typeName) ||
this.analyzer.isCFunctionPointerTypedef(typeName)
);
}
/**
* Track ISR-typed variables from variable declarations
* e.g., `ISR handler <- myFunction;`
*/
override enterVariableDeclaration = (
ctx: Parser.VariableDeclarationContext,
): void => {
const typeName = ctx.type().getText();
if (this.isCallableType(typeName)) {
this.analyzer.defineCallableVariable(ctx.IDENTIFIER().getText());
}
};
/**
* Track ISR-typed parameters in function declarations
* e.g., `void execute(ISR handler) { handler(); }`
*/
override enterParameter = (ctx: Parser.ParameterContext): void => {
const typeName = ctx.type().getText();
if (this.isCallableType(typeName)) {
this.analyzer.defineCallableVariable(ctx.IDENTIFIER().getText());
}
};
// ========================================================================
// Function Definitions
// ========================================================================
override enterFunctionDeclaration = (
ctx: Parser.FunctionDeclarationContext,
): void => {
const name = ctx.IDENTIFIER().getText();
// If inside a scope, the full name is Scope_functionName
let fullName: string;
if (this.currentScope) {
fullName = `${this.currentScope}_${name}`;
} else {
fullName = name;
}
// Track that we're currently inside this function (for self-recursion detection)
this.analyzer.enterFunction(fullName);
this.analyzer.defineFunction(fullName);
};
override exitFunctionDeclaration = (
_ctx: Parser.FunctionDeclarationContext,
): void => {
// We're leaving the function definition
this.analyzer.exitFunction();
};
// ========================================================================
// Scope Handling
// ========================================================================
override enterScopeDeclaration = (
ctx: Parser.ScopeDeclarationContext,
): void => {
this.currentScope = ctx.IDENTIFIER().getText();
};
override exitScopeDeclaration = (
_ctx: Parser.ScopeDeclarationContext,
): void => {
this.currentScope = null;
};
// ========================================================================
// Function Calls
// ========================================================================
/**
* SonarCloud S3776: Refactored to use helper method.
*/
override enterPostfixExpression = (
ctx: Parser.PostfixExpressionContext,
): void => {
const ops = ctx.postfixOp();
const primary = ctx.primaryExpression();
// Determine the base name: could be an identifier or 'this'
const baseName = this.extractBaseName(primary);
if (!baseName) return;
// Walk through postfix ops to find the call and resolve the name
const { resolvedName, foundCall } = this.resolveCallTarget(ops, baseName);
if (!foundCall) return;
// Check if the function is defined
const { line, column } = ParserUtils.getPosition(ctx);
this.analyzer.checkFunctionCall(
resolvedName,
line,
column,
this.currentScope,
);
};
/**
* Extract base name from primary expression.
*/
private extractBaseName(
primary: Parser.PrimaryExpressionContext,
): string | null {
if (primary.IDENTIFIER()) {
return primary.IDENTIFIER()!.getText();
}
if (primary.THIS()) {
return "this";
}
return null;
}
/**
* Resolve call target by walking postfix operations.
* Returns the resolved function name and whether a call was found.
* SonarCloud S3776: Extracted from enterPostfixExpression().
*/
private resolveCallTarget(
ops: Parser.PostfixOpContext[],
baseName: string,
): { resolvedName: string; foundCall: boolean } {
let resolvedName = baseName;
for (const op of ops) {
// Member access: check if it's Scope.member or this.member pattern
if (op.IDENTIFIER()) {
const resolved = this.resolveMemberAccess(resolvedName, op);
if (resolved === null) {
return { resolvedName, foundCall: false };
}
resolvedName = resolved;
continue;
}
// Function call: () or (args)
if (op.argumentList() || op.getChildCount() === 2) {
const text = op.getText();
Eif (text.startsWith("(")) {
return { resolvedName, foundCall: true };
}
}
}
return { resolvedName, foundCall: false };
}
/**
* Resolve member access pattern. Returns new name or null if not a C-Next function.
*/
private resolveMemberAccess(
resolvedName: string,
op: Parser.PostfixOpContext,
): string | null {
const memberName = op.IDENTIFIER()!.getText();
// Handle this.member -> CurrentScope_member (when inside a scope)
if (resolvedName === "this" && this.currentScope) {
return `${this.currentScope}_${memberName}`;
}
// Check if base is a known scope
if (this.analyzer.isScope(resolvedName)) {
return `${resolvedName}_${memberName}`;
}
// Object.method or chained access - not a C-Next function call
return null;
}
}
/**
* Analyzes C-Next AST for function calls before definition
*/
class FunctionCallAnalyzer {
private errors: IFunctionCallError[] = [];
/** Functions that have been defined (in order of appearance) */
private definedFunctions: Set<string> = new Set();
/** All functions that will be defined in this file (for distinguishing local vs cross-file) */
private allLocalFunctions: Set<string> = new Set();
/** Known scopes (for Scope.member -> Scope_member resolution) */
private knownScopes: Set<string> = new Set();
/** External symbol table for C/C++ interop */
private symbolTable: SymbolTable | null = null;
/** Included headers (for stdlib function lookup) */
private includedHeaders: Set<string> = new Set();
/** Current function being defined (for self-recursion detection) */
private currentFunctionName: string | null = null;
/** ADR-040: Variables of type ISR or callback types that can be invoked */
private callableVariables: Set<string> = new Set();
/** ADR-029: Callback types (function-as-type pattern) */
private callbackTypes: Set<string> = new Set();
/**
* Analyze a parsed program for function call errors
* @param tree The parsed program AST
* @param symbolTable Optional symbol table for external function lookup
* @returns Array of function call errors
*/
public analyze(
tree: Parser.ProgramContext,
symbolTable?: SymbolTable,
): IFunctionCallError[] {
this.errors = [];
this.definedFunctions = new Set();
this.allLocalFunctions = new Set();
this.knownScopes = new Set();
this.includedHeaders = new Set();
this.symbolTable = symbolTable ?? null;
this.currentFunctionName = null;
this.callableVariables = new Set();
this.callbackTypes = new Set();
// First pass: collect scope names, includes, callback types, and all local functions
this.collectScopes(tree);
this.collectIncludes(tree);
this.collectCallbackTypes(tree);
this.collectAllLocalFunctions(tree);
this.collectCallbackCompatibleFunctions(tree);
// Second pass: walk tree in order, tracking definitions and checking calls
const listener = new FunctionCallListener(this);
ParseTreeWalker.DEFAULT.walk(listener, tree);
return this.errors;
}
/**
* Collect scope names for member function resolution
*/
private collectScopes(tree: Parser.ProgramContext): void {
for (const decl of tree.declaration()) {
if (decl.scopeDeclaration()) {
const name = decl.scopeDeclaration()!.IDENTIFIER().getText();
this.knownScopes.add(name);
}
}
}
/**
* Collect included headers for stdlib function lookup
*/
private collectIncludes(tree: Parser.ProgramContext): void {
for (const include of tree.includeDirective()) {
// Extract header name from #include <header.h> or #include "header.h"
const text = include.getText();
const match = /#include\s*[<"]([^>"]+)[>"]/.exec(text);
Eif (match) {
this.includedHeaders.add(match[1]);
}
}
}
/**
* Check if a function is from an included standard library header
*/
private isStdlibFunction(name: string): boolean {
const header = STDLIB_FUNCTION_HEADERS[name];
return header !== undefined && this.includedHeaders.has(header);
}
/**
* Issue #787: Find which header a stdlib function belongs to (if any).
* Returns the header name if found, or null if not a known stdlib function.
* Checks ALL stdlib functions, not just from included headers.
*/
private findStdlibHeader(name: string): string | null {
return STDLIB_FUNCTION_HEADERS[name] ?? null;
}
/**
* ADR-029: Collect callback types (function-as-type pattern)
* Any function definition creates a type that can be used for callback fields/parameters
*/
private collectCallbackTypes(tree: Parser.ProgramContext): void {
for (const decl of tree.declaration()) {
if (decl.functionDeclaration()) {
const name = decl.functionDeclaration()!.IDENTIFIER().getText();
this.callbackTypes.add(name);
}
}
}
/**
* Issue #786: Pre-collect all function names defined in this file.
* Used to distinguish between local functions (subject to define-before-use)
* and cross-file functions from includes (allowed without local definition).
*/
private collectAllLocalFunctions(tree: Parser.ProgramContext): void {
for (const decl of tree.declaration()) {
// Standalone functions
if (decl.functionDeclaration()) {
const name = decl.functionDeclaration()!.IDENTIFIER().getText();
this.allLocalFunctions.add(name);
}
// Scope member functions
if (decl.scopeDeclaration()) {
const scopeDecl = decl.scopeDeclaration()!;
const scopeName = scopeDecl.IDENTIFIER().getText();
for (const member of scopeDecl.scopeMember()) {
if (member.functionDeclaration()) {
const funcName = member
.functionDeclaration()!
.IDENTIFIER()
.getText();
this.allLocalFunctions.add(`${scopeName}_${funcName}`);
}
}
}
}
}
/**
* ADR-029: Check if a type name is a callback type (function-as-type)
*/
public isCallbackType(name: string): boolean {
return this.callbackTypes.has(name);
}
/**
* Check if a type name is a C function pointer typedef.
* Looks up the type in the symbol table and checks if it's a typedef
* whose underlying type contains "(*)" indicating a function pointer.
*/
public isCFunctionPointerTypedef(typeName: string): boolean {
if (!this.symbolTable) return false;
const sym = this.symbolTable.getCSymbol(typeName);
if (sym?.kind !== "type") return false;
// ICTypedefSymbol has a `type` field with the underlying C type string
return (
"type" in sym && typeof sym.type === "string" && sym.type.includes("(*)")
);
}
/** Current scope name during callback assignment scanning */
private scanCurrentScope: string | null = null;
/**
* Detect functions assigned to C function pointer typedefs.
* When `PointCallback cb <- my_handler;` is found and PointCallback
* is a C function pointer typedef, mark my_handler as callback-compatible.
*/
private collectCallbackCompatibleFunctions(
tree: Parser.ProgramContext,
): void {
for (const decl of tree.declaration()) {
const funcDecl = decl.functionDeclaration();
if (funcDecl) {
this.scanStandaloneFunctionForCallbacks(funcDecl);
continue;
}
const scopeDecl = decl.scopeDeclaration();
if (scopeDecl) {
this.scanScopeForCallbacks(scopeDecl);
}
}
}
/**
* Scan a standalone function declaration for callback assignments.
*/
private scanStandaloneFunctionForCallbacks(
funcDecl: Parser.FunctionDeclarationContext,
): void {
const block = funcDecl.block();
Iif (!block) return;
this.scanCurrentScope = null;
this.scanBlockForCallbackAssignments(block);
}
/**
* Scan all member functions in a scope for callback assignments (Issue #895).
*/
private scanScopeForCallbacks(
scopeDecl: Parser.ScopeDeclarationContext,
): void {
const scopeName = scopeDecl.IDENTIFIER().getText();
for (const member of scopeDecl.scopeMember()) {
this.scanScopeMemberForCallbacks(member, scopeName);
}
this.scanCurrentScope = null;
}
/**
* Scan a single scope member for callback assignments.
*/
private scanScopeMemberForCallbacks(
member: Parser.ScopeMemberContext,
scopeName: string,
): void {
const memberFunc = member.functionDeclaration();
if (!memberFunc) return;
const block = memberFunc.block();
Iif (!block) return;
this.scanCurrentScope = scopeName;
this.scanBlockForCallbackAssignments(block);
}
/**
* Recursively scan all statements in a block for callback typedef assignments.
*/
private scanBlockForCallbackAssignments(block: Parser.BlockContext): void {
for (const stmt of block.statement()) {
this.scanStatementForCallbackAssignments(stmt);
}
}
/**
* Scan a single statement for callback typedef assignments,
* recursing into nested blocks (if/while/for/do-while/switch/critical).
*/
private scanStatementForCallbackAssignments(
stmt: Parser.StatementContext,
): void {
// Check variable declarations for callback assignments
const varDecl = stmt.variableDeclaration();
if (varDecl) {
this.checkVarDeclForCallbackAssignment(varDecl);
return;
}
// Check expression statements for function calls with callback arguments
const exprStmt = stmt.expressionStatement();
if (exprStmt) {
this.checkExpressionForCallbackArgs(exprStmt.expression());
return;
}
// Recurse into nested blocks/statements
const ifStmt = stmt.ifStatement();
if (ifStmt) {
for (const child of ifStmt.statement()) {
this.scanStatementForCallbackAssignments(child);
}
return;
}
const whileStmt = stmt.whileStatement();
if (whileStmt) {
this.scanStatementForCallbackAssignments(whileStmt.statement());
return;
}
const forStmt = stmt.forStatement();
if (forStmt) {
this.scanStatementForCallbackAssignments(forStmt.statement());
return;
}
const doWhileStmt = stmt.doWhileStatement();
Iif (doWhileStmt) {
this.scanBlockForCallbackAssignments(doWhileStmt.block());
return;
}
const switchStmt = stmt.switchStatement();
Iif (switchStmt) {
for (const caseCtx of switchStmt.switchCase()) {
this.scanBlockForCallbackAssignments(caseCtx.block());
}
const defaultCtx = switchStmt.defaultCase();
if (defaultCtx) {
this.scanBlockForCallbackAssignments(defaultCtx.block());
}
return;
}
const criticalStmt = stmt.criticalStatement();
Iif (criticalStmt) {
this.scanBlockForCallbackAssignments(criticalStmt.block());
return;
}
// A statement can itself be a block
const nestedBlock = stmt.block();
if (nestedBlock) {
this.scanBlockForCallbackAssignments(nestedBlock);
}
}
/**
* Check if a variable declaration assigns a function to a C callback typedef.
*/
private checkVarDeclForCallbackAssignment(
varDecl: Parser.VariableDeclarationContext,
): void {
const typeName = varDecl.type().getText();
if (!this.isCFunctionPointerTypedef(typeName)) return;
const expr = varDecl.expression();
Iif (!expr) return;
const funcRef = this.extractFunctionReference(expr);
Iif (!funcRef) return;
// Scope-qualified names use dot in source (MyScope.handler) but
// allLocalFunctions stores them with underscore (MyScope_handler)
const lookupName = funcRef.includes(".")
? funcRef.replace(".", "_")
: funcRef;
if (this.allLocalFunctions.has(lookupName)) {
// Store function name -> typedef name mapping
CodeGenState.callbackCompatibleFunctions.set(lookupName, typeName);
}
}
/**
* Extract a function reference from an expression context.
* Matches:
* - Bare identifiers: "my_handler"
* - Qualified scope names: "MyScope.handler"
* - Self-scope reference: "this.handler" (resolved using scanCurrentScope)
* - Global scope reference: "global.ScopeName.handler"
* Returns null if the expression is not a function reference.
*/
private extractFunctionReference(
expr: Parser.ExpressionContext,
): string | null {
const text = expr.getText();
// Pattern 1: this.member -> CurrentScope.member (Issue #895)
const thisPattern = /^this\.(\w+)$/;
const thisMatch = thisPattern.exec(text);
if (thisMatch) {
Iif (!this.scanCurrentScope) {
return null; // this.member outside scope context
}
return this.scanCurrentScope + "." + thisMatch[1];
}
// Pattern 2: global.Scope.member -> Scope.member (Issue #895)
const globalPattern = /^global\.(\w+)\.(\w+)$/;
const globalMatch = globalPattern.exec(text);
if (globalMatch) {
return globalMatch[1] + "." + globalMatch[2];
}
// Pattern 3: Bare identifier or simple Scope.member
const simplePattern = /^\w+(\.\w+)?$/;
Eif (simplePattern.test(text)) {
return text;
}
return null;
}
/**
* Issue #895: Check expression for function calls that pass C-Next functions
* to C function pointer parameters.
*
* Pattern: `global.widget_set_flush_cb(w, my_flush)`
* Where widget_set_flush_cb's 2nd param is a C function pointer typedef.
*/
private checkExpressionForCallbackArgs(expr: Parser.ExpressionContext): void {
// Navigate to the postfix expression (handles assignments, ternaries, etc.)
const postfix = this.findPostfixExpression(expr);
Iif (!postfix) return;
// Extract function name and argument list
const callInfo = this.extractCallInfo(postfix);
if (!callInfo) return;
// Look up the C function in symbol table
const cFunc = this.symbolTable?.getCSymbol(callInfo.funcName);
if (cFunc?.kind !== "function" || !cFunc.parameters) return;
// Check each argument against the corresponding parameter type
for (let i = 0; i < callInfo.args.length; i++) {
const param = cFunc.parameters[i];
Iif (!param) continue;
// Check if parameter type is a function pointer typedef
if (!this.isCFunctionPointerTypedef(param.type)) continue;
// Extract function reference from argument
const funcRef = this.extractFunctionReference(callInfo.args[i]);
Iif (!funcRef) continue;
// Normalize scope-qualified names
const lookupName = funcRef.includes(".")
? funcRef.replace(".", "_")
: funcRef;
// Mark as callback-compatible if it's a local C-Next function
if (this.allLocalFunctions.has(lookupName)) {
// Store function name -> typedef name mapping
CodeGenState.callbackCompatibleFunctions.set(lookupName, param.type);
}
}
}
/**
* Find the postfix expression within an expression tree.
* Uses ExpressionUnwrapper which validates that expression is "simple"
* (single term at each level), returning null for complex expressions.
*/
private findPostfixExpression(
expr: Parser.ExpressionContext,
): Parser.PostfixExpressionContext | null {
return ExpressionUnwrapper.getPostfixExpression(expr);
}
/**
* Extract function name and arguments from a postfix expression.
* Returns null if not a function call.
*/
private extractCallInfo(
postfix: Parser.PostfixExpressionContext,
): { funcName: string; args: Parser.ExpressionContext[] } | null {
const primary = postfix.primaryExpression();
const ops = postfix.postfixOp();
// Start with primary expression (identifier or 'global')
const ident = primary.IDENTIFIER();
const globalKw = primary.GLOBAL();
// Early return: neither identifier nor global keyword means not a function call
if (!ident && !globalKw) {
return null;
}
// Build function name from primary + member access ops
// For 'global' keyword, funcName starts empty and gets built from member access
let funcName = ident ? ident.getText() : "";
let argListOp: Parser.PostfixOpContext | null = null;
// Walk postfix ops to find function name and call
for (const op of ops) {
if (op.IDENTIFIER()) {
// Member access: build qualified name
const member = op.IDENTIFIER()!.getText();
funcName = funcName ? `${funcName}_${member}` : member;
E} else if (op.argumentList() || op.getText().startsWith("(")) {
// Found the call - this op has the arguments
argListOp = op;
break;
}
}
Iif (!argListOp || !funcName) return null;
// Extract arguments
const argList = argListOp.argumentList();
const args = argList?.expression() ?? [];
return { funcName, args };
}
/**
* ADR-040: Register a variable that holds a callable (ISR or callback)
*/
public defineCallableVariable(name: string): void {
this.callableVariables.add(name);
}
/**
* Register a function as defined
*/
public defineFunction(name: string): void {
this.definedFunctions.add(name);
}
/**
* Track entering a function definition (for self-recursion detection)
*/
public enterFunction(name: string): void {
this.currentFunctionName = name;
}
/**
* Track exiting a function definition
*/
public exitFunction(): void {
this.currentFunctionName = null;
}
/**
* Check if a function name is a known scope
*/
public isScope(name: string): boolean {
return this.knownScopes.has(name);
}
/**
* Check if a function call is valid (function is defined or external)
* @param name The function name being called
* @param line Source line number
* @param column Source column number
* @param currentScope The current scope name (if inside a scope)
*/
public checkFunctionCall(
name: string,
line: number,
column: number,
currentScope: string | null,
): void {
// Check for self-recursion (MISRA C:2012 Rule 17.2)
if (this.currentFunctionName && name === this.currentFunctionName) {
this.errors.push({
code: "E0423",
functionName: name,
line,
column,
message: `recursive call to '${name}' is forbidden (MISRA C:2012 Rule 17.2)`,
});
return;
}
// Check if function is defined in C-Next
if (this.definedFunctions.has(name)) {
return; // OK - defined before use
}
// Check if function is a C-Next built-in
if (CNEXT_BUILTINS.has(name)) {
return; // OK - built-in function
}
// Check if function is from an included standard library header
if (this.isStdlibFunction(name)) {
return; // OK - standard library function
}
// Check if function is external (from symbol table)
if (this.isExternalFunction(name)) {
return; // OK - external C/C++ function
}
// ADR-040: Check if this is an ISR or callback variable being invoked
if (this.callableVariables.has(name)) {
return; // OK - invoking a function pointer variable
}
// ADR-057: Allow implicit scope function calls without this. prefix
// Check if this is an unqualified call to a scope function
// e.g., calling helper() instead of this.helper() inside a scope
if (currentScope) {
const qualifiedName = `${currentScope}_${name}`;
if (this.definedFunctions.has(qualifiedName)) {
return; // OK - implicit resolution will handle it
}
}
// Not defined - report error with optional hint
const header = this.findStdlibHeader(name);
let message = `function '${name}' called before definition`;
if (header) {
message += `; hint: '${name}' is available from ${header} — try global.${name}()`;
}
this.errors.push({
code: "E0422",
functionName: name,
line,
column,
message,
});
}
/**
* Check if a function is defined externally (from included files)
* This includes C/C++ headers AND C-Next includes.
*
* Issue #786: Only considers a function "external" if it's NOT defined
* in the current file. Functions defined locally are subject to
* define-before-use checking, even if they exist in the SymbolTable.
*/
private isExternalFunction(name: string): boolean {
// If the function is defined in this file, it's not external
// (even if it's also in the SymbolTable from symbol collection)
if (this.allLocalFunctions.has(name)) {
return false;
}
if (!this.symbolTable) {
return false;
}
const symbols = this.symbolTable.getOverloads(name);
for (const sym of symbols) {
// Accept functions from any source language:
// - C/C++ functions from header includes
// - C-Next functions from .cnx file includes
Eif (sym.kind === "function") {
return true;
}
}
return false;
}
}
export default FunctionCallAnalyzer;
|