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 | 1387x 3x 21x 31x 77x 1192x 1192x 1192x 116x 85x 31x 133x 10x 123x 102x 68x 68x 109x 109x 46x 68x 2x 2x 13x 13x 2x 1058x 1058x 54x 54x 54x 54x 90x 90x 32x 90x 54x 54x 54x 54x 54x 54x 32x 54x 54x 14x 54x 21x 21x 54x 1078x 1186x 68x 68x 68x 68x | import LANGUAGE_STANDARD_FAMILY from "../transpiler/constants/LANGUAGE_STANDARD_FAMILY";
import LANGUAGE_STANDARD_ORDER from "../transpiler/constants/LANGUAGE_STANDARD_ORDER";
import TOOLCHAIN_REQUIREMENTS from "../transpiler/constants/TOOLCHAIN_REQUIREMENTS";
import type ICompilerFloor from "../transpiler/types/ICompilerFloor";
import type IRecordedRequirement from "../transpiler/types/IRecordedRequirement";
import type IToolchainRequirement from "../transpiler/types/IToolchainRequirement";
import type TLanguageStandard from "../transpiler/types/TLanguageStandard";
import type TCompilerExtension from "../transpiler/types/TCompilerExtension";
import type TOutputMode from "../transpiler/types/TOutputMode";
import type TRequirementKey from "../transpiler/types/TRequirementKey";
/**
* Issue #1143: Shared reasoning over recorded toolchain requirements.
*
* Every consumer -- the transpile-time report, the per-file banner,
* docs/compatibility.md, the MISRA rows, the guard emitter -- asks its
* questions here. That is deliberate: if each consumer decided for itself what
* counts as "worth mentioning", they would drift apart, which is the failure
* this issue exists to fix.
*/
class ToolchainRequirementUtils {
/** The requirement every file in a given mode already carries. */
static baselineKey(mode: TOutputMode): TRequirementKey {
return mode === "cpp" ? "baseline-cpp" : "baseline-c";
}
/**
* Is this key one of the per-mode baselines?
*
* Asked against the baseline keys themselves rather than a `baseline-` name
* prefix, so renaming a key cannot silently change which entries a consumer
* treats as free.
*/
static isBaseline(key: TRequirementKey): boolean {
return (
key === ToolchainRequirementUtils.baselineKey("c") ||
key === ToolchainRequirementUtils.baselineKey("cpp")
);
}
/**
* Which output mode produced a recorded set.
*
* Every file records its mode's baseline, so the answer is in the data. Read
* it here rather than at each consumer: a consumer that infers the mode from
* some other property of a requirement gets the right answer only while the
* registry happens to contain nothing that contradicts it.
*/
static modeOf(recorded: readonly IRecordedRequirement[]): TOutputMode {
const cppBaseline = ToolchainRequirementUtils.baselineKey("cpp");
return recorded.some((entry) => entry.key === cppBaseline) ? "cpp" : "c";
}
/** Look up a registry entry by key. */
static lookup(key: TRequirementKey): IToolchainRequirement {
return TOOLCHAIN_REQUIREMENTS[key];
}
/**
* Does this requirement cost the user anything beyond the mode's baseline?
*
* True when it needs a later language standard than the baseline, or a
* compiler extension, or a compiler version, or a platform library. A
* requirement that costs nothing on all four axes is not worth reporting --
* it is what the baseline already promised.
*/
static isAboveBaseline(key: TRequirementKey, mode: TOutputMode): boolean {
const baseline =
TOOLCHAIN_REQUIREMENTS[ToolchainRequirementUtils.baselineKey(mode)];
const requirement = TOOLCHAIN_REQUIREMENTS[key];
if (requirement.key === baseline.key) return false;
if (
requirement.compiler !== null ||
requirement.extensions.length > 0 ||
requirement.platformLib !== null
) {
return true;
}
return ToolchainRequirementUtils.exceedsStandard(requirement, baseline);
}
/**
* Is `requirement`'s standard later than `baseline`'s?
*
* Cross-family comparisons are meaningless (C11 vs C++11), so a requirement
* from another family never counts as exceeding this baseline; `modes` is
* what keeps such a pairing from arising in the first place.
*/
private static exceedsStandard(
requirement: IToolchainRequirement,
baseline: IToolchainRequirement,
): boolean {
if (
LANGUAGE_STANDARD_FAMILY[requirement.standard] !==
LANGUAGE_STANDARD_FAMILY[baseline.standard]
) {
return false;
}
return (
LANGUAGE_STANDARD_ORDER[requirement.standard] >
LANGUAGE_STANDARD_ORDER[baseline.standard]
);
}
/**
* Does this requirement need a later language standard than the baseline?
*
* Distinct from isAboveBaseline, which is true if the requirement costs
* anything on any axis. A construct can need a newer standard *or* a
* compiler extension as a fallback -- designated initializers in C++ are
* C++20, but GCC and Clang accept them earlier -- and a report that shows
* only the extension tells a C++20 user they need something they do not.
*/
static exceedsBaselineStandard(
key: TRequirementKey,
mode: TOutputMode,
): boolean {
return ToolchainRequirementUtils.exceedsStandard(
TOOLCHAIN_REQUIREMENTS[key],
TOOLCHAIN_REQUIREMENTS[ToolchainRequirementUtils.baselineKey(mode)],
);
}
/**
* Extensions the toolchain needs **whatever target is selected**.
*
* An extension belonging to a requirement that also names a platform library
* lives inside one arm of a `#if` chain, so only that arm's targets compile
* it. Listing it unconditionally tells an AVR user they need ARM inline
* assembly for code their build never sees.
*
* One home for the rule, because the banner and the transpile-time report
* are the same question asked twice: CLAUDE.md's single-source-of-truth rule
* is about the *decision*, not just the data behind it.
*/
static unconditionalExtensions(
reportable: readonly IRecordedRequirement[],
): readonly TCompilerExtension[] {
const extensions = new Set<TCompilerExtension>();
for (const entry of reportable) {
const requirement = TOOLCHAIN_REQUIREMENTS[entry.key];
if (requirement.platformLib !== null) continue;
for (const extension of requirement.extensions) extensions.add(extension);
}
return Array.from(extensions);
}
/**
* Distinct compiler-version floors across the recorded set, for the guard
* emitter. Returns an empty array when nothing recorded carries a floor,
* which is the current state of the registry.
*/
static distinctCompilerFloors(
recorded: readonly IRecordedRequirement[],
): readonly ICompilerFloor[] {
const seen = new Map<string, ICompilerFloor>();
for (const entry of recorded) {
const floor = TOOLCHAIN_REQUIREMENTS[entry.key].compiler;
Iif (floor !== null) seen.set(floor.guardExpression, floor);
}
return Array.from(seen.values());
}
/**
* The `Requires:` lines for a generated file's banner.
*
* Empty when nothing exceeds the baseline, so plain C99 output keeps the
* banner it has always had. Sibling arms of one feature are collapsed into a
* single "one of" line: a critical section needs ARMv7-M *or* Arduino *or*
* avr-libc *or* CMSIS, and listing four requirements would misstate what the
* reader has to provide.
*/
static describeForBanner(
recorded: readonly IRecordedRequirement[],
mode: TOutputMode,
): readonly string[] {
const reportable = ToolchainRequirementUtils.reportable(recorded, mode);
if (reportable.length === 0) return [];
const standards = new Set<string>();
const extensions = new Set<string>(
ToolchainRequirementUtils.unconditionalExtensions(reportable),
);
const platformsByFeature = new Map<string, Set<string>>();
for (const entry of reportable) {
const requirement = TOOLCHAIN_REQUIREMENTS[entry.key];
if (ToolchainRequirementUtils.exceedsBaselineStandard(entry.key, mode)) {
standards.add(requirement.standard);
}
if (requirement.platformLib !== null) {
const libraries =
platformsByFeature.get(requirement.feature) ?? new Set<string>();
libraries.add(requirement.platformLib);
platformsByFeature.set(requirement.feature, libraries);
}
}
const parts: string[] = [];
const baseline =
TOOLCHAIN_REQUIREMENTS[ToolchainRequirementUtils.baselineKey(mode)]
.standard;
// The highest standard subsumes the baseline, so listing both would read as
// needing two standards at once. Seeded with the baseline rather than
// relying on the array being non-empty.
const highest = Array.from(standards).reduce(
(left, right) =>
LANGUAGE_STANDARD_ORDER[right as TLanguageStandard] >
LANGUAGE_STANDARD_ORDER[left as TLanguageStandard]
? right
: left,
baseline as string,
);
parts.push(`Requires: ${highest}.`);
if (extensions.size > 0) {
parts.push(`GNU/Clang extensions: ${Array.from(extensions).join(", ")}.`);
}
for (const [feature, libraries] of platformsByFeature) {
const list = Array.from(libraries);
parts.push(
list.length === 1
? `${feature} requires ${list[0]}.`
: `${feature} requires one of: ${list.join(", ")} (by target).`,
);
}
return parts;
}
/**
* Recorded requirements worth showing the user, sorted for stable output.
* Ordering is by feature then key so the report and the generated docs list
* sibling platform arms together.
*/
static reportable(
recorded: readonly IRecordedRequirement[],
mode: TOutputMode,
): readonly IRecordedRequirement[] {
return recorded
.filter((entry) =>
ToolchainRequirementUtils.isAboveBaseline(entry.key, mode),
)
.slice()
.sort((left, right) => {
const leftRequirement = TOOLCHAIN_REQUIREMENTS[left.key];
const rightRequirement = TOOLCHAIN_REQUIREMENTS[right.key];
const byFeature = leftRequirement.feature.localeCompare(
rightRequirement.feature,
);
return byFeature !== 0 ? byFeature : left.key.localeCompare(right.key);
});
}
}
export default ToolchainRequirementUtils;
|