CppAst.CodeGen icon indicating copy to clipboard operation
CppAst.CodeGen copied to clipboard

fails to generate enum value based on macro definition

Open StephenHodgson opened this issue 4 years ago • 12 comments

When attempting to generate an enum using a macro definition the code generated does not compile.

Current

c++

/*! Macro to set a the prefix bytes of an API specific Result code. */
#define RESULT_PREFIX(val) (val << 16)

enum {
  /*! Defines the prefix for global Result codes. */
  ResultAPIPrefix_Global = RESULT_PREFIX(0),
};

enum {
  /*! Defines the prefix for AudioResult codes. */
  ResultAPIPrefix_Audio = RESULT_PREFIX(0x9e11)
};

C#

public enum __AnonymousCppEnum_api_3920AnonymousEnum : int
{
    /// <summary>
    /// Defines the prefix for global Result codes.
    /// </summary>
    ResultAPIPrefix_Global = unchecked((int)(0  16)enum{/*! Defines the prefix for global Result codes. */ResultAPIPrefix_Global=RESULT_PREFIX(0))),
}

public enum __AnonymousCppEnum_audio_8721AnonymousEnum : int
{
    /// <summary>
    /// Defines the prefix for AudioResult codes.
    /// </summary>
    ResultAPIPrefix_Audio = unchecked((int)(0x9e11  )), // Note this value should have also been computed using the macro.
}

Expected

public enum __AnonymousCppEnum_api_3920AnonymousEnum : int
{
    /// <summary>
    /// Defines the prefix for global Result codes.
    /// </summary>
    ResultAPIPrefix_Global = unchecked((int)0 << (int)16),
}

public enum __AnonymousCppEnum_audio_8721AnonymousEnum : int
{
    /// <summary>
    /// Defines the prefix for AudioResult codes.
    /// </summary>
    ResultAPIPrefix_Audio = unchecked((int)0x9e11 << (int)16),
}

StephenHodgson avatar Nov 10 '19 22:11 StephenHodgson

I'm not entirely sure what the result of this should look like, but probably a calculated value for the enum?

It should print the characters, don't know why it is not working.

PR much welcome for the various issues, as I don't have any time left dedicated for this project these days...

xoofx avatar Nov 10 '19 22:11 xoofx

PR much welcome for the various issues, as I don't have any time left dedicated for this project these days...

No worries, I understand. I will do my best, but I'm def not sure about this one in particular. The others I was planning on opening PRs if I solved them.

StephenHodgson avatar Nov 10 '19 22:11 StephenHodgson

@xoofx this actually seems to be a problem in the CppAst project, as the cpp macro isn't properly applied to the CppEnumItem.ValueExpression.

I'm trying to track down exactly what's going on but a bit slow going.

StephenHodgson avatar Nov 25 '19 17:11 StephenHodgson

@xoofx where exactly are the macros applied to the expressions?

StephenHodgson avatar Nov 25 '19 19:11 StephenHodgson

you mean this?

xoofx avatar Nov 25 '19 20:11 xoofx

No, I've already fixed that in https://github.com/xoofx/CppAst.CodeGen/pull/8

StephenHodgson avatar Nov 25 '19 20:11 StephenHodgson

I'm talking about more lower level in the CppAst model builder, where we're gathering all the enum expression values.

StephenHodgson avatar Nov 25 '19 20:11 StephenHodgson

https://github.com/xoofx/CppAst/blob/b27338c68fae3ab189ad58573b27a7e3547f98a9/src/CppAst/CppModelBuilder.cs#L192-L205

xoofx avatar Nov 25 '19 20:11 xoofx

Correct. That's where I'm currently looking.

StephenHodgson avatar Nov 25 '19 20:11 StephenHodgson

So after stepping through each of this I was able to figure out that the clang tokenizer is returning back invalid tokens.

StephenHodgson avatar Dec 04 '19 18:12 StephenHodgson

Seems to be fixed. Thanks!

StephenHodgson avatar May 07 '20 03:05 StephenHodgson

Nope, I was mistaken, I must have updated my headers with the precomputed values

StephenHodgson avatar May 07 '20 04:05 StephenHodgson