================
@@ -3167,3 +3167,30 @@ void tools::handleInterchangeLoopsArgs(const ArgList 
&Args,
                    options::OPT_fno_loop_interchange, EnableInterchange))
     CmdArgs.push_back("-floop-interchange");
 }
+
+void tools::ParseMPreferVectorWidthOption(clang::DiagnosticsEngine &Diags,
+                                          const llvm::opt::ArgList &Args,
+                                          ArgStringList &CmdArgs,
+                                          bool isCompilerDriver) {
+  // If this was invoked by the Compiler Driver, we pass through the option
+  // as-is. Otherwise, if this is the Frontend Driver, we want just the value.
+  StringRef Out = (isCompilerDriver) ? "-mprefer-vector-width=" : "";
+
+  Arg *A = 
Args.getLastArg(clang::driver::options::OPT_mprefer_vector_width_EQ);
+  if (!A)
+    return;
+
+  StringRef Value = A->getValue();
+  unsigned Width;
+
+  // Only "none" and Integer values are accepted by
+  // -mprefer-vector-width=<value>.
+  if (Value != "none" && Value.getAsInteger(10, Width)) {
+    Diags.Report(clang::diag::err_drv_invalid_value)
+        << A->getOption().getName() << Value;
+    return;
+  }
+
+  CmdArgs.push_back(Args.MakeArgString(Out + Value));
----------------
mcinally wrote:

> Just to clarify, by warnings you mean warnings when building clang itself, 
> right?

Correct. The unused result would be a warning.

> Is the real issue here that we need to validate the argument values? It is a 
> fairly common pattern in, e.g. 
> [clang/lib/Frontend/CompilerInvocation.cpp](https://github.com/llvm/llvm-project/blob/main/clang/lib/Frontend/CompilerInvocation.cpp)
>  to look at the ArgList and parse the arguments there.

Yes, that's what we're trying to avoid. Both `Frontend/CompilerInvocation.cpp` 
and `Driver/FC]lang.cpp` could do verification, but we don't want to have the 
same/similar code in both places. This patch would let 
`Frontend/CompilerInvocation.cpp` and `Driver/[FC]lang.cpp` share the same 
parsing/handling functions.

>  It may be better to share the argument validation and return, say, an 
> optional value with the correctly parsed value if it exists. How does that 
> sound?

I like that!!

I'll wait to see if there are other ideas before updating the PR.

https://github.com/llvm/llvm-project/pull/142800
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to