This is an archive of the discontinued LLVM Phabricator instance.

[libclang] Add CXBinaryOperatorKind and CXUnaryOperatorKind (implements 29138)
ClosedPublic

Authored by MineGame159 on May 18 2023, 2:30 PM.

Details

Summary

Adds 2 new functions to the C libclang api for retrieving operator kinds for binary and unary operators from cursors. Also adds 2 functions for retrieving the spelling of the new enums. This differential implements #29138.

Diff Detail

Event Timeline

MineGame159 created this revision.May 18 2023, 2:30 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 18 2023, 2:30 PM
Herald added a subscriber: arphaman. · View Herald Transcript
MineGame159 requested review of this revision.May 18 2023, 2:30 PM

Thank you for the additions! This should have some amount of test coverage added to clang/unittests/libclang/LibclangTest.cpp and a release note.

clang/tools/libclang/CIndex.cpp
9611

Instead of implementing this manually, I think this should do:

return cxstring::createRef(BinaryOperator::getOpcodeStr(kind - 1));

(Similar for unary operators, may need casts to compile properly).

9688

Please spell out the type.

9690–9694

Similar for unary operators.

Implemented requested changes

MineGame159 marked 3 inline comments as done.Jun 8 2023, 4:35 AM

I kinda thought the undefined reference error is just something I broke on my machine but guess not. Don't really know what can cause it since it can link to other functions from there and the function exists.

vedgy added a comment.Jun 8 2023, 5:33 AM

I kinda thought the undefined reference error is just something I broke on my machine but guess not. Don't really know what can cause it since it can link to other functions from there and the function exists.

I suspect the reason for the failed build is that you haven't updated clang/tools/libclang/libclang.map. Also CINDEX_VERSION_MINOR in clang/include/clang-c/Index.h needs to be incremented, unless it has been incremented in the targeted LLVM release version already. See e.g. cc929590ad305f0d068709c7c7999f5fc6118dc9.

MineGame159 updated this revision to Diff 529663.EditedJun 8 2023, 10:23 AM

I have added the new functions into the file and now it successfully compiles the test. (but fails when running it - EDIT: guess I broke my dev env somehow)

Also checked and made sure that CINDEX_VERSION_MINOR was already incremented in LLVM 17.
Another thing I am not sure is which naming convention I should be using. Some functions in the C api use clang_getCursorBinaryOperatorKind and others clang_Cursor_getBinaryOperatorKind.

I have added the new functions into the file and now it successfully compiles the test. (but fails when running it - EDIT: guess I broke my dev env somehow)

Also checked and made sure that CINDEX_VERSION_MINOR was already incremented in LLVM 17.
Another thing I am not sure is which naming convention I should be using. Some functions in the C api use clang_getCursorBinaryOperatorKind and others clang_Cursor_getBinaryOperatorKind.

The style you're using currently is fine.

clang/docs/ReleaseNotes.rst
731
clang/tools/libclang/CIndex.cpp
9620–9624

Sorry, I was unclear before. :-) Our style guide is to not use auto unless the type is spelled out explicitly in the RHS of the assignment (basically, use auto when it lets you avoid repeating the type name). So these uses with dyn_cast (and friends) should use auto, while a call like getCursorExpr() should spell out the type explicitly.

9640
clang/unittests/libclang/LibclangTest.cpp
1143
1160
MineGame159 marked 5 inline comments as done.
aaron.ballman accepted this revision.Jun 9 2023, 6:22 AM

LGTM! Do you need me to commit on your behalf? If so, what name and email address would you like me to use for patch attribution?

This revision is now accepted and ready to land.Jun 9 2023, 6:22 AM

LGTM! Do you need me to commit on your behalf? If so, what name and email address would you like me to use for patch attribution?

Yeah, this is my first contribution, thanks. My name can be MineGame159 and email petulko08@gmail.com.

This revision was landed with ongoing or failed builds.Jun 9 2023, 7:03 AM
This revision was automatically updated to reflect the committed changes.