Book a Demo

Author Topic: Code Template Editor  (Read 6367 times)

ferran

  • EA User
  • **
  • Posts: 26
  • Karma: +0/-0
    • View Profile
Code Template Editor
« on: November 14, 2019, 07:42:10 pm »
Dear Sparx Forum admin,

I am currently trying to generate code from EA modifying some CTF basic templates to change the comment styling of each attribute inside an enum element.
I changed in the Code Tempalte Editor the following basic templates:

ATTRIBUTE:

%AttributeDeclaration%       %AttributeNotes%

ATTRIBUTE NOTES:

%if genOptGenComments != "T"%
%endTemplate%

%PI=""%
$wrapLen = %genOptWrapComment%
%WRAP_LINES(attNotes, $wrapLen, "/*", "*/")%

The result out of these modifications is the following generated C code:

/**
 * @brief SPI CLK  Enum of the Available SPI CLK
 */
typedef enum SPI_CLK_t
{
   SPI_CLK_1_0MHz       /*< 1.0 MHz Clock*/,
   SPI_CLK_1_2MHz       /*< 1.2 MHz Clock*/,
   SPI_CLK_2_0MHz       /*< 2.0 MHz Clock*/,
   SPI_CLK_5_3MHz       /*< 5.3 MHz Clock*/,
   SPI_CLK_8_0MHz       /*< 8.0 MHz Clock*/,
   SPI_CLK_12_0MHz       /*<12.0 MHz Clock*/,
   SPI_CLK_19_2MHz       /*<19.2 MHz Clock*/,
   SPI_CLK_24_0MHz       /*<24.0 MHz Clock*/,
   SPI_CLK_LAST       
} SPI_CLK_t;

As it can be observed, the comma after each attribute of the enum is after the comment, which is not what I exactly need.
I would like to have the following result instead:

/**
 * @brief SPI CLK  Enum of the Available SPI CLK
 */
typedef enum SPI_CLK_t
{
   SPI_CLK_1_0MHz,       /*< 1.0 MHz Clock*/
   SPI_CLK_1_2MHz,       /*< 1.2 MHz Clock*/
   SPI_CLK_2_0MHz,       /*< 2.0 MHz Clock*/
   SPI_CLK_5_3MHz,       /*< 5.3 MHz Clock*/
   SPI_CLK_8_0MHz,       /*< 8.0 MHz Clock*/
   SPI_CLK_12_0MHz,       /*<12.0 MHz Clock*/
   SPI_CLK_19_2MHz,       /*<19.2 MHz Clock*/
   SPI_CLK_24_0MHz,       /*<24.0 MHz Clock*/
   SPI_CLK_LAST       
} SPI_CLK_t;

Is that somehow possible? I understand that the code template is executed for each attribute and porbably afterwards it inserts the comma...
But I am not sure if another tempalte handle this or it is just the LIST MACRO that handles this?

Kind regards,

Ferran.

Modesto Vega

  • EA Practitioner
  • ***
  • Posts: 1183
  • Karma: +30/-8
    • View Profile
Re: Code Template Editor
« Reply #1 on: November 14, 2019, 08:38:35 pm »
Where are the enum values held? In the notes?


P.S.: Not an admin, by the way :)

ferran

  • EA User
  • **
  • Posts: 26
  • Karma: +0/-0
    • View Profile
Re: Code Template Editor
« Reply #2 on: November 14, 2019, 09:24:11 pm »
yes, exactly! the comments of each attribute are held in the Notes field.

Admin or anyone, with experience is welcome.

Modesto Vega

  • EA Practitioner
  • ***
  • Posts: 1183
  • Karma: +30/-8
    • View Profile
Re: Code Template Editor
« Reply #3 on: November 15, 2019, 05:10:00 am »
My initial thought was to use %WRAP_LINES(attNotes, $wrapLen, ", /*", "*/")%. However, your example suggests WRAP_LINES is adding a comma at the end of each line.

Could please post some additional info? Version of Sparx and type of element, specifically this element is an enumeration.

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8110
  • Karma: +119/-20
    • View Profile
Re: Code Template Editor
« Reply #4 on: November 15, 2019, 09:15:05 am »
The comma is generated by the parent list macro.

