diff options
| author | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
| commit | 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch) | |
| tree | f1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/gcc~13/local-labels.html | |
new repository
Diffstat (limited to 'devdocs/gcc~13/local-labels.html')
| -rw-r--r-- | devdocs/gcc~13/local-labels.html | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/devdocs/gcc~13/local-labels.html b/devdocs/gcc~13/local-labels.html new file mode 100644 index 00000000..229f6a1e --- /dev/null +++ b/devdocs/gcc~13/local-labels.html @@ -0,0 +1,37 @@ +<div class="section-level-extent" id="Local-Labels"> <div class="nav-panel"> <p> Next: <a href="labels-as-values" accesskey="n" rel="next">Labels as Values</a>, Previous: <a href="statement-exprs" accesskey="p" rel="prev">Statements and Declarations in Expressions</a>, Up: <a href="c-extensions" accesskey="u" rel="up">Extensions to the C Language Family</a> [<a href="index#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="indices" title="Index" rel="index">Index</a>]</p> </div> <h1 class="section" id="Locally-Declared-Labels"><span>6.2 Locally Declared Labels<a class="copiable-link" href="#Locally-Declared-Labels"> ¶</a></span></h1> <p>GCC allows you to declare <em class="dfn">local labels</em> in any nested block scope. A local label is just like an ordinary label, but you can only reference it (with a <code class="code">goto</code> statement, or by taking its address) within the block in which it is declared. </p> <p>A local label declaration looks like this: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">__label__ <var class="var">label</var>;</pre> +</div> <p>or </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">__label__ <var class="var">label1</var>, <var class="var">label2</var>, /* <span class="r">…</span> */;</pre> +</div> <p>Local label declarations must come at the beginning of the block, before any ordinary declarations or statements. </p> <p>The label declaration defines the label <em class="emph">name</em>, but does not define the label itself. You must do this in the usual way, with <code class="code"><var class="var">label</var>:</code>, within the statements of the statement expression. </p> <p>The local label feature is useful for complex macros. If a macro contains nested loops, a <code class="code">goto</code> can be useful for breaking out of them. However, an ordinary label whose scope is the whole function cannot be used: if the macro can be expanded several times in one function, the label is multiply defined in that function. A local label avoids this problem. For example: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">#define SEARCH(value, array, target) \ +do { \ + __label__ found; \ + typeof (target) _SEARCH_target = (target); \ + typeof (*(array)) *_SEARCH_array = (array); \ + int i, j; \ + int value; \ + for (i = 0; i < max; i++) \ + for (j = 0; j < max; j++) \ + if (_SEARCH_array[i][j] == _SEARCH_target) \ + { (value) = i; goto found; } \ + (value) = -1; \ + found:; \ +} while (0)</pre> +</div> <p>This could also be written using a statement expression: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">#define SEARCH(array, target) \ +({ \ + __label__ found; \ + typeof (target) _SEARCH_target = (target); \ + typeof (*(array)) *_SEARCH_array = (array); \ + int i, j; \ + int value; \ + for (i = 0; i < max; i++) \ + for (j = 0; j < max; j++) \ + if (_SEARCH_array[i][j] == _SEARCH_target) \ + { value = i; goto found; } \ + value = -1; \ + found: \ + value; \ +})</pre> +</div> <p>Local label declarations also make the labels they declare visible to nested functions, if there are any. See <a class="xref" href="nested-functions">Nested Functions</a>, for details. </p> </div> <div class="nav-panel"> <p> Next: <a href="labels-as-values">Labels as Values</a>, Previous: <a href="statement-exprs">Statements and Declarations in Expressions</a>, Up: <a href="c-extensions">Extensions to the C Language Family</a> [<a href="index#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="indices" title="Index" rel="index">Index</a>]</p> </div><div class="_attribution"> + <p class="_attribution-p"> + © Free Software Foundation<br>Licensed under the GNU Free Documentation License, Version 1.3.<br> + <a href="https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Local-Labels.html" class="_attribution-link">https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Local-Labels.html</a> + </p> +</div> |
