Skip to content

[llvm-objcopy] Check for missing argument values #70710

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

quic-akaryaki
Copy link
Contributor

Report an error if a required value for a command line argument is missing.

@llvmbot
Copy link
Member

llvmbot commented Oct 30, 2023

@llvm/pr-subscribers-llvm-binary-utilities

Author: None (quic-akaryaki)

Changes

Report an error if a required value for a command line argument is missing.


Full diff: https://github.com/llvm/llvm-project/pull/70710.diff

2 Files Affected:

  • (added) llvm/test/tools/llvm-objcopy/tool-options.test (+4)
  • (modified) llvm/tools/llvm-objcopy/ObjcopyOptions.cpp (+6)
diff --git a/llvm/test/tools/llvm-objcopy/tool-options.test b/llvm/test/tools/llvm-objcopy/tool-options.test
new file mode 100644
index 000000000000000..7116ff39485562b
--- /dev/null
+++ b/llvm/test/tools/llvm-objcopy/tool-options.test
@@ -0,0 +1,4 @@
+## An error must be reported if a required argument value is missing.
+# RUN: not llvm-objcopy --only-section 2>&1 | FileCheck --check-prefix=CHECK-NO-VALUE %s
+# RUN: not llvm-objcopy -O 2>&1 | FileCheck --check-prefix=CHECK-NO-VALUE %s
+# CHECK-NO-VALUE: error: argument to '{{.*}}' is missing
diff --git a/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp b/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp
index d33adb0b6a3e478..63c3873b1e94340 100644
--- a/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp
+++ b/llvm/tools/llvm-objcopy/ObjcopyOptions.cpp
@@ -552,6 +552,12 @@ objcopy::parseObjcopyOptions(ArrayRef<const char *> RawArgsArr,
   llvm::opt::InputArgList InputArgs =
       T.ParseArgs(ArgsArr, MissingArgumentIndex, MissingArgumentCount);
 
+  if (MissingArgumentCount)
+    return createStringError(
+        errc::invalid_argument,
+        "argument to '%s' is missing (expected %d value(s))",
+        InputArgs.getArgString(MissingArgumentIndex), MissingArgumentCount);
+
   if (InputArgs.size() == 0 && DashDash == RawArgsArr.end()) {
     printHelp(T, errs(), ToolType::Objcopy);
     exit(1);

Copy link
Collaborator

@alexander-shaposhnikov alexander-shaposhnikov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to me LG, I'd wait for @jh7370

## An error must be reported if a required argument value is missing.
# RUN: not llvm-objcopy --only-section 2>&1 | FileCheck --check-prefix=CHECK-NO-VALUE %s
# RUN: not llvm-objcopy -O 2>&1 | FileCheck --check-prefix=CHECK-NO-VALUE %s
# CHECK-NO-VALUE: error: argument to '{{.*}}' is missing
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you using a wildcard pattern here? It seems sensible to properly check the message generated. This also is only testing part of the message, but there are other parts that are important too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am using a wildcard to reuse one pattern for several (two) different options. The point of this test is to verify that the message gets printed. I think there is enough context in the message to avoid misidentification for other messages.
I am not checking if the message is exactly the same because I don't see it as a realistic possibility.
I don't think a random corruption in the output is likely, and testing for it would be outside of the scope of this test.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is enough context in the message to avoid misidentification for other messages.

Agreed.

I don't think a random corruption in the output is likely, and testing for it would be outside of the scope of this test.

This isn't why I think you should check the exact contents. Checking the exact contents explicitly will show that you've used the correct %* stuff in your createStringError call. Equally, should the code change in the future to use a different mechanism to parse the command-line/report the error/etc, it would show that the change is still reporting the context correctly.

@@ -0,0 +1,4 @@
## An error must be reported if a required argument value is missing.
# RUN: not llvm-objcopy --only-section 2>&1 | FileCheck --check-prefix=CHECK-NO-VALUE %s
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: since at the moment there is only one check prefix in the entire test you can simply use the default one (CHECK)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, but I thought this test could be expanded in the future and in that case, it will be possible to add other patterns without rewriting existing ones.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry it was too strong. It is certainly possibly to add new check prefixes without rewriting the default one, but then the default one would stand out awkwardly, in my opinion.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless you have explicit plans to extend the test in the near future, go with the simpler approach now (default prefix). If we need to change it in the future, we can.

# RUN: not llvm-objcopy -O 2>&1 | FileCheck --check-prefix=CHECK-NO-VALUE %s
# CHECK-NO-VALUE: error: argument to '{{.*}}' is missing
# RUN: not llvm-objcopy --only-section 2>&1 | FileCheck --check-prefix=CHECK-NO-VALUE-ONLY-SECTION %s
# CHECK-NO-VALUE-ONLY-SECTION: error: argument to '--only-section' is missing
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're still not checking the latter part of the message, and in particular the substitution for the argument count.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the check for the full message in 49c078a. Thanks.

Copy link
Collaborator

@jh7370 jh7370 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there any options that expect 2+ values (i.e. something that would give a different response to "expected 1 value(s)"? If so, it might be worth switching one of the cases for that one, just for additional bonus coverage without additional test complexity.

Otherwise, LGTM.

@quic-akaryaki quic-akaryaki deleted the objdump-report-missing-arg-value branch April 2, 2024 20:14
@alexander-shaposhnikov
Copy link
Collaborator

LG

@quic-akaryaki quic-akaryaki restored the objdump-report-missing-arg-value branch April 2, 2024 21:13
@quic-akaryaki
Copy link
Contributor Author

I'm sorry I dropped the ball on this PR. I closed the branch as part of clean up. There may still be some value in this change.

@quic-akaryaki quic-akaryaki reopened this May 16, 2024
@quic-akaryaki
Copy link
Contributor Author

Are there any options that expect 2+ values (i.e. something that would give a different response to "expected 1 value(s)"? If so, it might be worth switching one of the cases for that one, just for additional bonus coverage without additional test complexity.

Otherwise, LGTM.

I don't think there are multi-arg options in objcopy. There are some when objcopy is invoked as llvm-install-name-tool, but they are handled separately.

@quic-akaryaki quic-akaryaki force-pushed the objdump-report-missing-arg-value branch from 4819ba4 to 6f5a5c1 Compare May 17, 2024 15:59
@quic-akaryaki quic-akaryaki force-pushed the objdump-report-missing-arg-value branch from 6f5a5c1 to 502219c Compare May 20, 2024 15:15
@quic-akaryaki
Copy link
Contributor Author

CoverageMapping/mcdc-system-headers.cpp test fails on Windows, which runs clang and filecheck only. That can't be related to this change. I'm not a frequent committer, is it an acceptable practice to merge PR with unrelated failures? I could continue rebasing until it passes, but this seems a waste of resources as each build takes a few hours.

@jh7370
Copy link
Collaborator

jh7370 commented May 22, 2024

CoverageMapping/mcdc-system-headers.cpp test fails on Windows, which runs clang and filecheck only. That can't be related to this change. I'm not a frequent committer, is it an acceptable practice to merge PR with unrelated failures? I could continue rebasing until it passes, but this seems a waste of resources as each build takes a few hours.

Yes, merging with unrelated failures is fine.

@quic-akaryaki quic-akaryaki merged commit 183beb3 into llvm:main May 22, 2024
3 of 4 checks passed
@quic-akaryaki quic-akaryaki deleted the objdump-report-missing-arg-value branch May 22, 2024 14:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants