Adds LintContext::Decls() — clang's view of the declarations written in the
file — plus AddAstLintRule to register a rule that reads it. No rule uses it
yet; the three that will are migrated separately.
The declarations come back as a flat vector with parent indices rather than an
opaque cursor handle: no lifetimes cross the project-DLL boundary, no callback
hops back into project.so per node, and "is this at namespace scope or inside a
function?" becomes an index lookup instead of a hand-rolled brace stack.
Three things had to be solved for this to work at all on this codebase.
libclang cannot see through `export`. A C++20 export declaration has no
CXCursorKind, so `export namespace Crafter { … }` arrives as a childless
CXCursor_UnexposedDecl and clang_visitChildren does not descend. Five of the
eleven interfaces here are written that way — 677 lines, including
Configuration and LintContext, yielding zero usable cursors. A plain
`namespace` IS descended into, so the fix is to blank the keyword before
parsing, byte-length preserving so every line and column still lands on the
original file. `export module` is left alone or the unit stops being a module
interface. Verified end-to-end against a fixture whose asserted line numbers
match the unblanked file.
PCMs are flag-locked, so each file has to parse with the flags that built it.
CollectConfigSources now records which Configuration owns each source instead
of flattening to a set, because three regimes coexist: the library, each test
(carrying its own target, defines and -march), and project.cpp, which Build
never touches and which therefore gets no command at all.
libclang resolves its builtin headers relative to its own install path, which
need not match the clang++ that wrote the PCMs. When it doesn't, every parse
dies on "'stddef.h' file not found", so -resource-dir is passed explicitly
from `clang++ -print-resource-dir`.
Two flags on each declaration replace what would otherwise become more
substring denylists: isExternC, and isForeignApi for a declaration that binds
to an entity declared outside the project root — resolved through
clang_getCursorReferenced and the same inside-the-root test the dependency
walk already uses. Parameters and fields inherit it, so an exemption covers a
whole signature rather than the one node that named the foreign entity.
Failure is never silent. A fatal diagnostic leaves a fragment that is
indistinguishable from a file declaring nothing, so it is reported as an error
instead: the rule is skipped, a finding explains why, and summary.errors makes
the run fail. --no-ast opts out deliberately and exits normally.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
67 lines
3.3 KiB
C++
67 lines
3.3 KiB
C++
// SPDX-License-Identifier: LGPL-3.0-only
|
|
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
|
|
|
module;
|
|
#include "Crafter.Build-Api.h"
|
|
export module Crafter.Build:Lint;
|
|
import std;
|
|
import :Clang;
|
|
|
|
export namespace Crafter {
|
|
enum class LintMode {
|
|
// `lint`: diagnose only; transform output becomes would-reformat
|
|
// findings. Never writes. The default.
|
|
Report,
|
|
// `format --check`: dry run; record the files that would change.
|
|
Check,
|
|
// `format`: rewrite changed files in place.
|
|
Apply,
|
|
};
|
|
|
|
struct RunLintOptions {
|
|
// Rule-name globs ('*', '?'); empty = every registered rule.
|
|
std::vector<std::string> globs;
|
|
// Enumerate matching rule names without running them.
|
|
bool listOnly = false;
|
|
LintMode mode = LintMode::Report;
|
|
// Skip rules registered with AddAstLintRule instead of building the
|
|
// module PCMs they need. The escape hatch for a fresh clone where the
|
|
// token-based rules are wanted immediately; the run then exits normally
|
|
// rather than erroring, so it must be asked for explicitly.
|
|
bool noAst = false;
|
|
// Absolute path of the loaded project.cpp. It is linted too, and its
|
|
// parent directory is the project root that decides which dependency
|
|
// Configurations contribute rules/files (GitProject / cache-dir deps
|
|
// are foreign code and skipped). Empty => fall back to cfg.path.
|
|
std::filesystem::path projectFile;
|
|
};
|
|
|
|
struct LintSummary {
|
|
std::vector<LintFinding> findings; // sorted by (file, line); filled in every mode
|
|
// Apply: files rewritten on disk. Report/Check: files a transform
|
|
// would change. The `format` verb's exit code keys off this in Check
|
|
// mode; formatting files in Apply mode is success.
|
|
std::vector<std::filesystem::path> changedFiles;
|
|
std::size_t filesLinted = 0;
|
|
std::size_t rulesRun = 0; // rules remaining after glob filter
|
|
// Rule exceptions, write failures, and infrastructure failures such as
|
|
// libclang not loading. Counted in Clean() so a run that could not do
|
|
// its job never looks like a run that found nothing.
|
|
std::size_t errors = 0;
|
|
bool noRulesDefined = false; // project registered no rules at all
|
|
// Host-side only (like TestSummary::AllPassed), safe as in-class inline.
|
|
bool Clean() const { return findings.empty() && !noRulesDefined && errors == 0; }
|
|
};
|
|
|
|
// Run the project's lint rules over its own sources: module interfaces
|
|
// (+ partitions), implementations, cFiles, cuda, shaders, declared tests'
|
|
// sources, and project.cpp itself — for the root Configuration plus every
|
|
// transitive dependency whose path lies inside the project root. Rules
|
|
// run in registration order per file, each seeing the previous rule's
|
|
// transform output; a rule that throws is reverted and surfaced as a
|
|
// finding + error. Only Apply mode writes to disk, and only files whose
|
|
// final content differs from the original. Prints per-mode output:
|
|
// findings compiler-style (Report), would-change paths (Check), or
|
|
// formatted paths (Apply), plus a summary line.
|
|
CRAFTER_API LintSummary RunLint(Configuration& projectCfg, const RunLintOptions& opts);
|
|
}
|