This is an archive of the discontinued LLVM Phabricator instance.

Add __attribute__((warn_unused_result)) to LLVMErrorRef
Needs ReviewPublic

Authored by dblaikie on Jun 16 2023, 12:40 PM.

Details

Summary

Sending this out a bit half-baked unfortunately - we added support for
this context a couple of years ago in D102122 (which turned up in clang 15
onwards: https://godbolt.org/z/haz8qrTT3 ), not sure if the macro detection
will correctly detect that the attribute is usable in a typedef context...

Diff Detail

Unit TestsFailed

Event Timeline

dblaikie created this revision.Jun 16 2023, 12:40 PM
Herald added a project: Restricted Project. · View Herald Transcript
dblaikie requested review of this revision.Jun 16 2023, 12:40 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 16 2023, 12:40 PM

Useful in my view:)

aaron.ballman added inline comments.Jun 18 2023, 5:07 AM
llvm/examples/OrcV2Examples/OrcV2CBindingsLazy/OrcV2CBindingsLazy.c
198

Keep in mind, I know nothing about Orc. :-D

It seems that the only in-tree uses of this API that check results are the unit tests. Perhaps this function's return type is inappropriate? Or does this suggest we should be marking individual APIs instead of the return type itself?

llvm/include/llvm-c/ExternC.h
47–51

Hmm, worth it to add __has_c_attribute(nodiscard) as a branch, or given C2x's newness, not worth it?

dblaikie added inline comments.Jun 20 2023, 1:31 PM
llvm/include/llvm-c/ExternC.h
47–51

Does C2x support the attribute on typedefs? (the C++ committee wasn't so enthusiastic about that change & I don't follow the C committee directly)

I'm happy enough to include C support, if it applies.

aaron.ballman added inline comments.Jun 21 2023, 5:49 AM
llvm/include/llvm-c/ExternC.h
47–51

Ah good point, it's not allowed on typedefs there either. Neeeevermind. :-)

dblaikie added inline comments.Jun 21 2023, 12:50 PM
llvm/include/llvm-c/ExternC.h
47–51

could rename the macro WARN_UNUSED_RESULT rather than NODISCARD_GNU_STYLE?

aaron.ballman added inline comments.Jun 22 2023, 4:54 AM
llvm/include/llvm-c/ExternC.h
47–51

Hmmm, either seems about equally as confusing. What about LLVM_NODISCARD_TYPEDEF to make it clear that this should be used for typedefs and leave it to another macro to handle non-discardable function markings?

dblaikie added inline comments.Jun 22 2023, 2:28 PM
llvm/include/llvm-c/ExternC.h
47–51

I guess, sure.

I guess this also needs a more narrow macro check to verify the clang version (since it seems we support back to clang 5, and this only works on clang 15 and above) - I can't seem to find an example of a macro check for clang version in any of our Compiler.h and similar macro-y/compat headers in LLVM - do you happen to know of an example off-hand?

Adding warn-unused attribute(s) either to LLVMErrorRef (if possible) or to the individual APIs seems like a great idea to me. No opinion on the mechanics -- I don't know anything about the relevant C attributes.

llvm/examples/OrcV2Examples/OrcV2CBindingsLazy/OrcV2CBindingsLazy.c
198

The return type is appropriate, I've just been lazy about checking it -- adding warn-unused seems like a big win for that reason. :)

Rather than casting to void I should add an LLVMCantFail macro that asserts that the result is LLVMErrorSuccess.