If your compiler allows you to have a trailing comma in an enumeration you could move it from there into the attribute declaration. If it doesn't things get more complicated. If you never have a comment on the last one you could generate the list into a variable and then TRIM the trailing comma.

Finally, you could add a stereotype or tagged value to the last item to specify that it shouldn't have the comma generated.

ferran

  • EA User
  • **
  • Posts: 26
  • Karma: +0/-0
    • View Profile
Re: Code Template Editor
« Reply #5 on: November 15, 2019, 08:56:14 pm »
The problem with this solution is that then at the struct class element that each attribute is concluded with a ";" character. I end up getting a ";," see example:

/**
 * @brief SPI Configuration  Structure used to configure the SPI interface
 */
typedef struct SPIConfig
{
   SPI_CLK_t clock;,      /*< SPI Clock rate*/
   SPI_DATASIZE_t datasize;,      /*< size of the data to be received and transmitted on SPI*/
   SPI_MODE_t mode;,      /*< SPI mode*/
}  SPIConfig;


/**
 * @brief SPI CLK  Enum of the Available SPI CLK
 */
typedef enum SPI_CLK_t
{
   SPI_CLK_1_0MHz = 0,      /*< 1.0 MHz Clock*/,
   SPI_CLK_1_2MHz,      /*< 1.2 MHz Clock*/,
   SPI_CLK_2_0MHz,      /*< 2.0 MHz Clock*/,
   SPI_CLK_5_3MHz,      /*< 5.3 MHz Clock*/,
   SPI_CLK_8_0MHz,      /*< 8.0 MHz Clock*/,
   SPI_CLK_12_0MHz,      /*<12.0 MHz Clock*/,
   SPI_CLK_19_2MHz,      /*<19.2 MHz Clock*/,
   SPI_CLK_24_0MHz,      /*<24.0 MHz Clock*/,
   SPI_CLK_LAST,      /*Test comment*/
} SPI_CLK_t;

Also creating an stereotype for the last element everytime, does not seem to me a good option for the SW developer engineers to spend time with.
I see that in the struct element type, the list macro is correctly creating the ";" before the comments:

/**
 * @brief SPI Configuration  Structure used to configure the SPI interface
 */
typedef struct SPIConfig
{
   SPI_CLK_t clock;      /*< SPI Clock rate*/
   SPI_DATASIZE_t datasize;      /*< size of the data to be received and transmitted on SPI*/
   SPI_MODE_t mode;      /*< SPI mode*/
}  SPIConfig;

but not for the enumeration that wait for the comment before setting the ",":

/**
 * @brief SPI CLK  Enum of the Available SPI CLK
 */
typedef enum SPI_CLK_t
{
   SPI_CLK_1_0MHz = 0      /*< 1.0 MHz Clock*/,
   SPI_CLK_1_2MHz      /*< 1.2 MHz Clock*/,
   SPI_CLK_2_0MHz      /*< 2.0 MHz Clock*/,
   SPI_CLK_5_3MHz      /*< 5.3 MHz Clock*/,
   SPI_CLK_8_0MHz      /*< 8.0 MHz Clock*/,
   SPI_CLK_12_0MHz      /*<12.0 MHz Clock*/,
   SPI_CLK_19_2MHz      /*<19.2 MHz Clock*/,
   SPI_CLK_24_0MHz      /*<24.0 MHz Clock*/,
   SPI_CLK_LAST      /*Test comment*/
} SPI_CLK_t;

Why is that? Can't it be tweaked somehow to behave like in the struct case?
We have some coding standards in the company that have to be complied with and without these small tweaks... It is difficult to defend EA to the board meeting to buy new licenses.

Can I find some other solution for this maybe? Or trigger some update of the code template editor list MACRO?

Thank you very much and sorry for the inconveniences.

Kind regards,

Ferran.

Modesto Vega

  • EA Practitioner
  • ***
  • Posts: 1183
  • Karma: +30/-8
    • View Profile
Re: Code Template Editor
« Reply #6 on: November 15, 2019, 10:10:13 pm »
The comma is generated by the parent list macro.
What is the parent list macro in this case? Class? Attribute? Something else?

ferran

  • EA User
  • **
  • Posts: 26
  • Karma: +0/-0
    • View Profile
