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 | 15x 81x 81x 79x 12x 12x 12x 11x 11x 10x 11x 11x 11x 6x 11x 49x 49x 49x 10x 10x 10x 39x 13x 13x 13x 26x 23x 23x 5x 18x 10x 9x 10x 9x 9x 13x 12x 12x 1x 11x 49x 168x 5x 5x 291x 5x 5x 291x 291x 13x 12x 13x 278x 56x 222x 222x 222x 2x 2x 1x 1x 1x 220x 5x 5x 5x 215x 23x 23x 23x 23x 23x 192x 192x 192x 56x 56x 4x 4x 52x 5x 5x 3x 3x 2x 2x 47x 47x | /**
* CompileCommandsReader — derive the compiler's own view (include search path,
* preprocessor defines, compiler binary) from a `compile_commands.json`
* compilation database.
*
* cnext is a source consumer that must resolve external C/C++ headers exactly as
* the compiler will. Rather than couple to any one build system's path logic (or
* hand-mirror it in cnext.config.json), it reads the build-system-agnostic
* contract every build system converges on: the compiler command line, serialized
* per translation unit in `compile_commands.json` (the LLVM compilation database
* that clangd/clang-tidy also consume). Framework include paths are project-global
* — identical across translation units — so the union of every entry's `-I` set is
* the search path cnext needs; defines and the compiler binary come along for free.
*
* This is the pure parse layer: a database string in, `{ includePaths, defines,
* compiler }` out. No filesystem access — the loader layer reads the file.
*/
import { readFileSync } from "node:fs";
import { resolve, isAbsolute } from "node:path";
import ICompileCommandsResult from "./types/ICompileCommandsResult";
/** One compile_commands.json entry (clang spec: `command` OR `arguments`). */
interface ICompileCommandsEntry {
directory?: string;
file?: string;
command?: string;
arguments?: string[];
}
/** Mutable state threaded through the POSIX word-splitter. */
interface ITokenizerState {
tokens: string[];
current: string;
/** Whether a token is in progress (so empty quotes still emit ""). */
started: boolean;
mode: "normal" | "single" | "double";
}
/** Mutable state accumulated across every compile-DB entry while parsing. */
interface ICompileCommandsAccumulator {
/** Include search paths in first-seen order, de-duplicated via `seen`. */
includePaths: string[];
/** Dedup guard for `includePaths`. */
seen: Set<string>;
/** Preprocessor defines (last write wins across entries). */
defines: Record<string, string | boolean>;
/** Compiler binary — the first entry-with-args argv[0] wins. */
compiler: string | null;
}
class CompileCommandsReader {
/**
* Include-search flags whose operand is a directory. Each accepts an operand
* attached to the flag or as the following token. `-I` is case-sensitive, so
* it never swallows the lowercase `-isystem`/`-iquote`/`-idirafter` flags.
*/
private static readonly INCLUDE_FLAGS = [
"-I",
"-isystem",
"-iquote",
"-idirafter",
];
/**
* Read and parse a `compile_commands.json` from disk. Returns null if the file
* is missing or malformed, so callers can fall back to configured includes
* rather than fail — a missing compile database is a normal, non-fatal state.
*/
static load(path: string): ICompileCommandsResult | null {
try {
return CompileCommandsReader.parse(readFileSync(path, "utf8"));
} catch {
return null;
}
}
/**
* Parse a `compile_commands.json` string into the compiler's include search
* path, defines, and compiler binary (combined across all entries).
*/
static parse(jsonContent: string): ICompileCommandsResult {
const accumulator: ICompileCommandsAccumulator = {
includePaths: [],
seen: new Set<string>(),
defines: {},
compiler: null,
};
const entries = JSON.parse(jsonContent) as ICompileCommandsEntry[];
for (const entry of entries) {
const args = CompileCommandsReader.entryArgs(entry);
if (accumulator.compiler === null && args.length > 0) {
accumulator.compiler = args[0]; // argv[0] is the compiler binary
}
CompileCommandsReader.processArgs(
accumulator,
args,
entry.directory ?? "",
);
}
return {
includePaths: accumulator.includePaths,
defines: accumulator.defines,
compiler: accumulator.compiler,
};
}
/**
* Resolve one entry's argv: explicit `arguments` if present, else the
* word-split `command` string, else none. `arguments` is used when truthy so a
* present-but-empty `[]` is kept (an entry with no compile args) — matching the
* clang spec's `command`-OR-`arguments` shape.
*/
private static entryArgs(entry: ICompileCommandsEntry): string[] {
if (entry.arguments) return entry.arguments;
return entry.command ? CompileCommandsReader.tokenize(entry.command) : [];
}
/** Collect include paths and defines from one entry's argv. */
private static processArgs(
accumulator: ICompileCommandsAccumulator,
args: string[],
directory: string,
): void {
for (let i = 0; i < args.length; i++) {
i += CompileCommandsReader.processArg(accumulator, args, i, directory);
}
}
/**
* Classify one arg — an include flag or a `-D` define — and collect its
* operand. Returns the number of ADDITIONAL tokens consumed (0, or 1 for a
* space-separated operand) so the caller advances past it.
*/
private static processArg(
accumulator: ICompileCommandsAccumulator,
args: string[],
index: number,
directory: string,
): number {
const includeFlag = CompileCommandsReader.matchIncludeFlag(args[index]);
if (includeFlag) {
const operand = CompileCommandsReader.resolveOperand(
args,
index,
includeFlag.length,
);
CompileCommandsReader.collectIncludePath(
accumulator,
directory,
operand.value,
);
return operand.extraConsumed;
}
if (args[index].startsWith("-D")) {
const operand = CompileCommandsReader.resolveOperand(args, index, 2);
CompileCommandsReader.collectDefine(accumulator, operand.value);
return operand.extraConsumed;
}
return 0;
}
/**
* Resolve a flag's operand: the text attached directly to the flag, or — if
* that is empty — the next token (the space-separated `-I foo` form), then
* consumed. The next-token form is taken unconditionally when the attached text
* is empty, even at the end of argv, so index advancement stays in step.
*/
private static resolveOperand(
args: string[],
index: number,
prefixLength: number,
): { value: string; extraConsumed: number } {
const attached = args[index].slice(prefixLength);
if (attached === "") {
return { value: args[index + 1] ?? "", extraConsumed: 1 };
}
return { value: attached, extraConsumed: 0 };
}
/**
* Collect an include directory (absolute kept verbatim; relative resolved
* against the entry `directory`), de-duplicated in first-seen order. An empty
* operand (a flag with no directory) is ignored.
*/
private static collectIncludePath(
accumulator: ICompileCommandsAccumulator,
directory: string,
raw: string,
): void {
if (raw === "") return;
const path = isAbsolute(raw) ? raw : resolve(directory, raw);
if (!accumulator.seen.has(path)) {
accumulator.seen.add(path);
accumulator.includePaths.push(path);
}
}
/**
* Collect a `-D` define, split on the FIRST `=`: `KEY=VAL` -> `VAL` (a trailing
* `KEY=` keeps the empty value), bare `KEY` -> `true`. An empty operand is
* ignored.
*/
private static collectDefine(
accumulator: ICompileCommandsAccumulator,
raw: string,
): void {
if (raw === "") return;
const eq = raw.indexOf("=");
if (eq === -1) {
accumulator.defines[raw] = true;
} else {
accumulator.defines[raw.slice(0, eq)] = raw.slice(eq + 1);
}
}
/** Return the include flag `arg` begins with, or null if it is not one. */
private static matchIncludeFlag(arg: string): string | null {
return (
CompileCommandsReader.INCLUDE_FLAGS.find((flag) =>
arg.startsWith(flag),
) ?? null
);
}
/**
* Split a `command` string into argv, matching POSIX shell word-splitting (as
* `shlex`/`/bin/sh` do) so tokens equal what the compiler was actually invoked
* with. Real quotes are syntax (consumed); backslash-escaped quotes are literal
* (kept); whitespace inside quotes is preserved. Only the cases that occur in
* real compile databases are handled — no `$`/backtick expansion.
*/
private static tokenize(command: string): string[] {
const state: ITokenizerState = {
tokens: [],
current: "",
started: false,
mode: "normal",
};
for (let i = 0; i < command.length; i++) {
i += CompileCommandsReader.tokenizeChar(state, command, i);
}
Eif (state.started) state.tokens.push(state.current);
return state.tokens;
}
/**
* Consume one character at `command[i]` in the current tokenizer mode.
* Returns the number of ADDITIONAL characters consumed (0 or 1) so the caller
* can advance past an escape's target.
*/
private static tokenizeChar(
state: ITokenizerState,
command: string,
i: number,
): number {
const ch = command[i];
if (state.mode === "single") {
if (ch === "'") state.mode = "normal";
else state.current += ch;
return 0;
}
if (state.mode === "double") {
return CompileCommandsReader.tokenizeDoubleQuoted(state, command, i);
}
return CompileCommandsReader.tokenizeNormal(state, command, i);
}
/** Handle one character outside quotes; returns extra chars consumed. */
private static tokenizeNormal(
state: ITokenizerState,
command: string,
i: number,
): number {
const ch = command[i];
if (ch === "\\") {
const next = command[i + 1];
if (next === undefined) return 0; // trailing backslash: nothing follows
state.current += next; // outside quotes, backslash escapes any next char
state.started = true;
return 1;
}
if (ch === "'" || ch === '"') {
state.mode = ch === "'" ? "single" : "double";
state.started = true;
return 0;
}
if (ch === " " || ch === "\t" || ch === "\n" || ch === "\r") {
Eif (state.started) {
state.tokens.push(state.current);
state.current = "";
state.started = false;
}
return 0;
}
state.current += ch;
state.started = true;
return 0;
}
/** Handle one character inside double quotes; returns extra chars consumed. */
private static tokenizeDoubleQuoted(
state: ITokenizerState,
command: string,
i: number,
): number {
const ch = command[i];
if (ch === '"') {
state.mode = "normal";
return 0;
}
if (ch === "\\") {
const next = command[i + 1];
// Match POSIX `shlex` (the reference this tokenizer is validated against):
// inside double quotes only `\"` and `\\` are escapes. Before anything else
// — `$`, a backtick, a newline — the backslash is a literal character.
// shlex and bash disagree there; we pin to shlex for verifiable parity, and
// real compile databases never quote those characters anyway.
if (next === '"' || next === "\\") {
state.current += next;
return 1;
}
state.current += ch;
return 0;
}
state.current += ch;
return 0;
}
}
export default CompileCommandsReader;
|