GCC Wikia
Advertisement

このページを編集する際は,編集に関する方針に従ってください.[]

概要[]

引数[]

実装[]

305 /* cpp_interpret_integer converts an integer constant into a cpp_num,
306    of precision options->precision.
307 
308    We do not provide any interface for decimal->float conversion,
309    because the preprocessor doesn't need it and we don't want to
310    drag in GCC's floating point emulator.  */
311 cpp_num
312 cpp_interpret_integer (cpp_reader *pfile, const cpp_token *token,
313                        unsigned int type)
314 {
315   const uchar *p, *end;
316   cpp_num result;
317 

  • 初期化

318   result.low = 0;
319   result.high = 0;
320   result.unsignedp = !!(type & CPP_N_UNSIGNED);
321   result.overflow = false;
322 

  • 文字列の起点と終点

323   p = token->val.str.text;
324   end = p + token->val.str.len;
325 

  • 一桁は簡単

326   /* Common case of a single digit.  */
327   if (token->val.str.len == 1)
328     result.low = p[0] - '0';
329   else
330     {
331       cpp_num_part max;
332       size_t precision = CPP_OPTION (pfile, precision);
333       unsigned int base = 10, c = 0;
334       bool overflow = false;
335 

  • 8進数の"0", 16進数の"0x"を飛ばす

336       if ((type & CPP_N_RADIX) == CPP_N_OCTAL)
337         {
338           base = 8;
339           p++;
340         }
341       else if ((type & CPP_N_RADIX) == CPP_N_HEX)
342         {
343           base = 16;
344           p += 2;
345         }
346 
347       /* We can add a digit to numbers strictly less than this without
348          needing the precision and slowness of double integers.  */
349       max = ~(cpp_num_part) 0;
350       if (precision < PART_PRECISION)
351         max >>= PART_PRECISION - precision;
352       max = (max - base + 1) / base + 1;
353 

  • 最後まで処理

354       for (; p < end; p++)
355         {
356           c = *p;
357 
358           if (ISDIGIT (c) || (base == 16 && ISXDIGIT (c)))
359             c = hex_value (c);
360           else
361             break;
362 
363           /* Strict inequality for when max is set to zero.  */
364           if (result.low < max)
365             result.low = result.low * base + c;
366           else
367             {
368               result = append_digit (result, c, base, precision);
369               overflow |= result.overflow;
370               max = 0;
371             }
372         }
373 

  • オーバーフロー

374       if (overflow)
375         cpp_error (pfile, CPP_DL_PEDWARN,
376                    "integer constant is too large for its type");

  • 符号ありなのに大きすぎる場合

377       /* If too big to be signed, consider it unsigned.  Only warn for
378          decimal numbers.  Traditional numbers were always signed (but
379          we still honor an explicit U suffix); but we only have
380          traditional semantics in directives.  */
381       else if (!result.unsignedp
382                && !(CPP_OPTION (pfile, traditional)
383                     && pfile->state.in_directive)
384                && !num_positive (result, precision))
385         {
386           if (base == 10)
387             cpp_error (pfile, CPP_DL_WARNING,
388                        "integer constant is so large that it is unsigned");
389           result.unsignedp = true;
390         }
391     }
392 
393   return result;
394 }



リンク元

Advertisement