(source: string)
| 941 | 'if', 'while', 'for', 'switch', 'return', 'do', 'else', 'sizeof', |
| 942 | ]); |
| 943 | export function blankCStatementMacroCalls(source: string): string { |
| 944 | const lines = source.split('\n'); |
| 945 | let changed = false; |
| 946 | const content = (l: string): string => l.replace(/\r$/, '').trim(); |
| 947 | for (let i = 0; i < lines.length; i++) { |
| 948 | const line = lines[i] as string; |
| 949 | const m = /^[ \t]+([a-z_][a-z0-9_]*)[ \t]*\(/.exec(line); |
| 950 | if (!m || C_STMT_MACRO_KEYWORDS.has(m[1] as string)) continue; |
| 951 | const open = line.indexOf('(', m[0].length - 1); |
| 952 | let depth = 0; |
| 953 | let close = -1; |
| 954 | for (let k = open; k < line.length; k++) { |
| 955 | const ch = line[k]; |
| 956 | if (ch === '"' || ch === "'") { |
| 957 | const quote = ch; |
| 958 | k++; |
| 959 | while (k < line.length && line[k] !== quote) { |
| 960 | if (line[k] === '\\') k++; |
| 961 | k++; |
| 962 | } |
| 963 | continue; |
| 964 | } |
| 965 | if (ch === '(') depth++; |
| 966 | else if (ch === ')') { |
| 967 | depth--; |
| 968 | if (depth === 0) { |
| 969 | close = k; |
| 970 | break; |
| 971 | } |
| 972 | } |
| 973 | } |
| 974 | let endLine = i; |
| 975 | if (close < 0) { |
| 976 | // Parens don't balance on the head line — the kernel wraps iterator |
| 977 | // macros (`hlist_for_each_entry_rcu(p, head, hlist,\n\t\t |
| 978 | // lockdep_is_held(&kprobe_mutex)) {`). Continue the same |
| 979 | // string-skipping paren scan over a few continuation lines. A `;` or |
| 980 | // a brace anywhere in the span means a real statement or compound |
| 981 | // literal — bail (missing a blank is safe; corrupting one is not). |
| 982 | if (line.indexOf(';') !== -1) continue; |
| 983 | for (let j = i + 1; j <= i + 5 && j < lines.length && close < 0; j++) { |
| 984 | const cont = lines[j] as string; |
| 985 | let bail = false; |
| 986 | for (let k = 0; k < cont.length; k++) { |
| 987 | const ch = cont[k]; |
| 988 | if (ch === '"' || ch === "'") { |
| 989 | const quote = ch; |
| 990 | k++; |
| 991 | while (k < cont.length && cont[k] !== quote) { |
| 992 | if (cont[k] === '\\') k++; |
| 993 | k++; |
| 994 | } |
| 995 | continue; |
| 996 | } |
| 997 | if (ch === ';' || ch === '{' || ch === '}') { |
| 998 | bail = true; |
| 999 | break; |
| 1000 | } |
no test coverage detected