Re: Code Template Editor
« Reply #7 on: November 16, 2019, 12:35:10 am »
class list is the parent

Modesto Vega

  • EA Practitioner
  • ***
  • Posts: 1183
  • Karma: +30/-8
    • View Profile
Re: Code Template Editor
« Reply #8 on: November 16, 2019, 02:21:40 am »
The default Class macro for C++ is

Code: [Select]
%if elemType != "Class" and elemType != "Interface" and elemType != "Enumeration"%
%endTemplate%

%elemType%
{
  %TRANSFORM_REFERENCE("Class")%
  %TRANSFORM_CURRENT("language")%
  language="C++"
%list="ClassParameter" @separator="\n" @indent="    "%
%list="ClassBase" @separator="\n" @indent="  "%
%list="ClassInterface" @separator="\n" @indent="  "%
%list="InnerClass" @separator="\n" @indent="  "%
%if elemType!="Interface"%
%list="Attribute" @separator="\n" @indent="  "%
%endIf%
%list="Attribute__Property" @separator="\n" @indent="  " attScope=="Public"%
%list="Operation" @separator="\n" @indent="  "%
}
%list="Connector" @separator="\n"%

I am assuming that @separator="\n" on list="Attribute" or list="Attribute__Property" plays a role in all of this. Is the @separator="\n" or ","?

Could you please post the Class macro?


Modesto Vega

  • EA Practitioner
  • ***
  • Posts: 1183
  • Karma: +30/-8
    • View Profile
Re: Code Template Editor
« Reply #9 on: November 16, 2019, 02:37:05 am »
I just double checked the templates. The one I posted above is a transformation template.

I am assuming that you are using a Code generation template. Is this correct?

If so, are you sure that the preceding macro is not Class Body?

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8110
  • Karma: +119/-20
    • View Profile
Re: Code Template Editor
« Reply #10 on: November 18, 2019, 10:10:05 am »
Select Code generation templates, 'Class Body' then enumeration in the second list. The separator used is ",\n"

Modesto Vega

  • EA Practitioner
  • ***
  • Posts: 1183
  • Karma: +30/-8
    • View Profile
Re: Code Template Editor
« Reply #11 on: November 18, 2019, 06:49:40 pm »
Select Code generation templates, 'Class Body' then enumeration in the second list. The separator used is ",\n"
I don’t have a computer in front of me but if Ferran  makes the following changes:
1) change the separator on the enumeration section of  'Class Body' to "\n"
2) uses  use  use %WRAP_LINES(attNotes, $wrapLen, ", /*",  "*/")% as suggested below

Would it produce the right result?

ferran

  • EA User
  • **
  • Posts: 26
  • Karma: +0/-0
    • View Profile
Re: Code Template Editor
« Reply #12 on: November 18, 2019, 07:29:48 pm »
Hi guys,

Thank you for the support, but i opted for a less elegant solution that works perfectly.

This consists of adding a never used sign like "$" in the code and after substituting this for a comma.
Only this way I can remove the trailing edge comma produced by the list Macro.

Following is my solution:

In Class Body Template:

$pubFeatures2 = %list="InnerClass" @separator="\n\n" classStereotype!="struct" and classScope=="Public" or classScope=="Package"% + "\n\n"
$pubFeatures2 = %TRIM($pubFeatures2, "\n")%
$pubFeatures2 = %REPLACE($pubFeatures2, ",", " ")%
$pubFeatures2 = %REPLACE($pubFeatures2, "&", ",")%

$pubFeatures2struct = %list="InnerClass" @separator="\n\n" classStereotype=="struct" and classScope=="Public" or classScope=="Package"% + "\n\n"
$pubFeatures2struct = %REPLACE($pubFeatures2struct, "&", " ")%

$protFeatures2 = %list="InnerClass" @separator="\n\n" classScope=="Protected"% + "\n\n"
$protFeatures2 = %TRIM($protFeatures2, "\n")%

In Attribute Template:

%if attTag:"define" == "true"%
%endTemplate%
%if attStereotype=="LAST"%
%AttributeDeclaration%      %AttributeNotes%
%endTemplate%
%AttributeDeclaration%&      %AttributeNotes%

Thank you for the support anyway.

Kind regards,

Ferran.