This is an archive of the discontinued LLVM Phabricator instance.

[clang-scan-deps] : Support -- in clang command lines.
ClosedPublic

Authored by saudi on Jan 20 2021, 2:48 PM.

Details

Summary

This fixes argument injection when clang command line contains "--" in the argument.

Previously, the arguments were injected at the end of the command line. It could be added after --, and be interpreted as input file paths.

This fix is needed for a subsequent patch, D92191

Diff Detail

Event Timeline

saudi created this revision.Jan 20 2021, 2:48 PM
saudi requested review of this revision.Jan 20 2021, 2:48 PM
saudi updated this revision to Diff 318214.Jan 21 2021, 8:04 AM

Bugfixes on the use of the iterator.

Thanks for splitting this out! The looks good, just a few nits in line below. I also have a question about the test.

clang/lib/Tooling/Tooling.cpp
440 ↗(On Diff #318214)

I think another name that doesn't sound like a synonym of Args.end() would be more clear. Maybe LastFlag or FlagsEnd?

clang/test/ClangScanDeps/Inputs/regular_cdb.json
12–16

It's not obvious to me how this exercises the new -resource-dir logic. Can you walk me through it?

clang/tools/clang-scan-deps/ClangScanDeps.cpp
420–421

Should this condition be instead check for LastFlag != Args.begin()?

421

I think it'd be a bit clearer to name this LastFlag or LastOption or FlagsEnd or something, since this is not the last argument, just the last flag/option. Especially helpful on the last usage below when referencing the range from ArgsEnd to Args.end().

423

Nit: since you're touching this line anyway, would be nice to update it to follow the usual LLVM style of calling rend() just once instead of on every iteration:

for (auto RB = llvm::make_reverse_iterator(LastFlag), RI = RB, RE = Args.rend();
     RI != RE; ++RI) {

Up to you, but I find the initialism RI a bit more indicative of a reverse iterator... this patch touches every use so you could rename it without pulling in any extra diff. If you prefer the names you have that's fine too though.

Also, if you narrow the scope of RB / ArgsRBegin you can probably reduce nesting by deleting the redundant if, but that'd be nice to commit separately (in a follow up?) to avoid cluttering up this patch with NFC indentation changes.

429

Does RI[-1] work here? Personally I find x[y] more clear than *(x + y).

475

Can we use AdjustedArgs.append(LastFlag, Args.end()) here?

saudi updated this revision to Diff 318304.Jan 21 2021, 1:39 PM
saudi marked 5 inline comments as done.

Updated for suggested modifications

saudi added inline comments.Jan 21 2021, 1:40 PM
clang/test/ClangScanDeps/Inputs/regular_cdb.json
12–16

clang tools like clang-scan-deps may edit the provided clang command line, especially to override the resource directory with their own default value in case it is not found as an argument.

This patch avoids inserting new arguments after --, since they would be wrongly interpreted as extra input files.

By adding -- in this test, we broadly verify that no argument inserted by clang-scan-deps is treated as an extra input file. -resource-dir is merely one possible case.

Note that -resource-dir case is a bit special: clang-scan-deps will first try to force its value using the result of clang -print-resource-dir (see ClangScanDeps.cpp).
Yet, this heuristic may fail (and it does in this clang-scan-deps test, where the clang command is not given as an absolute path). In that case ClangTool will also try to add -resource-dir (see Tooling.cpp).

clang/tools/clang-scan-deps/ClangScanDeps.cpp
475

AdjustedArgs is of type tooling::CommandLineArguments which inherits from std::vector, it doesn't have append().

This revision is now accepted and ready to land.Feb 3 2021, 5:42 PM
This revision was automatically updated to reflect the committed changes.