MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / cStatementLines

Function cStatementLines

src/extraction/languages/c-cpp.ts:2088–2129  ·  view source on GitHub ↗

* Which lines of a C file start a statement of a function body, given the * file's code (maskCppCode: directives and comments are spaces). A brace opens * a block after a function or control head, or where a statement could start * inside another block; `= {`, `struct s {` and `enum {` hold no st

(code: string)

Source from the content-addressed store, hash-verified

2086 * then reads as no statement.
2087 */
2088function cStatementLines(code: string): boolean[] {
2089 const statements: boolean[] = [];
2090 const open: number[] = [];
2091 let line = 0;
2092 let atLineStart = true;
2093 let prev = -1;
2094 // Whether the `)` at `prev` closes a control head, whose body comes next.
2095 let afterControlHead = false;
2096 for (let i = 0; i < code.length; i++) {
2097 const ch = code[i];
2098 if (ch === '\n') {
2099 line++;
2100 atLineStart = true;
2101 continue;
2102 }
2103 if (ch === ' ' || ch === '\t' || ch === '\r') continue;
2104 const top = open[open.length - 1];
2105 if (atLineStart) {
2106 atLineStart = false;
2107 statements[line] = top === C_BLOCK && !afterControlHead && cStatementFollows(code[prev]);
2108 }
2109 afterControlHead = false;
2110 if (ch === '(' || ch === '[') {
2111 open.push(i);
2112 } else if (ch === ')' || ch === ']') {
2113 if (top !== undefined && top >= 0) {
2114 open.pop();
2115 afterControlHead = /\b(?:if|for|while|switch)\s*$/.test(code.slice(Math.max(0, top - 10), top));
2116 }
2117 } else if (ch === '{') {
2118 const opensBlock =
2119 code[prev] === '(' || // `({ … })`, a GNU statement expression
2120 (code[prev] === ')' && top === undefined) ||
2121 (top === C_BLOCK && (cStatementFollows(code[prev]) || /(?:^|\W)(?:else|do)$/.test(code.slice(Math.max(0, prev - 4), prev + 1))));
2122 open.push(opensBlock ? C_BLOCK : C_OTHER_BRACES);
2123 } else if (ch === '}') {
2124 while (open.length > 0 && (open.pop() as number) >= 0);
2125 }
2126 prev = i;
2127 }
2128 return statements;
2129}
2130
2131export function blankCStatementMacroCalls(source: string): string {
2132 const rawStrings = maskCppRawStrings(source);

Callers 1

Calls 2

cStatementFollowsFunction · 0.85
pushMethod · 0.80

Tested by

no test coverage detected