This is an archive of the discontinued LLVM Phabricator instance.

[clang][deps] Fix clang-cl output argument parsing
Needs ReviewPublic

Authored by jansvoboda11 on Mar 24 2022, 5:12 AM.

Details

Summary

One goal of the ad-hoc command line argument parsing in clang-scan-deps is to get the last output path value.

Currently, the algorithm walks the arguments in reverse and identifies potention output path arguments.

Interestingly, in clang-cl mode, the output path can be specified in multiple ways. For example /o /opt/build and /o/opt/build are equivalent. The parser currently fails to correctly parse the former. It considers the value (/opt/build/) to be a shorthand for /o pt/build. It doesn't look at the preceding /o.

This patch simplifies the algorithm by doing forward iteration and fixes the bug by correctly handling separate /o argument.

Diff Detail

Event Timeline

jansvoboda11 created this revision.Mar 24 2022, 5:12 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 24 2022, 5:12 AM
jansvoboda11 requested review of this revision.Mar 24 2022, 5:12 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 24 2022, 5:12 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
saudi added a comment.Mar 24 2022, 6:26 AM

Nice! It's much more straightforward now, the reverse iteration was pretty confusing.
Thanks!

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

A test for boundary (I+1 != FlagsEnd) will be needed here, to avoid an infinite loop in the case -Xclang is the last arg.

I think it would be worth doing continue here, both because Arg's value is -Xclang, which won't be used in this iteration, and also it is a bit confusing as Arg has now represents *(I-1) instead of *I

478

llvm:: is unnecessary here, as it's not used for other StringRef in this file

479

The test for the end should be on I+1

gulfem added a subscriber: gulfem.Mar 24 2022, 10:53 AM
ormris removed a subscriber: ormris.May 16 2022, 11:15 AM

Code review feedback

jansvoboda11 marked 3 inline comments as done.Jun 20 2022, 6:15 AM
saudi added inline comments.Nov 9 2022, 7:46 AM
clang/tools/clang-scan-deps/ClangScanDeps.cpp
487

Before the patch LastO string allocation was done only once (because of the !LastO.empty() test).
It probably would be better to have it of type StringRef and allocate once at the end