[clang] 5aba689 - [Clang] Emit a warning for ambiguous joined '-o' arguments
Joseph Huber via cfe-commits
cfe-commits at lists.llvm.org
Thu Oct 6 16:20:26 PDT 2022
Author: Joseph Huber
Date: 2022-10-06T18:20:15-05:00
New Revision: 5aba68960719c5d2de9ee0b459def9203e521f5e
URL: https://github.com/llvm/llvm-project/commit/5aba68960719c5d2de9ee0b459def9203e521f5e
DIFF: https://github.com/llvm/llvm-project/commit/5aba68960719c5d2de9ee0b459def9203e521f5e.diff
LOG: [Clang] Emit a warning for ambiguous joined '-o' arguments
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.
Reviewed By: tra, MaskRay
Differential Revision: https://reviews.llvm.org/D135389
Added:
Modified:
clang/include/clang/Basic/DiagnosticDriverKinds.td
clang/lib/Driver/Driver.cpp
clang/test/Driver/unknown-arg.c
Removed:
################################################################################
diff --git a/clang/include/clang/Basic/DiagnosticDriverKinds.td b/clang/include/clang/Basic/DiagnosticDriverKinds.td
index e8a301ee6018c..b09e124206213 100644
--- a/clang/include/clang/Basic/DiagnosticDriverKinds.td
+++ b/clang/include/clang/Basic/DiagnosticDriverKinds.td
@@ -253,6 +253,9 @@ def warn_drv_yc_multiple_inputs_clang_cl : Warning<
"support for '/Yc' with more than one source file not implemented yet; flag ignored">,
InGroup<ClangClPch>;
+def warn_drv_potentially_misspelled_joined_argument : Warning<
+ "joined argument treated as '%0'; did you mean '%1'?">, InGroup<UnknownArgument>;
+
def err_drv_invalid_value : Error<"invalid value '%1' in '%0'">;
def err_drv_invalid_int_value : Error<"invalid integral value '%1' in '%0'">;
def err_drv_invalid_value_with_suggestion : Error<
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index 21dc180d26c36..e612afdd56c9b 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -327,6 +327,19 @@ InputArgList Driver::ParseArgStrings(ArrayRef<const char *> ArgStrings,
DiagnosticsEngine::Warning;
}
+ for (const Arg *A : Args.filtered(options::OPT_o)) {
+ if (ArgStrings[A->getIndex()] == A->getSpelling())
+ continue;
+
+ // Warn on joined arguments that are similar to a long argument.
+ std::string ArgString = ArgStrings[A->getIndex()];
+ std::string Nearest;
+ if (getOpts().findNearest("-" + ArgString, Nearest, IncludedFlagsBitmask,
+ ExcludedFlagsBitmask) == 0)
+ Diags.Report(diag::warn_drv_potentially_misspelled_joined_argument)
+ << A->getAsString(Args) << Nearest;
+ }
+
return Args;
}
diff --git a/clang/test/Driver/unknown-arg.c b/clang/test/Driver/unknown-arg.c
index 32346e3326f3f..9c1d433c8f0d4 100644
--- a/clang/test/Driver/unknown-arg.c
+++ b/clang/test/Driver/unknown-arg.c
@@ -1,5 +1,5 @@
-// RUN: not %clang %s -cake-is-lie -%0 -%d -HHHH -munknown-to-clang-option -print-stats -funknown-to-clang-option -ifoo -imultilib dir -### 2>&1 | \
-// RUN: FileCheck %s
+// RUN: not %clang %s -cake-is-lie -%0 -%d -HHHH -munknown-to-clang-option -print-stats -funknown-to-clang-option -ifoo -imultilib dir -o ffload-device-only -### 2>&1 | \
+// RUN: FileCheck %s --implicit-check-not=warning:
// RUN: %clang %s -imultilib dir -### 2>&1 | \
// RUN: FileCheck %s --check-prefix=MULTILIB
// RUN: not %clang %s -stdlibs=foo -hell -version -### 2>&1 | \
@@ -64,3 +64,9 @@
// RUN: %clang -S %s -o %t.s -Wunknown-to-clang-option 2>&1 | FileCheck --check-prefix=IGNORED %s
// IGNORED: warning: unknown warning option '-Wunknown-to-clang-option'
+
+// RUN: %clang -### -offload-arch=sm_70 --offload-arch=sm_70 -offload-device-only -o ffload-device-only \
+// RUN: -output -o foo %s 2>&1 | FileCheck --check-prefix=O-WARN %s
+// O-WARN: warning: joined argument treated as '-o ffload-arch=sm_70'; did you mean '--offload-arch=sm_70'?
+// O-WARN-NEXT: warning: joined argument treated as '-o ffload-device-only'; did you mean '--offload-device-only'?
+// O-WARN-NEXT: warning: joined argument treated as '-o utput'; did you mean '--output'?
More information about the cfe-commits
mailing list