现在的位置: 首页 > 综合 > 正文

Studying note of GCC-3.4.6 source (72)

2013年04月15日 ⁄ 综合 ⁄ 共 4646字 ⁄ 字号 评论关闭

5.1.2. C builtin macros

Below flag_undef is 0 by default, but it can be revised by option –undef, which if is nonzero, means do not predefine any system-specific or GCC-specific macros, but still predefine the standard predefined macros (i.e, macros defined in builtin_array).

 

295  void

296  c_cpp_builtins (cpp_reader *pfile)                                                       in c-cppbuiltin.c

297  {

298    /* -undef turns off target-specific built-ins.  */

299    if (flag_undef)

300      return;

301 

302    define__GNUC__ ();

5.1.2.1.    Macros of __GUNC__ family

In GCC, predefined macro __GNUC__ is always defined as the major version number of the compiler. For example, if the compiler version number is 3.4.6, this macro is defined as 3; while __GNUC_MINOR__, __GNUC_PATCHLEVEL__ are defined as 4 and 6 respectively. Further macro __GNUG__ is similar with __GNC__ but for C++. define__GNUC__ defines these macros from version_string which saves the version literal string.

 

252  static void

253  define__GNUC__ (void)                                                                     in c-cppbuiltin.c

254  {

255    /* The format of the version string, enforced below, is

256      ([^0-9]*-)?[0-9]+[.][0-9]+([.][0-9]+)?([- ].*)?  */

257    const char *q, *v = version_string;

258 

259    while (*v && ! ISDIGIT (*v))

260      v++;

261    if (!*v || (v > version_string && v[-1] != '-'))

262      abort ();

263 

264    q = v;

265    while (ISDIGIT (*v))

266      v++;

267    builtin_define_with_value_n ("__GNUC__", q, v - q);

268    if (c_dialect_cxx ())

269      builtin_define_with_value_n ("__GNUG__", q, v - q);

270 

271    if (*v != '.' || !ISDIGIT (v[1]))

272      abort ();

273    q = ++v;

274    while (ISDIGIT (*v))

275      v++;

276    builtin_define_with_value_n ("__GNUC_MINOR__", q, v - q);

277 

278    if (*v == '.')

279    {

280      if (!ISDIGIT (v[1]))

281        abort ();

282      q = ++v;

283      while (ISDIGIT (*v))

284        v++;

285      builtin_define_with_value_n ("__GNUC_PATCHLEVEL__", q, v - q);

286    }

287    else

288     builtin_define_with_value_n ("__GNUC_PATCHLEVEL__", "0", 1);

289 

290    if (*v && *v != ' ' && *v != '-')

291      abort ();

292  }

 

builtin_define_with_value_n will combine arguments macro and expansion to form definition pair and pass it to cpp_define.

 

492  static void

493  builtin_define_with_value_n (const char *macro, const char *expansion, size_t elen)

494  {

495    char *buf;

496    size_t mlen = strlen (macro);

497 

498    /* Space for an = and a NUL.  */

499    buf = alloca (mlen + elen + 2);

500    memcpy (buf, macro, mlen);

501    buf[mlen] = '=';

502    memcpy (buf + mlen + 1, expansion, elen);

503    buf[mlen + elen + 1] = '/0';

504 

505    cpp_define (parse_in, buf);

506  }

 

cpp_define also handles command line defined macros, which has form of m=val or m. For former ones, it needs be transformed into m val, and for rear ones, should be m 1. Then this regular form can be handled by run_directive which we have seen above.

 

1804 void

1805 cpp_define (cpp_reader *pfile, const char *str)                                             in cpplib.c

1806 {

1807   char *buf, *p;

1808   size_t count;

1809

1810   /* Copy the entire option so we can modify it.

1811     Change the first "=" in the string to a space. If there is none,

1812     tack " 1" on the end.  */

1813

1814   count = strlen (str);

1815   buf = alloca (count + 3);

1816   memcpy (buf, str, count);

1817

1818   p = strchr (str, '=');

1819   if (p)

1820     buf[p - str] = ' ';

1821   else

1822   {

1823     buf[count++] = ' ';

1824     buf[count++] = '1';

1825   }

1826   buf[count] = '/n';

1827

1828   run_directive (pfile, T_DEFINE, buf, count);

1829 }

5.1.2.2.    Macros in stddef.h

C++ defines some predefined macros in system header file stddef.h. They are size_t, ptrdiff_t, wchar, and wint. c_stddef_cpp_builtins helps to create the related objects for these macros.

 

c_cpp_builtins (continue)

 

304    /* For stddef.h. They require macros defined in c-common.c.  */

305    c_stddef_cpp_builtins ();

 

In below invocations, the second parameters, like SIZE_TYPE, are target related, and they are literal strings standing for specified type.

 

4249 void

4250 c_stddef_cpp_builtins(void)                                                                              in c-common.c

4251 {

4252   builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);

4253   builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);

4254   builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);

4255   builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);

4256 }

 

See that builtin_define_with_value puts macro and expansion tegother into macro=expansion, however in cpp_define, we have seen, the expression will be transformed into macro expansion and run_directive is invoked.

 

470  void

471  builtin_define_with_value (const char *macro, const char *expansion, int is_str)

472  {

473    char *buf;

474    size_t mlen = strlen (macro);

475    size_t elen = strlen (expansion);

476    size_t extra = 2;  /* space for an = and a NUL */

477 

478    if (is_str)

479      extra += 2;  /* space for two quote marks */

480 

481    buf = alloca (mlen + elen + extra);

482    if (is_str)

483      sprintf (buf, "%s=/"%s/"", macro, expansion);

484    else

485      sprintf (buf, "%s=%s", macro, expansion);

486 

487    cpp_define (parse_in, buf);

488  }

 

Following are controlling macros, they describe the characteristics about the compiler, and the target machine.

抱歉!评论已关闭.