This is an archive of the discontinued LLVM Phabricator instance.

[Clang] Emit a warning for ambiguous joined '-o' arguments
ClosedPublic

Authored by jhuber6 on Oct 6 2022, 12:54 PM.

Details

Summary

The offloading toolchain makes heavy use of options beginning with
--o. This is problematic when combined with the joined -o flag. In
the following situation, the user will not get the expected output and
will not notice as the expected output will still be written.

clang++ -x cuda foo.cu -offload-arch=sm_80 -o foo

This patch introduces a warning that checks for joined -o arguments
that would also be a valid driver argument if an additional - were
added. I believe this situation is uncommon enough to warrant a warning,
and can be trivially fixed by the end user by using the more common
separate form instead.

Diff Detail

Event Timeline

jhuber6 created this revision.Oct 6 2022, 12:54 PM
Herald added a project: Restricted Project. · View Herald TranscriptOct 6 2022, 12:54 PM
Herald added a subscriber: StephenFan. · View Herald Transcript
jhuber6 requested review of this revision.Oct 6 2022, 12:54 PM
Herald added a project: Restricted Project. · View Herald TranscriptOct 6 2022, 12:54 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
tra added a comment.Oct 6 2022, 1:18 PM

LGTM in principle.

clang/lib/Driver/Driver.cpp
337

This looks for all 1-symbol mismatches. and may trigger on differences other than --option vs -option. E.g. if user passes -oip-link (which looks like something we may see in real life), we'll try to find the nearest for --oip-link and will get a distance-1 match for the existing option --hip-link.

We may need to verify that the nearest match that we've found is indeed due to a missing leading dash.

clang/test/Driver/unknown-arg.c
68

I'd also add a test verifying that -o ffload-device-only does not trigger the warning.

jhuber6 updated this revision to Diff 465862.Oct 6 2022, 1:41 PM

Adjusting check to ensure that this only applies for an edit length of zero once the - is added. Also adding a negative check line for other inputs.

jhuber6 marked 2 inline comments as done.Oct 6 2022, 1:42 PM
jhuber6 added inline comments.
clang/lib/Driver/Driver.cpp
337

You're right, I changed it to use the raw string from the input. Since it's joined it will be the full string, so if we append a - to it and the edit length is zero then we know for a fact that it's due to a missing dash.

tra accepted this revision.Oct 6 2022, 1:57 PM

Test nit. LGTM otherwise.

clang/test/Driver/unknown-arg.c
73

I think this check will never trigger, because, if the warning would happen to be issued, it would happen before the warning for -output above and we'd miss it.

Doing negative test in a separate RUN would probably be the easiest way to deal with it.

This revision is now accepted and ready to land.Oct 6 2022, 1:57 PM
jhuber6 updated this revision to Diff 465876.Oct 6 2022, 2:20 PM
jhuber6 marked an inline comment as done.

Changing test to just check that we emit to warnings in a separate run line.

tra added inline comments.Oct 6 2022, 2:31 PM
clang/test/Driver/unknown-arg.c
74

Has this patch been updated with incomplete changes? This RUN line seems to miss 2>&1 | FileCheck and the check lines.

jhuber6 added inline comments.Oct 6 2022, 2:31 PM
clang/test/Driver/unknown-arg.c
74

I'm relying on the fact that -Werror will cause clang to return a non-zero error code and make the test fail if any warnings show up.

jhuber6 updated this revision to Diff 465883.Oct 6 2022, 2:36 PM

Apparently -### prevents clang from returning a non-zero error code. Fixing.

tra accepted this revision.Oct 6 2022, 2:37 PM
tra added inline comments.
clang/test/Driver/unknown-arg.c
74

Got it. I've missed that.

jhuber6 added inline comments.Oct 6 2022, 2:38 PM
clang/test/Driver/unknown-arg.c
74

You were somewhat right, I had -### which apparently makes clang return 0 even on an error. So thanks for making me double check.

MaskRay accepted this revision.Oct 6 2022, 2:55 PM
MaskRay added inline comments.
clang/test/Driver/unknown-arg.c
74

Use -### --implicit-check-not=warning: instead of -emit-llvm which runs more than the driver.

jhuber6 updated this revision to Diff 465894.Oct 6 2022, 3:10 PM

Using the implicit check suggestion.

This revision was automatically updated to reflect the committed changes.
thakis added a subscriber: thakis.Oct 6 2022, 5:34 PM

This breaks tests on windows: http://45.33.8.238/win/67644/step_7.txt

Please take a look and revert for now if it takes a while to fix.