GetInterfacesAndImplementations found the module declarations and the imports of a configuration's sources by searching their text with regular expressions, after deleting the comments that fit on one line. The expressions skipped neither string literals nor comments that span lines, knew nothing of the preprocessor, and wanted one space after `import` and none before its semicolon (#45). An implementation source that held "export module std;" in a string literal was taken for a unit of a module std, and listing it threw "Module std not found not found". An import in a comment that spans lines or under #if 0 was followed. `import X;` and `import X ;` were missed, so their unit had no edge to the module it imports and could be compiled before that module's BMI was written. What a .cppm or .cpp declares and imports now comes from clang-scan-deps -format=p1689, which preprocesses the source with the configuration's C++ compile command, the one Build() compiles every unit of it with: strings, comments, spacing, macros and #if come out as they do for the compiler. GetInterfacesAndImplementations only lists the sources, in the new interfaceSources and implementationSources. Build() reads them (ReadSources) once the configuration's dependencies and external dependencies are built: the command takes their include paths, and a source may include headers of an external dependency, which are not there before it is fetched. From what it read, Build() makes the configuration's modules and units, its `interfaces` and `implementations`, and places each import on a module of the configuration or of what it is built on, whose own Build() has read its sources by then. It makes those of its own configuration alone, as the reset of the units' state did before it. A build pass builds a directory once, for whichever configuration of it asks first, and the loader can make two configurations of one directory: one project loaded along two paths with its args in two orders, which it keeps apart and VariantId, sorting the args, does not. Only the one that was built has the modules that build read, so the result of a build now names the configuration it ran on (BuildResult::built), and a dependency's modules are looked up through the pass's record of its directory. 6.4.1 had every configuration's modules from the listing and took such a graph for one with two builds of a module, which it refused; it is now built, once, and the units of both sides have their edges. A scan is kept in the build directory as an object is, in scans/ or, for a source outside cfg.path, in scans-outside/, as JSON of the command it was taken with and of what clang-scan-deps wrote, with the dependency file clang-scan-deps wrote beside it, which lists the source and every file it included. A build scans a source again unless its scan is newer than every file that dependency file lists, was taken with the same command and can be read, so a build with nothing changed runs no clang-scan-deps. Both files are written beside their places and renamed into them, as the archive and the list of objects are, for another crafter-build that may be building the same clone. A configuration's modules are known only once its build has read its sources, so the refusals of a module that two configurations of the graph build (DuplicateModuleError, #41) and of interface units that import each other (ImportCycleError) now come there: once the dependencies are built, before any .cppm or .cpp of the configuration is compiled. They came before the configuration made a directory or built a dependency. The contributor guide names clang-scan-deps among the prerequisites and among what :Toolchain resolves, has a test look at the modules of a configuration only after building it, and has the gotcha for the new order. ResolvePendingImports and the pendingImports of the unit classes go. They placed the imports that a scan at listing time could not, once the dependencies were wired up, and with the imports placed when the dependencies are built there is nothing pending. The tests that looked at a configuration's modules and units before building now look after it (DependencyLink, ModuleInterface, StaticLib, ConcurrentDependencyReset and the disabled TransitiveInterfaceChange), IncrementalInterfaceChange checks the edge that the build of a consumer gives it when its dependency was assigned after its sources were listed, and ImportCycle and DuplicateModule check that the refused configuration compiled nothing of its own. The workarounds for the old scanner are not needed: MingwSysroot writes its stand-in std module as a plain literal again, JobLimit writes the declarations of the sources it generates as plain literals, and Lint's comment on its fixture file no longer gives the scanner as the reason for it. The messages of the regex code stay where they still apply. The one for a source that declares a unit of a module the configuration does not have loses its doubled "not found", and applies to an implementation source that declares one, such as an implementation partition (`module X:P;`). An implementation unit written `module X;` declares no unit for clang-scan-deps but imports X, and an import needs no module of the configuration: the compile reports a module no one builds. A .cppm that declares an implementation partition is built as a partition of its module, where it was refused as declaring no module. The Toolchain gains clangScanDeps, resolved as llvm-ar is: in the bin/ of the LLVM prefix, else by name on PATH. The Flatpak has /app/bin/clang-scan-deps. Fixes #45
799 lines
40 KiB
C++
799 lines
40 KiB
C++
// SPDX-License-Identifier: LGPL-3.0-only
|
|
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
|
|
|
import std;
|
|
import Crafter.Build;
|
|
namespace fs = std::filesystem;
|
|
using namespace Crafter;
|
|
|
|
extern "C" int setenv(const char* name, const char* value, int overwrite);
|
|
extern "C" int unsetenv(const char* name);
|
|
|
|
namespace {
|
|
std::int32_t Failures = 0;
|
|
|
|
void Check(bool cond, std::string_view msg) {
|
|
if (!cond) {
|
|
std::println(std::cerr, "FAIL: {}", msg);
|
|
++Failures;
|
|
}
|
|
}
|
|
|
|
// Scratch-dir fixture for transform tests: Apply mode rewrites files, so
|
|
// these cases must never point at the checked-in fixture/. Each case
|
|
// writes its own sources into a fresh temp dir.
|
|
struct Scratch {
|
|
fs::path dir;
|
|
explicit Scratch(std::string_view name) {
|
|
dir = fs::temp_directory_path() / "crafter-build-lint-format" / name;
|
|
fs::remove_all(dir);
|
|
fs::create_directories(dir);
|
|
}
|
|
void Write(std::string_view stem, std::string_view content) const {
|
|
std::ofstream f(dir / std::format("{}.cpp", stem), std::ios::binary | std::ios::trunc);
|
|
f.write(content.data(), static_cast<std::streamsize>(content.size()));
|
|
}
|
|
std::string Read(std::string_view stem) const {
|
|
std::ifstream f(dir / std::format("{}.cpp", stem), std::ios::binary);
|
|
std::stringstream buffer;
|
|
buffer << f.rdbuf();
|
|
return std::move(buffer).str();
|
|
}
|
|
Configuration Config(std::vector<fs::path> impls) const {
|
|
Configuration cfg;
|
|
cfg.path = dir;
|
|
cfg.name = "fmt-fixture";
|
|
cfg.outputName = "fmt-fixture";
|
|
cfg.target = HostTarget();
|
|
std::array<fs::path, 0> ifaces = {};
|
|
cfg.GetInterfacesAndImplementations(ifaces, impls);
|
|
return cfg;
|
|
}
|
|
};
|
|
|
|
void AddTrimRule(Configuration& cfg) {
|
|
cfg.AddLintRule("trim", [](LintContext& ctx) {
|
|
std::string out;
|
|
out.reserve(ctx.content.size());
|
|
for (std::size_t n = 1; n <= ctx.lines.size(); ++n) {
|
|
std::string_view line = ctx.Line(n);
|
|
const bool crlf = line.ends_with('\r');
|
|
if (crlf) line.remove_suffix(1);
|
|
while (line.ends_with(' ') || line.ends_with('\t')) line.remove_suffix(1);
|
|
out += line;
|
|
if (crlf) out += '\r';
|
|
if (n < ctx.lines.size() || ctx.content.ends_with('\n')) out += '\n';
|
|
}
|
|
ctx.SetContent(std::move(out));
|
|
});
|
|
}
|
|
|
|
RunLintOptions Mode(LintMode m) {
|
|
RunLintOptions opts;
|
|
opts.mode = m;
|
|
return opts;
|
|
}
|
|
|
|
// Fresh Configuration over the two fixture sources. Rebuilt per case so
|
|
// rule registrations don't leak between them.
|
|
Configuration FixtureConfig() {
|
|
Configuration cfg;
|
|
cfg.path = fs::current_path() / "tests" / "Lint" / "fixture";
|
|
cfg.name = "lint-fixture";
|
|
cfg.outputName = "lint-fixture";
|
|
cfg.target = HostTarget();
|
|
std::array<fs::path, 0> ifaces = {};
|
|
std::array<fs::path, 2> impls = { "clean", "dirty" };
|
|
cfg.GetInterfacesAndImplementations(ifaces, impls);
|
|
return cfg;
|
|
}
|
|
|
|
void AddTabRule(Configuration& cfg) {
|
|
cfg.AddLintRule("tab-rule", [](LintContext& ctx) {
|
|
for (std::size_t n = 1; n <= ctx.lines.size(); ++n) {
|
|
if (ctx.Line(n).contains('\t')) ctx.Report(n, "tab character");
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
// In-process tests for RunLint: rule registration, file collection over a
|
|
// Configuration, glob filtering, --list, comment stripping, and dedup. Lint
|
|
// only reads sources, so no Build() is needed.
|
|
int main() {
|
|
// No rules registered → noRulesDefined, not Clean.
|
|
{
|
|
Configuration cfg = FixtureConfig();
|
|
LintSummary s = RunLint(cfg, {});
|
|
Check(s.noRulesDefined, "no rules -> noRulesDefined");
|
|
Check(!s.Clean(), "no rules -> not Clean (exit 1)");
|
|
Check(s.findings.empty(), "no rules -> no findings");
|
|
}
|
|
|
|
// tab-rule fires on dirty.cpp line 9 only; never-fires stays silent.
|
|
{
|
|
Configuration cfg = FixtureConfig();
|
|
AddTabRule(cfg);
|
|
cfg.AddLintRule("never-fires", [](LintContext&) {});
|
|
LintSummary s = RunLint(cfg, {});
|
|
Check(s.rulesRun == 2, "both rules run");
|
|
Check(s.filesLinted == 2, "both fixture files linted");
|
|
Check(s.findings.size() == 1, "exactly one finding");
|
|
Check(!s.Clean(), "findings -> not Clean");
|
|
if (!s.findings.empty()) {
|
|
Check(s.findings[0].file.filename() == "dirty.cpp", "finding is in dirty.cpp");
|
|
Check(s.findings[0].line == 9, "tab reported at line 9");
|
|
Check(s.findings[0].rule == "tab-rule", "finding attributed to tab-rule");
|
|
}
|
|
}
|
|
|
|
// Glob filter drops tab-rule → clean run.
|
|
{
|
|
Configuration cfg = FixtureConfig();
|
|
AddTabRule(cfg);
|
|
cfg.AddLintRule("never-fires", [](LintContext&) {});
|
|
RunLintOptions opts;
|
|
opts.globs = { "never-*" };
|
|
LintSummary s = RunLint(cfg, opts);
|
|
Check(s.rulesRun == 1, "glob filters to one rule");
|
|
Check(s.findings.empty(), "filtered run has no findings");
|
|
Check(s.Clean(), "filtered run is Clean");
|
|
}
|
|
|
|
// listOnly enumerates without running any rule.
|
|
{
|
|
Configuration cfg = FixtureConfig();
|
|
AddTabRule(cfg);
|
|
RunLintOptions opts;
|
|
opts.listOnly = true;
|
|
LintSummary s = RunLint(cfg, opts);
|
|
Check(s.findings.empty(), "listOnly records no findings");
|
|
Check(s.filesLinted == 0, "listOnly reads no files");
|
|
Check(s.rulesRun == 1, "listOnly still counts matching rules");
|
|
}
|
|
|
|
// CommentStripped: MARKER in a comment (line 7) and in a string literal
|
|
// (line 12) are blanked; the identifier at line 13 survives, and line
|
|
// numbers computed from the stripped text match the real file.
|
|
{
|
|
Configuration cfg = FixtureConfig();
|
|
cfg.AddLintRule("marker", [](LintContext& ctx) {
|
|
const std::string& code = ctx.CommentStripped();
|
|
for (std::size_t pos = code.find("MARKER"); pos != std::string::npos; pos = code.find("MARKER", pos + 1)) {
|
|
std::size_t line = 1 + std::count(code.begin(), code.begin() + pos, '\n');
|
|
ctx.Report(line, "MARKER in code");
|
|
}
|
|
});
|
|
LintSummary s = RunLint(cfg, {});
|
|
Check(s.findings.size() == 1, "only the code MARKER is found");
|
|
if (!s.findings.empty()) {
|
|
Check(s.findings[0].line == 13, "code MARKER reported at line 13");
|
|
Check(s.findings[0].file.filename() == "dirty.cpp", "MARKER finding is in dirty.cpp");
|
|
}
|
|
}
|
|
|
|
// Duplicate rule name: first registration wins, second never runs.
|
|
{
|
|
Configuration cfg = FixtureConfig();
|
|
AddTabRule(cfg);
|
|
cfg.AddLintRule("tab-rule", [](LintContext& ctx) {
|
|
ctx.Report(1, "duplicate ran");
|
|
});
|
|
LintSummary s = RunLint(cfg, {});
|
|
Check(s.rulesRun == 1, "duplicate name deduplicated");
|
|
Check(s.findings.size() == 1 && s.findings[0].line == 9, "only the first registration ran");
|
|
}
|
|
|
|
// A throwing rule surfaces as a finding, not an unwind.
|
|
{
|
|
Configuration cfg = FixtureConfig();
|
|
cfg.AddLintRule("throws", [](LintContext& ctx) {
|
|
if (ctx.file.filename() == "clean.cpp") throw std::runtime_error("boom");
|
|
});
|
|
LintSummary s = RunLint(cfg, {});
|
|
Check(s.findings.size() == 1, "exception becomes a finding");
|
|
if (!s.findings.empty()) {
|
|
Check(s.findings[0].message.contains("boom"), "finding carries the exception message");
|
|
Check(s.findings[0].line == 0, "exception finding is whole-file (line 0)");
|
|
}
|
|
}
|
|
|
|
// --- Transform (format) cases: scratch dir, never the checked-in fixture ---
|
|
|
|
// Report mode: transform diffs become would-reformat findings; disk untouched.
|
|
{
|
|
Scratch s("report");
|
|
s.Write("a", "hello \nworld\n");
|
|
Configuration cfg = s.Config({"a"});
|
|
AddTrimRule(cfg);
|
|
LintSummary sum = RunLint(cfg, Mode(LintMode::Report));
|
|
Check(sum.findings.size() == 1, "one would-reformat finding");
|
|
if (!sum.findings.empty()) {
|
|
Check(sum.findings[0].line == 1, "would-reformat at line 1");
|
|
Check(sum.findings[0].rule == "trim", "attributed to the transform rule");
|
|
Check(sum.findings[0].message == "would reformat", "derived message");
|
|
}
|
|
Check(sum.changedFiles.size() == 1, "changedFiles populated in Report mode");
|
|
Check(!sum.Clean(), "would-reformat gates lint");
|
|
Check(s.Read("a") == "hello \nworld\n", "Report mode never writes");
|
|
}
|
|
|
|
// Apply mode: disk rewritten; a sibling report-only rule's findings are
|
|
// still recorded (the verb, not the driver, ignores them).
|
|
{
|
|
Scratch s("apply");
|
|
s.Write("a", "hello \nworld\n");
|
|
Configuration cfg = s.Config({"a"});
|
|
AddTrimRule(cfg);
|
|
cfg.AddLintRule("note", [](LintContext& ctx) { ctx.Report(2, "note"); });
|
|
LintSummary sum = RunLint(cfg, Mode(LintMode::Apply));
|
|
Check(s.Read("a") == "hello\nworld\n", "Apply rewrites the file");
|
|
Check(sum.changedFiles.size() == 1, "one file formatted");
|
|
const bool hasNote = std::any_of(sum.findings.begin(), sum.findings.end(), [](const LintFinding& f) { return f.rule == "note"; });
|
|
Check(hasNote, "report-only findings still recorded in Apply mode");
|
|
Check(sum.errors == 0, "clean apply has no errors");
|
|
}
|
|
|
|
// Check mode: reported, not written.
|
|
{
|
|
Scratch s("check");
|
|
s.Write("a", "hello \n");
|
|
Configuration cfg = s.Config({"a"});
|
|
AddTrimRule(cfg);
|
|
LintSummary sum = RunLint(cfg, Mode(LintMode::Check));
|
|
Check(sum.changedFiles.size() == 1, "Check records would-change file");
|
|
Check(!sum.findings.empty(), "Check records would-reformat findings");
|
|
Check(s.Read("a") == "hello \n", "Check mode never writes");
|
|
}
|
|
|
|
// The configurations a project file returns: one run over the rules and
|
|
// the sources of all of them. The second lies inside the first's
|
|
// directory, which is the project root when no project file is given.
|
|
{
|
|
Scratch first("several");
|
|
Scratch second("several/second");
|
|
first.Write("a", "one \n");
|
|
second.Write("b", "two \n");
|
|
Configuration one = first.Config({"a"});
|
|
Configuration two = second.Config({"b"});
|
|
AddTrimRule(one);
|
|
AddTrimRule(two);
|
|
std::int32_t noted = 0;
|
|
two.AddLintRule("note", [¬ed](LintContext&) { ++noted; });
|
|
std::array<Configuration*, 2> both = { &one, &two };
|
|
LintSummary sum = RunLint(both, Mode(LintMode::Apply));
|
|
Check(first.Read("a") == "one\n" && second.Read("b") == "two\n", "several: the sources of every configuration are formatted");
|
|
Check(sum.rulesRun == 2, "several: a rule two configurations register under one name is one rule");
|
|
Check(noted == 2, "several: a rule one configuration registers runs over the sources of all");
|
|
}
|
|
|
|
// A rule two of the returned configurations register under one name is
|
|
// the one the first of them registered, in the order the file returned
|
|
// them, whatever depends on the other: app depends on tools, so a walk
|
|
// of one configuration's graph after another's meets tools before
|
|
// `other` and would run its rule.
|
|
{
|
|
Scratch appDir("order");
|
|
Scratch otherDir("order/other");
|
|
Scratch toolsDir("order/tools");
|
|
appDir.Write("a", "app\n");
|
|
otherDir.Write("b", "other\n");
|
|
toolsDir.Write("c", "tools\n");
|
|
Configuration app = appDir.Config({"a"});
|
|
Configuration other = otherDir.Config({"b"});
|
|
Configuration tools = toolsDir.Config({"c"});
|
|
app.dependencies = { &tools };
|
|
std::atomic<std::int32_t> otherRan = 0;
|
|
std::atomic<std::int32_t> toolsRan = 0;
|
|
other.AddLintRule("pick", [&otherRan](LintContext&) { ++otherRan; });
|
|
tools.AddLintRule("pick", [&toolsRan](LintContext&) { ++toolsRan; });
|
|
std::array<Configuration*, 3> roots = { &app, &other, &tools };
|
|
const LintSummary sum = RunLint(roots, Mode(LintMode::Report));
|
|
Check(sum.rulesRun == 1 && otherRan == 3 && toolsRan == 0, std::format("several: the rule of the first configuration that registers the name runs, over every source (rules {}, the first's {} times, the later one's {} times)", sum.rulesRun, otherRan.load(), toolsRan.load()));
|
|
}
|
|
|
|
// Chaining: rule2 sees rule1's output; both attributed in Report mode.
|
|
{
|
|
Scratch s("chain");
|
|
s.Write("a", "AAA\n");
|
|
Configuration cfg = s.Config({"a"});
|
|
cfg.AddLintRule("one", [](LintContext& ctx) {
|
|
std::string c = ctx.content;
|
|
if (auto p = c.find("AAA"); p != std::string::npos) c.replace(p, 3, "BBB");
|
|
ctx.SetContent(std::move(c));
|
|
});
|
|
cfg.AddLintRule("two", [](LintContext& ctx) {
|
|
std::string c = ctx.content;
|
|
if (auto p = c.find("BBB"); p != std::string::npos) c.replace(p, 3, "CCC");
|
|
ctx.SetContent(std::move(c));
|
|
});
|
|
LintSummary rep = RunLint(cfg, Mode(LintMode::Report));
|
|
const bool one = std::any_of(rep.findings.begin(), rep.findings.end(), [](const LintFinding& f) { return f.rule == "one"; });
|
|
const bool two = std::any_of(rep.findings.begin(), rep.findings.end(), [](const LintFinding& f) { return f.rule == "two"; });
|
|
Check(one && two, "chained transforms both attributed");
|
|
Check(s.Read("a") == "AAA\n", "Report leaves chain input untouched");
|
|
LintSummary app = RunLint(cfg, Mode(LintMode::Apply));
|
|
Check(s.Read("a") == "CCC\n", "Apply composes chained transforms");
|
|
Check(app.changedFiles.size() == 1, "chain counts as one changed file");
|
|
}
|
|
|
|
// A throwing transform is reverted — half-applied content never lands.
|
|
{
|
|
Scratch s("throws");
|
|
s.Write("a", "keep\n");
|
|
Configuration cfg = s.Config({"a"});
|
|
cfg.AddLintRule("bad", [](LintContext& ctx) {
|
|
ctx.SetContent("garbage");
|
|
throw std::runtime_error("mid-transform");
|
|
});
|
|
LintSummary sum = RunLint(cfg, Mode(LintMode::Apply));
|
|
Check(s.Read("a") == "keep\n", "throwing transform reverted, disk untouched");
|
|
Check(sum.errors == 1, "exception counted as error");
|
|
Check(sum.changedFiles.empty(), "reverted transform is not a change");
|
|
const bool threw = std::any_of(sum.findings.begin(), sum.findings.end(),
|
|
[](const LintFinding& f) {
|
|
return f.line == 0 && f.message.contains("mid-transform");
|
|
});
|
|
Check(threw, "exception surfaced as line-0 finding");
|
|
}
|
|
|
|
// CRLF byte fidelity + the final-newline whole-file (line 0) diff edge.
|
|
{
|
|
Scratch s("bytes");
|
|
s.Write("crlf", "x \r\ny\r\n");
|
|
s.Write("noeol", "a\nb");
|
|
Configuration cfg = s.Config({"crlf", "noeol"});
|
|
AddTrimRule(cfg);
|
|
cfg.AddLintRule("final-newline", [](LintContext& ctx) {
|
|
if (!ctx.content.empty() && !ctx.content.ends_with('\n')) {
|
|
ctx.SetContent(ctx.content + '\n');
|
|
}
|
|
});
|
|
LintSummary rep = RunLint(cfg, Mode(LintMode::Report));
|
|
const bool wholeFile = std::any_of(rep.findings.begin(), rep.findings.end(),
|
|
[](const LintFinding& f) {
|
|
return f.rule == "final-newline" && f.line == 0;
|
|
});
|
|
Check(wholeFile, "missing final newline reports as whole-file finding");
|
|
RunLint(cfg, Mode(LintMode::Apply));
|
|
Check(s.Read("crlf") == "x\r\ny\r\n", "trim preserves CRLF endings");
|
|
Check(s.Read("noeol") == "a\nb\n", "final-newline appends exactly one newline");
|
|
}
|
|
|
|
// Mixed rule: Report() and SetContent() from the same rule.
|
|
{
|
|
Scratch s("mixed");
|
|
s.Write("a", "bad \n");
|
|
Configuration cfg = s.Config({"a"});
|
|
cfg.AddLintRule("mixed", [](LintContext& ctx) {
|
|
ctx.Report(1, "flagged");
|
|
std::string c = ctx.content;
|
|
if (auto p = c.find(' '); p != std::string::npos) c.erase(p, 1);
|
|
ctx.SetContent(std::move(c));
|
|
});
|
|
LintSummary rep = RunLint(cfg, Mode(LintMode::Report));
|
|
const bool flagged = std::any_of(rep.findings.begin(), rep.findings.end(), [](const LintFinding& f) { return f.message == "flagged"; });
|
|
const bool reformat = std::any_of(rep.findings.begin(), rep.findings.end(), [](const LintFinding& f) { return f.message == "would reformat"; });
|
|
Check(flagged && reformat, "mixed rule records both finding kinds");
|
|
RunLint(cfg, Mode(LintMode::Apply));
|
|
Check(s.Read("a") == "bad\n", "mixed rule's transform applied");
|
|
}
|
|
|
|
// Idempotency: a second Apply is a no-op; identical SetContent bytes are
|
|
// not a change (compare-by-value, not call-tracking).
|
|
{
|
|
Scratch s("idempotent");
|
|
s.Write("a", "hello \n");
|
|
Configuration cfg = s.Config({"a"});
|
|
AddTrimRule(cfg);
|
|
LintSummary first = RunLint(cfg, Mode(LintMode::Apply));
|
|
Check(first.changedFiles.size() == 1, "first apply changes the file");
|
|
LintSummary second = RunLint(cfg, Mode(LintMode::Apply));
|
|
Check(second.changedFiles.empty(), "second apply is a no-op");
|
|
Check(second.findings.empty(), "no-op apply has no findings");
|
|
}
|
|
|
|
// --- Suppression directives (engine-level) ---
|
|
|
|
// next-line directive with a rule name suppresses that finding only.
|
|
{
|
|
Scratch s("suppress-next-line");
|
|
s.Write("a", "// lint-disable-next-line flag\nbad\nbad\n");
|
|
Configuration cfg = s.Config({"a"});
|
|
cfg.AddLintRule("flag", [](LintContext& ctx) {
|
|
for (std::size_t n = 1; n <= ctx.lines.size(); ++n) {
|
|
if (ctx.Line(n).contains("bad")) ctx.Report(n, "bad");
|
|
}
|
|
});
|
|
LintSummary sum = RunLint(cfg, Mode(LintMode::Report));
|
|
Check(sum.findings.size() == 1, "next-line directive suppresses one finding");
|
|
if (!sum.findings.empty()) Check(sum.findings[0].line == 3, "the unsuppressed line still reports");
|
|
}
|
|
|
|
// next-line suppression also reverts a transform's edit on that line —
|
|
// `format` must not rewrite what lint is told to ignore.
|
|
{
|
|
Scratch s("suppress-transform");
|
|
s.Write("a", "// lint-disable-next-line trim\nkeep \ntrim \n");
|
|
Configuration cfg = s.Config({"a"});
|
|
AddTrimRule(cfg);
|
|
RunLint(cfg, Mode(LintMode::Apply));
|
|
Check(s.Read("a") == "// lint-disable-next-line trim\nkeep \ntrim\n", "suppressed line keeps its bytes; the unsuppressed one is fixed");
|
|
}
|
|
|
|
// Multiple rule names on one directive (space- or comma-separated).
|
|
{
|
|
Scratch s("suppress-multi");
|
|
s.Write("a", "// lint-disable-next-line flagA, flagB\nbad \nbad \n");
|
|
Configuration cfg = s.Config({"a"});
|
|
cfg.AddLintRule("flagA", [](LintContext& ctx) {
|
|
for (std::size_t n = 1; n <= ctx.lines.size(); ++n) {
|
|
if (ctx.Line(n).contains("bad")) ctx.Report(n, "A");
|
|
}
|
|
});
|
|
cfg.AddLintRule("flagB", [](LintContext& ctx) {
|
|
for (std::size_t n = 1; n <= ctx.lines.size(); ++n) {
|
|
if (ctx.Line(n).contains("bad")) ctx.Report(n, "B");
|
|
}
|
|
});
|
|
AddTrimRule(cfg);
|
|
LintSummary sum = RunLint(cfg, Mode(LintMode::Report));
|
|
const bool line2Silent = std::none_of(sum.findings.begin(), sum.findings.end(), [](const LintFinding& f) { return f.line == 2 && f.rule != "trim"; });
|
|
const bool line3Loud = std::count_if(sum.findings.begin(), sum.findings.end(), [](const LintFinding& f) { return f.line == 3; }) >= 2;
|
|
Check(line2Silent, "both named rules suppressed on the target line");
|
|
const bool trimStillFires = std::any_of(sum.findings.begin(), sum.findings.end(), [](const LintFinding& f) { return f.line == 2 && f.rule == "trim"; });
|
|
Check(trimStillFires, "unnamed rule still fires on the target line");
|
|
Check(line3Loud, "unsuppressed line reports from both rules");
|
|
}
|
|
|
|
// file-level all-rules directive silences findings and stops format.
|
|
{
|
|
Scratch s("suppress-file");
|
|
s.Write("a", "// lint-disable-file\nbad \n");
|
|
Configuration cfg = s.Config({"a"});
|
|
AddTrimRule(cfg);
|
|
cfg.AddLintRule("flag", [](LintContext& ctx) { ctx.Report(2, "bad"); });
|
|
LintSummary rep = RunLint(cfg, Mode(LintMode::Report));
|
|
Check(rep.findings.empty(), "file-level all directive suppresses every finding");
|
|
LintSummary app = RunLint(cfg, Mode(LintMode::Apply));
|
|
Check(app.changedFiles.empty(), "file-level all directive stops format");
|
|
Check(s.Read("a") == "// lint-disable-file\nbad \n", "file bytes untouched");
|
|
}
|
|
|
|
// file-level with a rule name: that rule is dead, others still act.
|
|
{
|
|
Scratch s("suppress-file-rule");
|
|
s.Write("a", "// lint-disable-file flag\nbad \n");
|
|
Configuration cfg = s.Config({"a"});
|
|
AddTrimRule(cfg);
|
|
cfg.AddLintRule("flag", [](LintContext& ctx) { ctx.Report(2, "bad"); });
|
|
LintSummary rep = RunLint(cfg, Mode(LintMode::Report));
|
|
const bool onlyTrim = !rep.findings.empty() && std::all_of(rep.findings.begin(), rep.findings.end(), [](const LintFinding& f) { return f.rule == "trim"; });
|
|
Check(onlyTrim, "file-level rule directive kills that rule, trim still fires");
|
|
RunLint(cfg, Mode(LintMode::Apply));
|
|
Check(s.Read("a") == "// lint-disable-file flag\nbad\n", "other rules still format");
|
|
}
|
|
|
|
// ---------------- token layer ----------------
|
|
//
|
|
// The source is spelled with escaped literals rather than a raw string so
|
|
// that this file stays lintable by the very rules under test; the scratch
|
|
// file it writes does contain a genuine multi-line raw string.
|
|
{
|
|
constexpr std::string_view Source =
|
|
"#ifdef CRAFTER_LINT_NEVER_DEFINED\n" // 1
|
|
"void HiddenBranch();\n" // 2
|
|
"#endif\n" // 3
|
|
"// a real comment\n" // 4
|
|
"int url = 1; // https://example.com\n" // 5
|
|
"auto raw = R\"raw(spans lines\n" // 6
|
|
" // not a comment\n" // 7
|
|
" int notADecl;\n" // 8
|
|
")raw\";\n"; // 9
|
|
|
|
Scratch s("tokens");
|
|
s.Write("f", Source);
|
|
Configuration cfg = s.Config({"f"});
|
|
cfg.AddLintRule("tokens", [](LintContext& ctx) {
|
|
std::span<const LintToken> toks = ctx.Tokens();
|
|
Check(!toks.empty(), "tokens: file lexes to a non-empty stream");
|
|
|
|
// Every token's offset/length must address its own bytes, or a
|
|
// transform editing at an offset would corrupt the file.
|
|
bool offsetsSound = true;
|
|
for (const LintToken& t : toks) {
|
|
if (t.offset + t.length > ctx.content.size() || ctx.TokenText(t).empty()) offsetsSound = false;
|
|
}
|
|
Check(offsetsSound, "tokens: every offset/length addresses real bytes");
|
|
|
|
// Ordered by offset, so binary search in TokensOnLine is valid.
|
|
const bool ordered = std::ranges::is_sorted(toks, {}, &LintToken::offset);
|
|
Check(ordered, "tokens: stream is in source order");
|
|
|
|
// Inactive #ifdef branch is still lexed — this is what keeps token
|
|
// rules covering every platform, unlike an AST.
|
|
const bool sawHidden = std::ranges::any_of(toks, [&](const LintToken& t) {
|
|
return t.kind == LintTokenKind::Identifier && ctx.TokenText(t) == "HiddenBranch";
|
|
});
|
|
Check(sawHidden, "tokens: inactive #ifdef branch is lexed");
|
|
|
|
// A multi-line raw string is exactly one Literal, comment markers
|
|
// and declarations inside it included.
|
|
auto isRaw = [&](const LintToken& t) { return ctx.TokenText(t).starts_with("R\"raw("); };
|
|
Check(std::ranges::count_if(toks, isRaw) == 1, "tokens: raw string is a single token");
|
|
auto raw = std::ranges::find_if(toks, isRaw);
|
|
if (raw != toks.end()) {
|
|
Check(raw->kind == LintTokenKind::Literal, "tokens: raw string is a Literal");
|
|
Check(raw->line == 6, "tokens: raw string starts on line 6");
|
|
Check(ctx.TokenText(*raw).contains("// not a comment"), "tokens: raw string body kept intact");
|
|
Check(ctx.TokenText(*raw).ends_with(")raw\""), "tokens: raw string spans to its own terminator");
|
|
}
|
|
|
|
// The only comments are the two real ones on lines 4 and 5 — the
|
|
// `//` on line 7 lives inside the raw string.
|
|
std::vector<std::size_t> commentLines;
|
|
for (const LintToken& t : toks) {
|
|
if (t.kind == LintTokenKind::Comment) commentLines.push_back(t.line);
|
|
}
|
|
Check(commentLines == std::vector<std::size_t>{4, 5}, "tokens: only real comments are Comment tokens");
|
|
Check(ctx.LineHasComment(4), "tokens: LineHasComment finds a whole-line comment");
|
|
Check(ctx.LineHasComment(5), "tokens: LineHasComment finds a trailing comment");
|
|
Check(!ctx.LineHasComment(7), "tokens: `//` inside a raw string is not a comment");
|
|
Check(!ctx.LineHasComment(2), "tokens: code-only line has no comment");
|
|
|
|
// TokensOnLine brackets by starting line.
|
|
std::span<const LintToken> line2 = ctx.TokensOnLine(2);
|
|
Check(!line2.empty() && ctx.TokenText(line2.front()) == "void", "tokens: TokensOnLine starts at the line's first token");
|
|
Check(std::ranges::all_of(line2, [](const LintToken& t) { return t.line == 2; }), "tokens: TokensOnLine stays on its line");
|
|
|
|
// SetContent must invalidate the cache, or offsets point into a
|
|
// buffer that no longer exists.
|
|
ctx.SetContent("int replaced;\n");
|
|
std::span<const LintToken> after = ctx.Tokens();
|
|
Check(!after.empty() && ctx.TokenText(after.front()) == "int", "tokens: re-lexed after SetContent");
|
|
Check(std::ranges::none_of(after, [&](const LintToken& t) { return ctx.TokenText(t) == "HiddenBranch"; }), "tokens: stale tokens are dropped after SetContent");
|
|
});
|
|
RunLint(cfg, Mode(LintMode::Report));
|
|
}
|
|
|
|
// CommentStripped over a raw string holding an ODD number of quotes. The
|
|
// character-scanning version treated R"( as an ordinary string open, so the
|
|
// quote inside the body closed it early and every following line was
|
|
// swallowed as literal text — code after the raw string vanished from the
|
|
// stripped view. Lexing gets the extent right.
|
|
{
|
|
constexpr std::string_view Source =
|
|
"auto banner = R\"(he said \"hi)\";\n" // 1: one quote inside the body
|
|
"int afterRaw = 2;\n" // 2: must survive as code
|
|
"// MARKER comment\n" // 3
|
|
"auto plain = \"MARKER text\";\n"; // 4
|
|
|
|
Scratch s("strip-rawstring");
|
|
s.Write("f", Source);
|
|
Configuration cfg = s.Config({"f"});
|
|
cfg.AddLintRule("strip", [](LintContext& ctx) {
|
|
const std::string& code = ctx.CommentStripped();
|
|
Check(code.size() == ctx.content.size(), "strip: byte length preserved");
|
|
Check(std::ranges::count(code, '\n') == std::ranges::count(ctx.content, '\n'), "strip: newlines preserved");
|
|
Check(code.contains("afterRaw"), "strip: code after an odd-quoted raw string survives");
|
|
Check(!code.contains("he said"), "strip: raw string body is blanked");
|
|
Check(!code.contains("MARKER"), "strip: comment and literal bodies are blanked");
|
|
Check(code.contains("auto plain ="), "strip: code around a literal survives");
|
|
// The raw string collapses to R"…" — exactly two quotes, so rules
|
|
// that bracket a literal by counting quotes still work.
|
|
std::string_view line1 = std::string_view(code).substr(0, code.find('\n'));
|
|
Check(std::ranges::count(line1, '"') == 2, "strip: raw string leaves exactly two quotes");
|
|
});
|
|
RunLint(cfg, Mode(LintMode::Report));
|
|
}
|
|
|
|
// Non-C++ extensions are not lexed: GLSL through a C++ lexer would produce
|
|
// plausible-looking nonsense rather than an honest refusal.
|
|
{
|
|
Scratch s("tokens-foreign");
|
|
fs::path shader = s.dir / "f.frag";
|
|
{
|
|
std::ofstream f(shader, std::ios::binary | std::ios::trunc);
|
|
f << "#version 450\nvoid main() { }\n";
|
|
}
|
|
Configuration cfg = s.Config({});
|
|
cfg.shaders.emplace_back(fs::path(shader), "main", ShaderType::Fragment);
|
|
cfg.AddLintRule("no-lex", [](LintContext& ctx) {
|
|
Check(ctx.Tokens().empty(), "tokens: shaders are not lexed as C++");
|
|
});
|
|
RunLint(cfg, Mode(LintMode::Report));
|
|
}
|
|
|
|
// ---------------- AST layer ----------------
|
|
//
|
|
// A standalone source with no imports, so it parses without this project's
|
|
// PCMs and the case stays a unit test.
|
|
{
|
|
constexpr std::string_view Source =
|
|
"#include <string>\n" // 1
|
|
"namespace Demo {\n" // 2
|
|
" enum class Scoped { A, B };\n" // 3
|
|
" enum Plain { C, D };\n" // 4
|
|
" struct Widget {\n" // 5
|
|
" int count;\n" // 6
|
|
" std::string name;\n" // 7
|
|
" };\n" // 8
|
|
" static int GlobalCounter = 0;\n" // 9
|
|
" constexpr int Limit = 10;\n" // 10
|
|
" int Compute(int input) {\n" // 11
|
|
" int local = input;\n" // 12
|
|
" return local;\n" // 13
|
|
" }\n" // 14
|
|
"}\n"; // 15
|
|
|
|
Scratch s("ast");
|
|
s.Write("f", Source);
|
|
Configuration cfg = s.Config({"f"});
|
|
cfg.AddAstLintRule("ast", [](LintContext& ctx) {
|
|
Check(ctx.AstAvailable(), std::format("ast: parse succeeded ({})", ctx.AstUnavailableReason()));
|
|
std::span<const LintDecl> decls = ctx.Decls();
|
|
Check(!decls.empty(), "ast: declarations found");
|
|
|
|
auto find = [&](LintDeclKind kind, std::string_view name) -> const LintDecl* {
|
|
auto it = std::ranges::find_if(decls, [&](const LintDecl& d) { return d.kind == kind && d.name == name; });
|
|
return it == decls.end() ? nullptr : &*it;
|
|
};
|
|
auto parentOf = [&](const LintDecl& d) -> const LintDecl* {
|
|
return d.parent == LintNoParent ? nullptr : &decls[d.parent];
|
|
};
|
|
|
|
// Only this file's declarations: <string> drags in thousands and
|
|
// none of them may appear here.
|
|
Check(std::ranges::none_of(decls, [](const LintDecl& d) { return d.name == "basic_string"; }), "ast: declarations from #included headers are excluded");
|
|
|
|
const LintDecl* demo = find(LintDeclKind::Namespace, "Demo");
|
|
Check(demo != nullptr && demo->line == 2, "ast: namespace found at its own line");
|
|
|
|
// The whole point for enum-class: an exact query, not a regex.
|
|
const LintDecl* scoped = find(LintDeclKind::Enum, "Scoped");
|
|
const LintDecl* plain = find(LintDeclKind::Enum, "Plain");
|
|
Check(scoped != nullptr && scoped->isScopedEnum, "ast: enum class is scoped");
|
|
Check(plain != nullptr && !plain->isScopedEnum, "ast: plain enum is not scoped");
|
|
Check(scoped != nullptr && parentOf(*scoped) == demo, "ast: enum's parent is the namespace");
|
|
|
|
// The whole point for naming: scope without a brace stack.
|
|
const LintDecl* widget = find(LintDeclKind::Struct, "Widget");
|
|
const LintDecl* count = find(LintDeclKind::Field, "count");
|
|
Check(widget != nullptr, "ast: struct found");
|
|
Check(count != nullptr && parentOf(*count) == widget, "ast: field's parent is its struct");
|
|
Check(count != nullptr && count->type == "int", "ast: field carries a resolved type");
|
|
const LintDecl* name = find(LintDeclKind::Field, "name");
|
|
Check(name != nullptr && name->type.contains("string"), "ast: library type resolves");
|
|
|
|
const LintDecl* global = find(LintDeclKind::Variable, "GlobalCounter");
|
|
Check(global != nullptr && global->isStatic, "ast: static storage class reported");
|
|
const LintDecl* limit = find(LintDeclKind::Variable, "Limit");
|
|
Check(limit != nullptr && limit->isConstexpr, "ast: constexpr reported");
|
|
Check(global != nullptr && !global->isConstexpr, "ast: non-constexpr not misreported");
|
|
|
|
const LintDecl* compute = find(LintDeclKind::Function, "Compute");
|
|
const LintDecl* local = find(LintDeclKind::Variable, "local");
|
|
const LintDecl* input = find(LintDeclKind::Parameter, "input");
|
|
Check(compute != nullptr && compute->isDefinition, "ast: function definition reported");
|
|
Check(input != nullptr && parentOf(*input) == compute, "ast: parameter's parent is its function");
|
|
// A local's parent is the function, not the namespace — which is
|
|
// exactly the distinction the brace stack was approximating.
|
|
Check(local != nullptr && parentOf(*local) == compute, "ast: local's parent is its function");
|
|
Check(global != nullptr && parentOf(*global) == demo, "ast: namespace-scope variable's parent is the namespace");
|
|
|
|
// Extents must address the declaration's own bytes so a transform
|
|
// can mark a region untouchable.
|
|
Check(widget != nullptr && widget->end > widget->begin && widget->end <= ctx.content.size(), "ast: extent is in range");
|
|
if (count != nullptr) {
|
|
Check(std::string_view(ctx.content).substr(count->begin, count->end - count->begin) == "int count", "ast: extent brackets exactly the declaration");
|
|
}
|
|
});
|
|
RunLint(cfg, Mode(LintMode::Report));
|
|
}
|
|
|
|
// A module interface unit. libclang reports `export namespace X { … }` as a
|
|
// childless CXCursor_UnexposedDecl and refuses to descend, so without the
|
|
// export-blanking pass every declaration in it would be invisible — which
|
|
// is five of this repo's own interfaces, 677 lines. Blanking the keyword is
|
|
// byte-length preserving, so the lines reported here must match the file.
|
|
//
|
|
// The source lives in fixture/ExportNamespace.cppm.in, copied into the
|
|
// scratch directory as Demo.cppm.
|
|
{
|
|
Scratch s("ast-module");
|
|
fs::copy_file(fs::current_path() / "tests" / "Lint" / "fixture" / "ExportNamespace.cppm.in", s.dir / "Demo.cppm", fs::copy_options::overwrite_existing);
|
|
Configuration cfg;
|
|
cfg.path = s.dir;
|
|
cfg.name = "ast-module";
|
|
cfg.outputName = "ast-module";
|
|
cfg.target = HostTarget();
|
|
std::array<fs::path, 1> ifaces = { "Demo" };
|
|
std::array<fs::path, 0> impls = {};
|
|
cfg.GetInterfacesAndImplementations(ifaces, impls);
|
|
cfg.AddAstLintRule("ast-module", [](LintContext& ctx) {
|
|
Check(ctx.AstAvailable(), std::format("ast-module: parse succeeded ({})", ctx.AstUnavailableReason()));
|
|
std::span<const LintDecl> decls = ctx.Decls();
|
|
auto find = [&](LintDeclKind kind, std::string_view name) -> const LintDecl* {
|
|
auto it = std::ranges::find_if(decls, [&](const LintDecl& d) { return d.kind == kind && d.name == name; });
|
|
return it == decls.end() ? nullptr : &*it;
|
|
};
|
|
const LintDecl* ns = find(LintDeclKind::Namespace, "Demo");
|
|
const LintDecl* mode = find(LintDeclKind::Enum, "Mode");
|
|
const LintDecl* widget = find(LintDeclKind::Struct, "Widget");
|
|
const LintDecl* count = find(LintDeclKind::Field, "count");
|
|
const LintDecl* exported = find(LintDeclKind::Variable, "Exported");
|
|
Check(ns != nullptr, "ast-module: descends into export namespace");
|
|
Check(mode != nullptr && mode->isScopedEnum, "ast-module: enum inside export namespace is visible");
|
|
Check(widget != nullptr && count != nullptr, "ast-module: struct and field inside export namespace are visible");
|
|
Check(exported != nullptr, "ast-module: per-declaration export is visible");
|
|
// Byte fidelity: blanking must not shift a single line.
|
|
Check(ns != nullptr && ns->line == 15, "ast-module: namespace line matches the unblanked file");
|
|
Check(mode != nullptr && mode->line == 16, "ast-module: enum line matches");
|
|
Check(count != nullptr && count->line == 18, "ast-module: field line matches");
|
|
Check(exported != nullptr && exported->line == 20, "ast-module: exported variable line matches");
|
|
});
|
|
RunLint(cfg, Mode(LintMode::Report));
|
|
}
|
|
|
|
// An unavailable AST must fail the run, never look like a clean file. A
|
|
// module unit with no PCMs is the realistic way to hit this.
|
|
{
|
|
Scratch s("ast-unavailable");
|
|
s.Write("f", "import Crafter.DefinitelyNotAModule;\nint Value = 1;\n");
|
|
Configuration cfg = s.Config({"f"});
|
|
bool ran = false;
|
|
cfg.AddAstLintRule("needs-ast", [&ran](LintContext&) { ran = true; });
|
|
LintSummary summary = RunLint(cfg, Mode(LintMode::Report));
|
|
Check(!ran, "ast: rule is skipped when the AST is unavailable");
|
|
Check(summary.errors > 0, "ast: unavailable AST counts as an error");
|
|
Check(!summary.Clean(), "ast: unavailable AST is not Clean");
|
|
// Explained on stderr as one grouped message rather than a finding per
|
|
// (file, rule): a single missing PCM would otherwise bury the one fact
|
|
// that matters. The run still failing is the part that counts, and the
|
|
// two assertions above cover it.
|
|
}
|
|
|
|
// A configuration whose std module cannot be built is in the build graph
|
|
// all the same: its files cannot be parsed, which has to fail the run.
|
|
// Its command was cleared, which read as "not a translation unit", and
|
|
// every semantic rule passed the files. A sysroot without libc++ is one
|
|
// way there. The std module's directory is made before its build fails,
|
|
// so the cache is redirected around this run.
|
|
{
|
|
Scratch s("ast-no-std-module");
|
|
s.Write("f", "int Value = 1;\n");
|
|
Configuration cfg = s.Config({"f"});
|
|
cfg.sysroot = (s.dir / "empty-sysroot").string();
|
|
fs::create_directories(cfg.sysroot);
|
|
bool ran = false;
|
|
cfg.AddAstLintRule("needs-ast", [&ran](LintContext&) { ran = true; });
|
|
const char* realCache = std::getenv("XDG_CACHE_HOME");
|
|
const std::string saved = realCache ? realCache : "";
|
|
setenv("XDG_CACHE_HOME", (s.dir / "cache").c_str(), 1);
|
|
LintSummary summary = RunLint(cfg, Mode(LintMode::Report));
|
|
if (saved.empty()) unsetenv("XDG_CACHE_HOME");
|
|
else setenv("XDG_CACHE_HOME", saved.c_str(), 1);
|
|
Check(!ran, "ast: rule is skipped when the std module cannot be built");
|
|
Check(summary.errors > 0, "ast: a std module that cannot be built fails the run");
|
|
Check(!summary.Clean(), "ast: a std module that cannot be built is not Clean");
|
|
}
|
|
|
|
// --no-ast skips those rules deliberately and exits normally.
|
|
{
|
|
Scratch s("ast-optout");
|
|
s.Write("f", "import Crafter.DefinitelyNotAModule;\nint Value = 1;\n");
|
|
Configuration cfg = s.Config({"f"});
|
|
cfg.AddAstLintRule("needs-ast", [](LintContext& ctx) { ctx.Report(1, "should not run"); });
|
|
RunLintOptions opts = Mode(LintMode::Report);
|
|
opts.noAst = true;
|
|
LintSummary summary = RunLint(cfg, opts);
|
|
Check(summary.errors == 0, "ast: --no-ast does not error");
|
|
Check(summary.Clean(), "ast: --no-ast run is Clean");
|
|
}
|
|
|
|
if (Failures > 0) {
|
|
std::println(std::cerr, "{} assertions failed", Failures);
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|