lint's parse splits the compile command at spaces, where the shell that runs it honours quotes: a quoted flag with a space is one argument to the compile and two to the parse #59

Open
opened 2026-11-27 18:28:37 +00:00 by jorijnvdgraaf · 0 comments

Summary

lint hands libclang the compile command as CommandToArgs splits it: at every space, undoing only \" (implementations/Crafter.Build-Lint.cpp:358-383). The build runs the same string through sh, which honours quotes. So a cfg.compileFlags entry that quotes a space for the shell is one argument to the compile, and since 6.4.2 to the scan, and two to lint's parse.

Reproduce

project.cpp, with <dir> an absolute directory:

import std;
import Crafter.Build;
namespace fs = std::filesystem;
using namespace Crafter;

extern "C" Configuration CrafterBuildProject(std::span<const std::string_view> args) {
    Configuration cfg;
    cfg.path = "./";
    cfg.name = "split";
    cfg.outputName = "split";
    ApplyStandardArgs(cfg, args);
    cfg.compileFlags.push_back("-I\"<dir>/inc dir\"");
    std::array<fs::path, 0> interfaces = {};
    std::array<fs::path, 1> implementations = {"main"};
    cfg.GetInterfacesAndImplementations(interfaces, implementations);
    cfg.AddAstLintRule("reads-ast", [](LintContext& ctx) { (void)ctx.Decls(); });
    return cfg;
}

<dir>/inc dir/greeting.h:

#pragma once
inline constexpr int Greeting = 1;

main.cpp:

#include <greeting.h>

int main() {
    return Greeting - 1;
}

crafter-build -r builds the program and runs it, which exits 0. crafter-build lint then exits 1:

lint: 1 rule(s) needing an AST (reads-ast) could not run on 1 of 2 file(s).
      main.cpp: 'greeting.h' file not found
      Build the project first so the module PCMs exist, or pass --no-ast to skip these rules.

Reproduced with cb8914d (6.4.3) built natively. A -D value quoted the same way, -DGREETING="hello world", is split as well, but libclang took the two halves without an error and lint passed.

The comment on CommandToArgs says a path containing a space "is already unsupported upstream of here" (:360-361). A flag the project quotes itself works everywhere but in lint.

6.4.3's Toolchain::InstalledClangxx reads the command's words the same way, split at spaces only (implementations/Crafter.Build-Toolchain.cpp:154-170), to find the last of -canonical-prefixes and -no-canonical-prefixes. clang's driver reads them from its arguments, after the shell has split the command and after it has expanded @file arguments (LLVM 23.1.2 clang/tools/driver/driver.cpp:265, :280-291). So a -no-canonical-prefixes that only a response file holds, or that a compileFlags entry puts after a tab, is seen by the compile and not by the scan and the parse.

Found by the pre-push review of 6.4.3.

Line numbers refer to cb8914d (6.4.3).

Proposal

Split the command as the shell does, single and double quotes and backslashes included, in one function that CommandToArgs and InstalledClangxx both use. Or keep the compile command as a list of arguments, from which the shell command and libclang's argv are both made, so that nothing has to split it back.

## Summary lint hands libclang the compile command as `CommandToArgs` splits it: at every space, undoing only `\"` (`implementations/Crafter.Build-Lint.cpp:358-383`). The build runs the same string through `sh`, which honours quotes. So a `cfg.compileFlags` entry that quotes a space for the shell is one argument to the compile, and since 6.4.2 to the scan, and two to lint's parse. ## Reproduce `project.cpp`, with `<dir>` an absolute directory: ```cpp import std; import Crafter.Build; namespace fs = std::filesystem; using namespace Crafter; extern "C" Configuration CrafterBuildProject(std::span<const std::string_view> args) { Configuration cfg; cfg.path = "./"; cfg.name = "split"; cfg.outputName = "split"; ApplyStandardArgs(cfg, args); cfg.compileFlags.push_back("-I\"<dir>/inc dir\""); std::array<fs::path, 0> interfaces = {}; std::array<fs::path, 1> implementations = {"main"}; cfg.GetInterfacesAndImplementations(interfaces, implementations); cfg.AddAstLintRule("reads-ast", [](LintContext& ctx) { (void)ctx.Decls(); }); return cfg; } ``` `<dir>/inc dir/greeting.h`: ```cpp #pragma once inline constexpr int Greeting = 1; ``` `main.cpp`: ```cpp #include <greeting.h> int main() { return Greeting - 1; } ``` `crafter-build -r` builds the program and runs it, which exits 0. `crafter-build lint` then exits 1: ``` lint: 1 rule(s) needing an AST (reads-ast) could not run on 1 of 2 file(s). main.cpp: 'greeting.h' file not found Build the project first so the module PCMs exist, or pass --no-ast to skip these rules. ``` Reproduced with `cb8914d` (6.4.3) built natively. A `-D` value quoted the same way, `-DGREETING="hello world"`, is split as well, but libclang took the two halves without an error and lint passed. The comment on `CommandToArgs` says a path containing a space "is already unsupported upstream of here" (`:360-361`). A flag the project quotes itself works everywhere but in lint. 6.4.3's `Toolchain::InstalledClangxx` reads the command's words the same way, split at spaces only (`implementations/Crafter.Build-Toolchain.cpp:154-170`), to find the last of `-canonical-prefixes` and `-no-canonical-prefixes`. clang's driver reads them from its arguments, after the shell has split the command and after it has expanded `@file` arguments (LLVM 23.1.2 `clang/tools/driver/driver.cpp:265`, `:280-291`). So a `-no-canonical-prefixes` that only a response file holds, or that a `compileFlags` entry puts after a tab, is seen by the compile and not by the scan and the parse. Found by the pre-push review of 6.4.3. Line numbers refer to `cb8914d` (6.4.3). ## Proposal Split the command as the shell does, single and double quotes and backslashes included, in one function that `CommandToArgs` and `InstalledClangxx` both use. Or keep the compile command as a list of arguments, from which the shell command and libclang's argv are both made, so that nothing has to split it back.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Catcrafts/Crafter.Build#59
No description provided.