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

Function cStatementLines

src/extraction/languages/c-cpp.ts:2089–2130  ·  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

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

Callers 1

Calls 2

cStatementFollowsFunction · 0.85
pushMethod · 0.80

Tested by

no test coverage detected