Chinaunix首页 | 论坛 | 博客
  • 博客访问: 263597
  • 博文数量: 52
  • 博客积分: 1379
  • 博客等级: 大尉
  • 技术积分: 525
  • 用 户 组: 普通用户
  • 注册时间: 2006-06-18 17:34
文章分类

全部博文(52)

文章存档

2011年(48)

2010年(4)

分类: C/C++

2011-03-05 22:04:43

  1. /* return next token with macro substitution */
  2. ST_FUNC void next(void)
  3. {
  4.     Sym *nested_list, *s;
  5.     TokenString str;
  6.     struct macro_level *ml = 0;

  7.  redo:
  8.     if (parse_flags & PARSE_FLAG_SPACES)
  9.         next_nomacro_spc();
  10.     else
  11.         next_nomacro();
  12.     if (!macro_ptr) {
  13.         //macro_ptr为空表明, 是直接从源码中读到的,而不是宏替换后的
  14.         //可能需要进行宏替换
  15.         /* if not reading from macro substituted string, then try
  16.            to substitute macros */
  17.         if (tok >= TOK_IDENT &&
  18.             (parse_flags & PARSE_FLAG_PREPROCESS)) {
  19.             //如果tok是宏, table_ident[tok-TOK_IDENT]->sym_define
  20.             //将记录它到底被定义成什么tok序列了
  21.             //tcc_define_symbol
  22.             //parse_define
  23.             //define_push

  24.             // 例子;
  25.             // #define s ss
  26.             // #define tt ttt
  27.             // #define t tt
  28.             // #define t1 t2
  29.             // #define b(x) t + t1
  30.             // #define a(d) d + s
  31.             // a(b(x));


  32.             s = define_find(tok);
  33.             if (s) {
  34.                 //a 是宏
  35.                 /* we have a macro: we try to substitute */
  36.                 tok_str_new(&str);
  37.                 nested_list = NULL;
  38.                 ml = NULL;
  39.                 //进行替换, tok序列保存在str中, 结果由macro_ptr保存
  40.                 if (macro_subst_tok(&str, &nested_list, s, &ml) == 0) {
  41.                     /* substitution done, NOTE: maybe empty */
  42.                     tok_str_add(&str, 0);
  43.                     macro_ptr = str.str;
  44.                     macro_ptr_allocated = str.str;
  45.                     goto redo;
  46.                 }
  47.             }
  48.         }
  49.     } else {
  50.         if (tok == 0) {
  51.             /* end of macro or end of unget buffer */
  52.             if (unget_buffer_enabled) {
  53.                 macro_ptr = unget_saved_macro_ptr;
  54.                 unget_buffer_enabled = 0;
  55.             } else {
  56.                 /* end of macro string: free it */
  57.                 tok_str_free(macro_ptr_allocated);
  58.                 macro_ptr_allocated = NULL;
  59.                 macro_ptr = NULL;
  60.             }
  61.             goto redo;
  62.         } else if (tok == TOK_NOSUBST) {
  63.             /* discard preprocessor's nosubst markers */
  64.             goto redo;
  65.         }
  66.     }
  67.     
  68.     /* convert preprocessor tokens into C tokens */
  69.     if (tok == TOK_PPNUM &&
  70.         (parse_flags & PARSE_FLAG_TOK_NUM)) {
  71.         parse_number((char *)tokc.cstr->data);
  72.     }
  73. }
阅读(1981) | 评论(0) | 转发(0) |
0

上一篇:git add -u

下一篇:macro_subst_tok

给主人留下些什么吧!~~