(source: string)
| 2129 | } |
| 2130 | |
| 2131 | export function blankCStatementMacroCalls(source: string): string { |
| 2132 | const rawStrings = maskCppRawStrings(source); |
| 2133 | source = rawStrings.source; |
| 2134 | const lines = source.split('\n'); |
| 2135 | let changed = false; |
| 2136 | let statementLines: boolean[] | null = null; |
| 2137 | const content = (l: string): string => l.replace(/\r$/, '').trim(); |
| 2138 | for (let i = 0; i < lines.length; i++) { |
| 2139 | const line = lines[i] as string; |
| 2140 | // Lowercase iterator macros, or an all-caps block macro |
| 2141 | // (`ATOMIC_BLOCK(NVIC_PRIO_MAX) {`, `PG_FOREACH(reg) {` — betaflight); |
| 2142 | // a PascalCase name is left alone (a constructor, if the file is C++). |
| 2143 | const m = /^[ \t]+([a-z_][a-z0-9_]*|[A-Z_][A-Z0-9_]*)[ \t]*\(/.exec(line); |
| 2144 | if (!m || C_STMT_MACRO_KEYWORDS.has(m[1] as string)) continue; |
| 2145 | const open = line.indexOf('(', m[0].length - 1); |
| 2146 | let depth = 0; |
| 2147 | let close = -1; |
| 2148 | for (let k = open; k < line.length; k++) { |
| 2149 | const ch = line[k]; |
| 2150 | if (ch === '"' || ch === "'") { |
| 2151 | const quote = ch; |
| 2152 | k++; |
| 2153 | while (k < line.length && line[k] !== quote) { |
| 2154 | if (line[k] === '\\') k++; |
| 2155 | k++; |
| 2156 | } |
| 2157 | continue; |
| 2158 | } |
| 2159 | if (ch === '(') depth++; |
| 2160 | else if (ch === ')') { |
| 2161 | depth--; |
| 2162 | if (depth === 0) { |
| 2163 | close = k; |
| 2164 | break; |
| 2165 | } |
| 2166 | } |
| 2167 | } |
| 2168 | let endLine = i; |
| 2169 | if (close < 0) { |
| 2170 | // Parens don't balance on the head line — the kernel wraps iterator |
| 2171 | // macros (`hlist_for_each_entry_rcu(p, head, hlist,\n\t\t |
| 2172 | // lockdep_is_held(&kprobe_mutex)) {`). Continue the same |
| 2173 | // string-skipping paren scan over a few continuation lines. A `;` or |
| 2174 | // a brace anywhere in the span means a real statement or compound |
| 2175 | // literal — bail (missing a blank is safe; corrupting one is not). |
| 2176 | if (line.indexOf(';') !== -1) continue; |
| 2177 | for (let j = i + 1; j <= i + 5 && j < lines.length && close < 0; j++) { |
| 2178 | const cont = lines[j] as string; |
| 2179 | let bail = false; |
| 2180 | for (let k = 0; k < cont.length; k++) { |
| 2181 | const ch = cont[k]; |
| 2182 | if (ch === '"' || ch === "'") { |
| 2183 | const quote = ch; |
| 2184 | k++; |
| 2185 | while (k < cont.length && cont[k] !== quote) { |
| 2186 | if (cont[k] === '\\') k++; |
| 2187 | k++; |
| 2188 | } |
no test coverage detected