martine/ninja — Issue #168: Escaped spaces in dependency files
Author: sorbits
Created: 2011-12-26 11:22:57+00:00
State: open

The dependency file parser does not handle escaped spaces, e.g.:

# build.ninja
rule cc
depfile = $out.d
command = gcc -MMD -MF "$out.d" -c "$in" -o "$out"

build my$ test.o: cc my$ test.cc

And:

// my test.cc
int main (int argc, char const* argv[])
{
return 0;
}

The generated `my test.o.d` becomes:

my\ test.o: my\ test.cc

The parser doesn’t do anything special with `\ ` so on a second run we get:

% ninja
ninja: ERROR: expected depfile 'my test.o.d' to mention 'my test.o', got 'my\ test.o'

It should be noted that while we get an error because of output name mismatch the dependency setup is also incorrect.

While looking into creating a patch for this I noticed that the dependency parser uses `StringPiece` for the paths. This class is a wrapper for a memory pointer and the memory given to `DepfileParser::Parse` is immutable so I’d suggest `DepfileParser` be changed to use `std::string` instead of `StringPiece` as the alternative would be ad hoc heap allocation when the need for decoding a string arise either tracking these allocations in `DepfileParser` or leaking…

Any hesitation wrt. changing `StringPiece` to `std::string` in `DepfileParser`?