⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 argument-prescan.html

📁 gcc手册
💻 HTML
字号:
<html lang="en">

<head>

<title>The C Preprocessor</title>

<meta http-equiv="Content-Type" content="text/html">

<meta name="description" content="The C Preprocessor">

<meta name="generator" content="makeinfo 4.3">

<link href="http://www.gnu.org/software/texinfo/" rel="generator-home">

<!--

Copyright &copy; 1987, 1989, 1991, 1992, 1993, 1994, 1995, 1996,

1997, 1998, 1999, 2000, 2001, 2002, 2003

Free Software Foundation, Inc.



   <p>Permission is granted to copy, distribute and/or modify this document

under the terms of the GNU Free Documentation License, Version 1.1 or

any later version published by the Free Software Foundation.  A copy of

the license is included in the

section entitled "GNU Free Documentation License".



   <p>This manual contains no Invariant Sections.  The Front-Cover Texts are

(a) (see below), and the Back-Cover Texts are (b) (see below).



   <p>(a) The FSF's Front-Cover Text is:



   <p>A GNU Manual



   <p>(b) The FSF's Back-Cover Text is:



   <p>You have freedom to copy and modify this GNU Manual, like GNU

     software.  Copies published by the Free Software Foundation raise

     funds for GNU development. 

-->

</head>

<body>

<div class="node">

<p>

Node:<a name="Argument%20Prescan">Argument Prescan</a>,

Next:<a rel="next" accesskey="n" href="Newlines-in-Arguments.html#Newlines%20in%20Arguments">Newlines in Arguments</a>,

Previous:<a rel="previous" accesskey="p" href="Self-Referential-Macros.html#Self-Referential%20Macros">Self-Referential Macros</a>,

Up:<a rel="up" accesskey="u" href="Macro-Pitfalls.html#Macro%20Pitfalls">Macro Pitfalls</a>

<hr><br>

</div>



<h4 class="subsection">Argument Prescan</h4>



   <p>Macro arguments are completely macro-expanded before they are

substituted into a macro body, unless they are stringified or pasted

with other tokens.  After substitution, the entire macro body, including

the substituted arguments, is scanned again for macros to be expanded. 

The result is that the arguments are scanned <em>twice</em> to expand

macro calls in them.



   <p>Most of the time, this has no effect.  If the argument contained any

macro calls, they are expanded during the first scan.  The result

therefore contains no macro calls, so the second scan does not change

it.  If the argument were substituted as given, with no prescan, the

single remaining scan would find the same macro calls and produce the

same results.



   <p>You might expect the double scan to change the results when a

self-referential macro is used in an argument of another macro

(see <a href="Self-Referential-Macros.html#Self-Referential%20Macros">Self-Referential Macros</a>): the self-referential macro would be

expanded once in the first scan, and a second time in the second scan. 

However, this is not what happens.  The self-references that do not

expand in the first scan are marked so that they will not expand in the

second scan either.



   <p>You might wonder, "Why mention the prescan, if it makes no difference? 

And why not skip it and make the preprocessor faster?"  The answer is

that the prescan does make a difference in three special cases:



     <ul>

<li>Nested calls to a macro.



     <p>We say that <dfn>nested</dfn> calls to a macro occur when a macro's argument

contains a call to that very macro.  For example, if <code>f</code> is a macro

that expects one argument, <code>f (f (1))</code> is a nested pair of calls to

<code>f</code>.  The desired expansion is made by expanding <code>f (1)</code> and

substituting that into the definition of <code>f</code>.  The prescan causes

the expected result to happen.  Without the prescan, <code>f (1)</code> itself

would be substituted as an argument, and the inner use of <code>f</code> would

appear during the main scan as an indirect self-reference and would not

be expanded.



     </p><li>Macros that call other macros that stringify or concatenate.



     <p>If an argument is stringified or concatenated, the prescan does not

occur.  If you <em>want</em> to expand a macro, then stringify or

concatenate its expansion, you can do that by causing one macro to call

another macro that does the stringification or concatenation.  For

instance, if you have



     <pre class="example">          #define AFTERX(x) X_ ## x

          #define XAFTERX(x) AFTERX(x)

          #define TABLESIZE 1024

          #define BUFSIZE TABLESIZE

          </pre>



     <p>then <code>AFTERX(BUFSIZE)</code> expands to <code>X_BUFSIZE</code>, and

<code>XAFTERX(BUFSIZE)</code> expands to <code>X_1024</code>.  (Not to

<code>X_TABLESIZE</code>.  Prescan always does a complete expansion.)



     </p><li>Macros used in arguments, whose expansions contain unshielded commas.



     <p>This can cause a macro expanded on the second scan to be called with the

wrong number of arguments.  Here is an example:



     <pre class="example">          #define foo  a,b

          #define bar(x) lose(x)

          #define lose(x) (1 + (x))

          </pre>



     <p>We would like <code>bar(foo)</code> to turn into <code>(1 + (foo))</code>, which

would then turn into <code>(1 + (a,b))</code>.  Instead, <code>bar(foo)</code>

expands into <code>lose(a,b)</code>, and you get an error because <code>lose</code>

requires a single argument.  In this case, the problem is easily solved

by the same parentheses that ought to be used to prevent misnesting of

arithmetic operations:



     <pre class="example">          #define foo (a,b)

     <br>or<br>

          #define bar(x) lose((x))

          </pre>



     <p>The extra pair of parentheses prevents the comma in <code>foo</code>'s

definition from being interpreted as an argument separator.



   </ul>



   </body></html>



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -