<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic macro evaluation in compiler in CodeWarrior for MCU</title>
    <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/macro-evaluation-in-compiler/m-p/211354#M8322</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I am currently using "&lt;SPAN style="font-size: 1;"&gt;ANSI-C/cC++ Compiler for HC12 V-5.0.40 Build 10020, Jan 21 2010&lt;/SPAN&gt;".&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In my code, I am using this macro:&lt;/P&gt;&lt;DIV&gt;&lt;PRE&gt;&lt;SPAN&gt;Mk4Bytes&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;x&lt;/SPAN&gt;&lt;SPAN&gt;)&lt;/SPAN&gt; &lt;SPAN&gt;{&lt;/SPAN&gt; &lt;SPAN&gt;(((&lt;/SPAN&gt;&lt;SPAN&gt;u8&lt;/SPAN&gt;&lt;SPAN&gt;)((&lt;/SPAN&gt;&lt;SPAN&gt;x&lt;/SPAN&gt;&lt;SPAN&gt;)&lt;/SPAN&gt; &lt;SPAN&gt;&amp;gt;&amp;gt;&lt;/SPAN&gt; &lt;SPAN&gt;24&lt;/SPAN&gt;&lt;SPAN&gt;)) , ((&lt;/SPAN&gt;&lt;SPAN&gt;u8&lt;/SPAN&gt;&lt;SPAN&gt;)((&lt;/SPAN&gt;&lt;SPAN&gt;x&lt;/SPAN&gt;&lt;SPAN&gt;)&lt;/SPAN&gt; &lt;SPAN&gt;&amp;gt;&amp;gt;&lt;/SPAN&gt; &lt;SPAN&gt;16&lt;/SPAN&gt;&lt;SPAN&gt;)) , ((&lt;/SPAN&gt;&lt;SPAN&gt;u8&lt;/SPAN&gt;&lt;SPAN&gt;)((&lt;/SPAN&gt;&lt;SPAN&gt;x&lt;/SPAN&gt;&lt;SPAN&gt;)&lt;/SPAN&gt; &lt;SPAN&gt;&amp;gt;&amp;gt;&lt;/SPAN&gt; &lt;SPAN&gt;8&lt;/SPAN&gt;&lt;SPAN&gt;)) , ((&lt;/SPAN&gt;&lt;SPAN&gt;u8&lt;/SPAN&gt;&lt;SPAN&gt;)(&lt;/SPAN&gt;&lt;SPAN&gt;x&lt;/SPAN&gt;&lt;SPAN&gt;)))&lt;/SPAN&gt; &lt;SPAN&gt;}&lt;/SPAN&gt;&lt;/PRE&gt;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;which I have used successfully in other compilers. However, when I used it to initialize a field in a structure, I got the error "ERROR C2207: Initializer must be constant"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is a excerpt of my code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;typedef unsigned char u8;&lt;/P&gt;&lt;P&gt;typedef struct {&lt;/P&gt;&lt;P&gt;u8 partnumber[4];&lt;/P&gt;&lt;P&gt;u8 version[2];&lt;/P&gt;&lt;P&gt;}&amp;nbsp; tIDheader;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;#pragma CONST_SEG IDHEADER&lt;/P&gt;&lt;P&gt;const tIDheader IDheader = {&lt;/P&gt;&lt;P&gt;Mk4Bytes(12345678),&lt;/P&gt;&lt;P&gt;"A0"&lt;/P&gt;&lt;P&gt;};&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;While I know that I could simply change the field definition to use "unsigned long" instead of an array of bytes, the struct is actually defined in automatically generated code, so I am not allowed to change the definition.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Anyway, the value given to the macro is a constant so the resulting expression is a constant expression, so I would expect the compiler to replace the expression with { 0x00, 0xBC, 0x61, 0x4E }, which is a constant initializer.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As I said, I have used this macro successfully&amp;nbsp;with other compilers. Is this a bug with this compiler?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 07 Jul 2010 00:47:02 GMT</pubDate>
    <dc:creator>rlw</dc:creator>
    <dc:date>2010-07-07T00:47:02Z</dc:date>
    <item>
      <title>macro evaluation in compiler</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/macro-evaluation-in-compiler/m-p/211354#M8322</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I am currently using "&lt;SPAN style="font-size: 1;"&gt;ANSI-C/cC++ Compiler for HC12 V-5.0.40 Build 10020, Jan 21 2010&lt;/SPAN&gt;".&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In my code, I am using this macro:&lt;/P&gt;&lt;DIV&gt;&lt;PRE&gt;&lt;SPAN&gt;Mk4Bytes&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;x&lt;/SPAN&gt;&lt;SPAN&gt;)&lt;/SPAN&gt; &lt;SPAN&gt;{&lt;/SPAN&gt; &lt;SPAN&gt;(((&lt;/SPAN&gt;&lt;SPAN&gt;u8&lt;/SPAN&gt;&lt;SPAN&gt;)((&lt;/SPAN&gt;&lt;SPAN&gt;x&lt;/SPAN&gt;&lt;SPAN&gt;)&lt;/SPAN&gt; &lt;SPAN&gt;&amp;gt;&amp;gt;&lt;/SPAN&gt; &lt;SPAN&gt;24&lt;/SPAN&gt;&lt;SPAN&gt;)) , ((&lt;/SPAN&gt;&lt;SPAN&gt;u8&lt;/SPAN&gt;&lt;SPAN&gt;)((&lt;/SPAN&gt;&lt;SPAN&gt;x&lt;/SPAN&gt;&lt;SPAN&gt;)&lt;/SPAN&gt; &lt;SPAN&gt;&amp;gt;&amp;gt;&lt;/SPAN&gt; &lt;SPAN&gt;16&lt;/SPAN&gt;&lt;SPAN&gt;)) , ((&lt;/SPAN&gt;&lt;SPAN&gt;u8&lt;/SPAN&gt;&lt;SPAN&gt;)((&lt;/SPAN&gt;&lt;SPAN&gt;x&lt;/SPAN&gt;&lt;SPAN&gt;)&lt;/SPAN&gt; &lt;SPAN&gt;&amp;gt;&amp;gt;&lt;/SPAN&gt; &lt;SPAN&gt;8&lt;/SPAN&gt;&lt;SPAN&gt;)) , ((&lt;/SPAN&gt;&lt;SPAN&gt;u8&lt;/SPAN&gt;&lt;SPAN&gt;)(&lt;/SPAN&gt;&lt;SPAN&gt;x&lt;/SPAN&gt;&lt;SPAN&gt;)))&lt;/SPAN&gt; &lt;SPAN&gt;}&lt;/SPAN&gt;&lt;/PRE&gt;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;which I have used successfully in other compilers. However, when I used it to initialize a field in a structure, I got the error "ERROR C2207: Initializer must be constant"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is a excerpt of my code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;typedef unsigned char u8;&lt;/P&gt;&lt;P&gt;typedef struct {&lt;/P&gt;&lt;P&gt;u8 partnumber[4];&lt;/P&gt;&lt;P&gt;u8 version[2];&lt;/P&gt;&lt;P&gt;}&amp;nbsp; tIDheader;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;#pragma CONST_SEG IDHEADER&lt;/P&gt;&lt;P&gt;const tIDheader IDheader = {&lt;/P&gt;&lt;P&gt;Mk4Bytes(12345678),&lt;/P&gt;&lt;P&gt;"A0"&lt;/P&gt;&lt;P&gt;};&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;While I know that I could simply change the field definition to use "unsigned long" instead of an array of bytes, the struct is actually defined in automatically generated code, so I am not allowed to change the definition.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Anyway, the value given to the macro is a constant so the resulting expression is a constant expression, so I would expect the compiler to replace the expression with { 0x00, 0xBC, 0x61, 0x4E }, which is a constant initializer.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As I said, I have used this macro successfully&amp;nbsp;with other compilers. Is this a bug with this compiler?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 07 Jul 2010 00:47:02 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/macro-evaluation-in-compiler/m-p/211354#M8322</guid>
      <dc:creator>rlw</dc:creator>
      <dc:date>2010-07-07T00:47:02Z</dc:date>
    </item>
    <item>
      <title>Re: macro evaluation in compiler</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/macro-evaluation-in-compiler/m-p/211355#M8323</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The provided macro contains one pair of braces too much .&lt;/P&gt;&lt;P&gt;Try:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;#define Mk4Bytes(x) { ((u8)((x) &amp;gt;&amp;gt; 24)) , ((u8)((x) &amp;gt;&amp;gt; 16)) , ((u8)((x) &amp;gt;&amp;gt; 8)) , ((u8)(x)) }&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The original code contained an expression like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;char buf[1] = {(1,2)};&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;using (1,2) can be taken by some compilers as comma expression with a value of 2. But it is obviously not what was intended, I don't think the comma operator is legal in a constant expression in C, I did not look it up though. In C++ the code would be legal (well legal, but still not do what was intended ...).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Daniel&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 07 Jul 2010 04:28:29 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/macro-evaluation-in-compiler/m-p/211355#M8323</guid>
      <dc:creator>CompilerGuru</dc:creator>
      <dc:date>2010-07-07T04:28:29Z</dc:date>
    </item>
    <item>
      <title>Re: macro evaluation in compiler</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/macro-evaluation-in-compiler/m-p/211356#M8324</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Another issue, not connected with the macro, but with the structure initialisation, is that the null termination byte&amp;nbsp;associated with the string has not been allowed for within the second structure element.&amp;nbsp; This results in a compiler warning, but the expected result occurs.&amp;nbsp; To avoid this warning, perhaps the following should be used.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;const tIDheader IDheader = {&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; Mk4Bytes(12345678),&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; {'A','0'}&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;};&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Mac&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 07 Jul 2010 12:46:25 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/macro-evaluation-in-compiler/m-p/211356#M8324</guid>
      <dc:creator>bigmac</dc:creator>
      <dc:date>2010-07-07T12:46:25Z</dc:date>
    </item>
    <item>
      <title>Re: macro evaluation in compiler</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/macro-evaluation-in-compiler/m-p/211357#M8325</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 07 Jul 2010 21:43:39 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/macro-evaluation-in-compiler/m-p/211357#M8325</guid>
      <dc:creator>rlw</dc:creator>
      <dc:date>2010-07-07T21:43:39Z</dc:date>
    </item>
    <item>
      <title>Re: macro evaluation in compiler</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/macro-evaluation-in-compiler/m-p/211358#M8326</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;A more readable version would be:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;#define x 12345678

#define x0 ((u8)(x &amp;gt;&amp;gt; 24))
#define x1 ((u8)(x &amp;gt;&amp;gt; 16))
#define x2 ((u8)(x &amp;gt;&amp;gt;  8))
#define x3 ((u8)(x      ))


const tIDheader IDheader =
{
  {x0, x1, x2, x3},
  {'A','0'}
};&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 08 Jul 2010 16:01:20 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/macro-evaluation-in-compiler/m-p/211358#M8326</guid>
      <dc:creator>Lundin</dc:creator>
      <dc:date>2010-07-08T16:01:20Z</dc:date>
    </item>
    <item>
      <title>Re: macro evaluation in compiler</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/macro-evaluation-in-compiler/m-p/211359#M8327</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Even though either style of macro solution gives the intended outcome, I notice that a couple of C2705 compiler warning messages are generated with with either approach.&amp;nbsp; This is for "Possible loss of data", which in this case&amp;nbsp;is not of any consequence.&amp;nbsp; However,&amp;nbsp;attempts to eliminate&amp;nbsp;the warning&amp;nbsp;(other than globally inhibiting them) have failed.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;HR /&gt;&lt;P&gt;Lundin wrote:&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;PRE&gt;#define x 12345678

#define x0 ((u8)(x &amp;gt;&amp;gt; 24))
#define x1 ((u8)(x &amp;gt;&amp;gt; 16))
#define x2 ((u8)(x &amp;gt;&amp;gt;  8))
#define x3 ((u8)(x      ))

const tIDheader IDheader =
{
  {x0, x1, x2, x3},
  {'A','0'}
};&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;Using Lundin's approach, I can identify that a warning is associated with&amp;nbsp;each of&amp;nbsp;the x2 and x3 macros, but not the x0 and x1 macros.&amp;nbsp; The x value is obviously a 32 bit quantity, so there is no ambiguity here.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am curious as to the cause of the warnings.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Mac&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 09 Jul 2010 13:01:10 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/macro-evaluation-in-compiler/m-p/211359#M8327</guid>
      <dc:creator>bigmac</dc:creator>
      <dc:date>2010-07-09T13:01:10Z</dc:date>
    </item>
    <item>
      <title>Re: macro evaluation in compiler</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/macro-evaluation-in-compiler/m-p/211360#M8328</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What is the type associated with tIDheader?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;CrasyCat&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 09 Jul 2010 13:56:46 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/macro-evaluation-in-compiler/m-p/211360#M8328</guid>
      <dc:creator>CrasyCat</dc:creator>
      <dc:date>2010-07-09T13:56:46Z</dc:date>
    </item>
    <item>
      <title>Re: macro evaluation in compiler</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/macro-evaluation-in-compiler/m-p/211361#M8329</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello CrasyCat,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The OP defined the following types -&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;typedef unsigned char u8;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;typedef struct {&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; u8 partnumber[4];&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; u8 version[2];&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;}&amp;nbsp;tIDheader;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Mac&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 09 Jul 2010 23:43:42 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/macro-evaluation-in-compiler/m-p/211361#M8329</guid>
      <dc:creator>bigmac</dc:creator>
      <dc:date>2010-07-09T23:43:42Z</dc:date>
    </item>
  </channel>
</rss>

