Skip to content
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

Fix wrapping of %define and #define expressions containing chars #1

Merged
merged 9 commits into from
Jun 6, 2018

Conversation

nunoguedelha
Copy link
Collaborator

This PR merges the fix swig#781, after the discussion on the issue regarding the wrapping of global defines and enums having char type elements, or being expanded with char type inputs. The chars would lose the quotes and become variables.

jiulongw added 9 commits June 5, 2018 04:27
Problem: When enum value contains compound expression with a char
constant, the quotes around char constant is missing in the generated
expression. Example:

enum media_type {
  YUY2 = ((('2' << 24) | ('Y' << 16)) | ('U' << 8)) | 'Y'
};

The generated C# enum becomes:

public enum media_type {
  YUY2 = (((2 << 24)|(Y << 16))|(U << 8))|Y
}

While the correct representation (after this fix) should be:

public enum media_type {
  YUY2 = ((('2' << 24)|('Y' << 16))|('U' << 8))|'Y'
}

Causes: the exprcompound promotes the expression type from char to int
and uses $1.val in the generated expression. However $1.val does not
contain the quotes. Since the type is promoted to int, there's no way to
know there's char component in the compound expression.

Solution: in exprcomound, use $1.rawval if $1.type is T_CHAR or T_WCHAR.
The rawval contains quotes which yield correct expression.
@nunoguedelha
Copy link
Collaborator Author

Implementation

@traversaro , this PR is a result of 2 steps:

  1. I first pushed to this repo the branch update/matlab2jaeandersson aligned with the latest commit on the https://github.com/jaeandersson/swig.git matlab branch. I suggest we later just do a fast-forward of our own matlab branch, as soon as tests are complete.
  2. I then "rebased" the fix Fix enum and #define error when value contains char in compound expression swig/swig#781 (9 commits, from e1afb6f to 9b85318) on to the branch update/matlab2jaeandersson.

Tests

The tests on simple examples work fine:

%define VOCAB(x1,x2,x3,x4) x4*16777216+x3*65536+x2*256+x1
#define VOCAB_FRAMEGRABBER_IMAGE        VOCAB('a','b','c','d')

generates:

SWIGINTERN int swigConstant(int SWIGUNUSEDPARM(resc), mxArray *resv[], int argc, mxArray *argv[]) {
...
  case 6: *resv = SWIG_Matlab_SetConstant(module_ns,"VOCAB_FRAMEGRABBER_IMAGE",SWIG_From_int(static_cast< int >('d'*16777216+'c'*65536+'b'*256+'a')));; break;

But it's failing with the yarp-matlab-bindings files. Checking again now it's not an issue with the install.

@traversaro
Copy link

But it's failing with the yarp-matlab-bindings files.

I think we can also just try to merge the mastlab branch directly with the latest version of SWIG, if it is easier.

@nunoguedelha nunoguedelha changed the title WIP: Fix wrapping of %define expressions containing chars WIP: Fix wrapping of %define and #define expressions containing chars Jun 5, 2018
@nunoguedelha nunoguedelha changed the base branch from update/matlab2jaeandersson to matlab June 5, 2018 13:01
@nunoguedelha
Copy link
Collaborator Author

The bindings generation is working fine, the problems I had earlier were probably due to leftover temporary objects involved in the SWIG processing. After rebuilding yarp-matlab-bindings the VOCABS enums and global defines were properly generated with the correcct values. I've also checked on Matlab, by comparing:

  • the yarp.VOCAB_XXXXXXX constant value
  • the formula x4*16777216+x3*65536+x2*256+x1 result
  • the result of yarp.Vocab.encode(<string>)
    For instance :
>> yarp.Vocab.encode('vel')
ans =
  int64
   7103862
>> 0*16777216+'l'*65536+'e'*256+'v'
ans =
     7103862
>> yarp.VOCAB_CM_VELOCITY
ans =
  int64
   7103862

@nunoguedelha
Copy link
Collaborator Author

Last updates on the PR:

"Our" matlab branch was pointing to the commit we've been using for a while now (260ed47).
I've fast-forwarded that branch to update/matlab2jaeandersson and updated the PR accordingly, which just changed the PR base name, not the diff obviously.

So we just need to merge the PR to matlab.

@nunoguedelha
Copy link
Collaborator Author

I had an issue with a missing SwigClear() method in the SwigRef, the superclass of every class wrapped by SWIG.
The SWIG commit cd3f6c5 - Using method for clearing swigPtr introduces the method SwigClear() definition in the SwigRef creation, but for some reason it's not working as expected.
Taking a closer look now...

@nunoguedelha
Copy link
Collaborator Author

nunoguedelha commented Jun 6, 2018

@traversaro , the issue with the missing SwiClear() was due to SwigRef.m being overwritten at configuration time by these lines. I noticed that you did those changes (robotology/yarp-matlab-bindings#11) for restoring the missing subsref and subsasgn (robotology/idyntree#188), which was a requirement for iDynTree.

Since we now have and will maintain our own fork of SWIG, I'll apply the changes directly to the SWIG source files MATLAB::createSwigRef().

@nunoguedelha
Copy link
Collaborator Author

I will make the change in a different PR, in case anyone needs the fix in this current PR just by doing a single commit cherry-pick. So I'm merging this PR now.

@nunoguedelha nunoguedelha changed the title WIP: Fix wrapping of %define and #define expressions containing chars Fix wrapping of %define and #define expressions containing chars Jun 6, 2018
@nunoguedelha nunoguedelha merged commit b6c74de into matlab Jun 6, 2018
@nunoguedelha nunoguedelha deleted the fix/wrongVocabsWrap branch June 6, 2018 07:18
@traversaro
Copy link

@traversaro , the issue with the missing SwiClear() was due to SwigRef.m being overwritten at configuration time by these lines. I noticed that you did those changes (robotology/idyntree#305) for restoring the missing subsref and subsasgn (robotology/idyntree#188), which was a requirement for iDynTree.

Oh fak, my fault in accumulating technical debt.

Let's proceed with your solution of patching our branch, but this will be problematic once the matlab branch will be merged in SWIG upstream. I think we should open an issue discussing robotology/idyntree#188 (comment) in either https://github.com/jaeandersson/swig or https://github.com/swig/swig .

@nunoguedelha
Copy link
Collaborator Author

I suggest we open it in https://github.com/jaeandersson/swig so that we avoid diverging from his matlab branch as much as possible. He commented on the issue linked to this PR, he would be interested in this fix, so there's a chance we don't diverge.

@traversaro
Copy link

@nunoguedelha I agree.

@traversaro
Copy link

Ok, for now opened an iDynTree bug to track the problem on iDynTree's side: robotology/idyntree#448 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants