Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
631 changes: 334 additions & 297 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"url": "https://github.com/robertoraggi/cplusplus/issues"
},
"devDependencies": {
"@types/node": "^24.0.13",
"jsonc-cli": "^1.0.2",
"zx": "^8.6.1"
},
Expand Down
60 changes: 0 additions & 60 deletions scripts/update-predefined-macros.mjs

This file was deleted.

120 changes: 120 additions & 0 deletions scripts/update-toolchain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import { execFile } from "node:child_process";

const machines = ["aarch64", "x86_64", "wasm32"] as const;
const oses = ["linux", "macosx", "windows", "wasi"] as const;
const compilers = ["c23", "c++26"];

type Machine = (typeof machines)[number];
type OS = (typeof oses)[number];
type Compiler = (typeof compilers)[number];
type Config = [machine: Machine, os: OS, compiler: Compiler];

const configs: Config[] = [
["aarch64", "linux", "c++26"],
["aarch64", "linux", "c23"],
["aarch64", "macosx", "c++26"],
["aarch64", "macosx", "c23"],
["aarch64", "windows", "c++26"],
["aarch64", "windows", "c23"],
["wasm32", "wasi", "c++26"],
["wasm32", "wasi", "c23"],
["x86_64", "linux", "c++26"],
["x86_64", "linux", "c23"],
["x86_64", "macosx", "c++26"],
["x86_64", "macosx", "c23"],
["x86_64", "windows", "c++26"],
["x86_64", "windows", "c23"],
];

function configMatches({
config,
machine,
os,
compiler,
}: {
config: Config;
machine?: Machine;
os?: OS;
compiler?: Compiler;
}): boolean {
const [configMachine, configOS, configCompiler] = config;
return (
(machine ? configMachine === machine : true) &&
(os ? configOS === os : true) &&
(compiler ? configCompiler === compiler : true)
);
}

async function getPredefinedMacros({
config,
}: {
config: Config;
}): Promise<Set<string>> {
const [machine, os, compiler] = config;

const lang = compiler.startsWith("c++") ? "c++" : "c";
const target = `${machine}-${os}`;

return new Promise((resolve, reject) => {
execFile(
"clang",
[
`--target=${target}`,
"-E",
"-dM",
`-x${lang}`,
`-std=${compiler}`,
"/dev/null",
],
(err, stdout) => {
if (err) {
reject(err);
return;
}

const predefinedMacros = stdout
.split("\n")
.map((line) => line.trim())
.filter(Boolean);

resolve(new Set(predefinedMacros));
}
);
});
}

async function main() {
const predefinedMacrosByConfig: Record<string, Set<string>> = {};
for (const config of configs) {
const predefinedMacros = await getPredefinedMacros({ config });
predefinedMacrosByConfig[config.join("-")] = predefinedMacros;
console.log(
`Config: ${config.join("-")} has ${predefinedMacros.size} predefined macros.`
);
}

machines.forEach((machine) => {
const machineConfigs = configs.filter((config) =>
configMatches({ config, machine })
);
const predefinedMacrosForMachine = machineConfigs.map((config) => {
return predefinedMacrosByConfig[config.join("-")];
});
return intersection(...predefinedMacrosForMachine);
});
}

function intersection<T>(...sets: Set<T>[]): Set<T> {
if (sets.length === 0) return new Set();
return sets.reduce((acc, set) => {
const intersection = new Set<T>();
for (const item of set) {
if (acc.has(item)) {
intersection.add(item);
}
}
return intersection;
}, sets[0]);
}

await main();
11 changes: 8 additions & 3 deletions src/frontend/cxx/frontend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,13 @@ auto readAll(const std::string& fileName) -> std::optional<std::string> {
}

void dumpTokens(const CLI& cli, TranslationUnit& unit, std::ostream& output) {
const auto lang =
cli.getSingle("-x") == "c" ? LanguageKind::kC : LanguageKind::kCXX;
auto lang = LanguageKind::kCXX;

if (auto x = cli.getSingle("x")) {
if (x == "c") lang = LanguageKind::kC;
} else if (unit.fileName().ends_with(".c")) {
lang = LanguageKind::kC;
}

std::string flags;

Expand Down Expand Up @@ -158,7 +163,7 @@ auto runOnFile(const CLI& cli, const std::string& fileName) -> bool {

const auto lang = cli.getSingle("-x");

if (lang == "c") {
if (lang == "c" || (!lang.has_value() && fileName.ends_with(".c"))) {
// set the language to C
preprocessor->setLanguage(LanguageKind::kC);
}
Expand Down
Loading