LoginSignup
0
0

More than 1 year has passed since last update.

6.8 Statements and blocks, CN3054:2022 (10) p152.c

Posted at

はじめに(Introduction)

N3054 Working Draft, Standard for Programming Language C

C++ nは、ISO/IEC JTC1 SC22 WG14の作業原案(Working Draft)です。
公式のISO/IEC 9899原本ではありません。

ISO/IEC JTC1 SC22 のWG14を含むいくつかのWGでは、可能な限り作業文書を公開し、幅広い意見を求めています。

ISO/IEC JTC1 SC7からISO/IEC JTC1 SC22リエゾンとして、2000年頃、C/C++の品質向上に貢献しようとした活動をしていたことがあります。その頃は、まだISO/IEC TS 17961の原案が出る前です。Cの精神が優勢で、セキュリティ対策は補助的な位置付けでした。ISO/IEC TS 17961の制定と、C/C++のライブラリ類の見直しと、C++の進化はどんどん進んでいきます。 

進化の具合が、どちらに行こうとしているかは、コンパイルて実行させてみないとわかりません。C/C++の規格案の電子ファイルは、そのままコンパイルできる形式であるとよいと主張してきました。MISRA-C/C++, CERTC/C++でも同様です。MISRA-C/C++は、Example Suiteという形で、コード断片をコンパイルできる形で提供するようになりました。

一連の記事はコード断片をコンパイルできる形にする方法を検討してコンパイル、リンク、実行して、規格案の原文と処理系(gcc, clang)との違いを確認し、技術内容を検討し、ISO/IEC JTC1 SC22 WG21にフィードバックするために用います。
また、CERT C/C++, MISRA C/C++等のコーディング標準のコード断片をコンパイルする際の参考にさせていただこうと考えています。CERT C++, MISRA C++が標準化の動きとの時間的なずれがあれば確認できれば幸いです。また、boostライブラリとの関連、Linux OS, 箱庭プロジェクト、g++(GCC), clang++(LLVM)との関係も調査中です。
何か、抜け漏れ、耳より情報がありましたらおしらせくださると幸いです。

<この項は書きかけです。順次追記します。>

背景(back ground)

C/C++でコンパイルエラーが出ると、途方にくれることがしばしばあります。
何回かに1回は、該当するエラーが検索できます。
ただ、条件が違っていて、そこでの修正方法では目的を達成しないこともしばしばです。いろいろな条件のコンパイルエラーとその対応方法について、広く記録することによって、いつか同じエラーに遭遇した時にやくに立つことを目指しています。

過去に何度か、自分のネットでの記録に助けられたことがあります。

また
https://researchmap.jp/joub9b3my-1797580/#_1797580
に記載したサイトのお世話になっています。

作業方針(sequence)

clangでは--std=c11, -std=C17 -std=c2xの3種類
gccでは-std=c11, -std=C17 -std=c2xの3種類
でコンパイルし、

1)コンパイルエラーを収集する。
2)コンパイルエラーをなくす方法を検討する。
コンパイルエラーになる例を示すだけが目的のコードは、コンパイルエラーをなくすのではなく、コンパイルエラーの種類を収集するだけにする。
文法を示すのが目的のコード場合に、コンパイルエラーをなくすのに手間がかかる場合は、順次作業します。
3)リンクエラーをなくす方法を検討する。
文法を示すのが目的のコード場合に、リンクエラーをなくすのに手間がかかる場合は、順次作業します。
4)意味のある出力を作る。
コンパイル、リンクが通っても、意味のある出力を示そうとすると、コンパイル・リンクエラーが出て収拾できそうにない場合がある。順次作業します。

1)だけのものから4)まで進んだものと色々ある状態です。一歩でも前に進むご助言をお待ちしています。「検討事項」の欄に現状を記録するようにしています。

bash
$ docker run -v /Users/ogawakiyoshi/n4910/n3540:/Users/ogawakiyoshi/n4910/n3540 -it kaizenjapan/n3540 /bin/bash

読書感想文

CコンパイラによるC言語規格の読書感想文として掲載しています。

コンパイル実験が、CN3242に対する、gccとclangによる感想文だということご理解いただけると幸いです。

読書感想文は人間かAIだけが作るものとは限りません。
本(電子書籍を含む)を入力として、その内容に対する文字列を読書感想文として受け止めましょう。
元の文章をあり方、コンパイルできるように電子化しておくこと、コンパイラが解釈可能な断片の作り方など。

個人開発

Cコンパイラの試験を一人でもくもくとやっているのは個人開発の一つの姿です。

<この項は書きかけです。順次追記します。>

編纂器(Compiler)

clang --version

Debian clang version 14.0.6-++20220622053050+f28c006a5895-1~exp1~20220622173135.152
Target: x86_64-pc-linux-gnu Thread model: posix InstalledDir: /usr/bin

gcc --version

gcc (GCC) 12.1.0 Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

算譜(source code)

p.c
// CN3054 Committee Draft, Standard for Programming Language C
// http://www.open-std.org/jtc1/sc22/wg14/docs/papers/2022/n3054.pdf
const char * n3054 = "";
// Debian clang version 14.0.5-++20220610033153+c12386ae247c-
// g++ (GCC) 12.1.0 Copyright (C) 2022 Free Software Foundation, Inc.
// Edited by Dr. OGAWA Kiyoshi. Compile procedure and results record.
// C++N3054:2022 Standard Working Draft on ISO/IEC 14882(0) sample code compile list

#include "N3054.h"

//

int main() {
//    PR3(st.a,st.b,st.c,d);
    printf("%s\n", n3054);
    return EXIT_SUCCESS;
}

編纂・実行結果(compile and go)

bash

検討事項(agenda)

コンパイルエラーを取るか、コンパイルエラーの理由を解説する。

応用例1 MISRA C/C++

MISRA C まとめ #include

MISRA C++ 5-0-16

応用例2 CERT C/C++

SEI CERT C++ Coding Standard AA. Bibliography 確認中。

MISRA C/C++, AUTOSAR C++, CERT C/C++とC/C++工業標準をコンパイルする

自己参考資料(self reference)

関連する自己参照以外は、こちらの先頭に移転。

C言語(C++)に対する誤解、曲解、無理解、爽快。

C2011コンパイル一覧@researchmap

https://researchmap.jp/jownvh0ye-1797580/#_1797580

[C][C++]の国際規格案の例題をコンパイルするときの課題7つ。

C Puzzle Bookの有り難み5つ、C言語規格及びCコンパイラの特性を認識

dockerにclang

docker gnu(gcc/g++) and llvm(clang/clang++)

コンパイル用shell script C版(clangとgcc)とC++版(clang++とg++)

astyle 使ってみた

<この記事は個人の過去の経験に基づく個人の感想です。現在所属する組織、業務とは関係がありません。>

文書履歴(document history)

ver. 0.01 初稿  20221120

CN3054 AdventCalendar2022 読書感想文 個人開発 小川メソッド # はじめに(Introduction)

N3054 Working Draft, Standard for Programming Language C

C++ nは、ISO/IEC JTC1 SC22 WG14の作業原案(Working Draft)です。
公式のISO/IEC 9899原本ではありません。

ISO/IEC JTC1 SC22 のWG14を含むいくつかのWGでは、可能な限り作業文書を公開し、幅広い意見を求めています。

ISO/IEC JTC1 SC7からISO/IEC JTC1 SC22リエゾンとして、2000年頃、C/C++の品質向上に貢献しようとした活動をしていたことがあります。その頃は、まだISO/IEC TS 17961の原案が出る前です。Cの精神が優勢で、セキュリティ対策は補助的な位置付けでした。ISO/IEC TS 17961の制定と、C/C++のライブラリ類の見直しと、C++の進化はどんどん進んでいきます。 

進化の具合が、どちらに行こうとしているかは、コンパイルて実行させてみないとわかりません。C/C++の規格案の電子ファイルは、そのままコンパイルできる形式であるとよいと主張してきました。MISRA-C/C++, CERTC/C++でも同様です。MISRA-C/C++は、Example Suiteという形で、コード断片をコンパイルできる形で提供するようになりました。

一連の記事はコード断片をコンパイルできる形にする方法を検討してコンパイル、リンク、実行して、規格案の原文と処理系(gcc, clang)との違いを確認し、技術内容を検討し、ISO/IEC JTC1 SC22 WG21にフィードバックするために用います。
また、CERT C/C++, MISRA C/C++等のコーディング標準のコード断片をコンパイルする際の参考にさせていただこうと考えています。CERT C++, MISRA C++が標準化の動きとの時間的なずれがあれば確認できれば幸いです。また、boostライブラリとの関連、Linux OS, 箱庭プロジェクト、g++(GCC), clang++(LLVM)との関係も調査中です。
何か、抜け漏れ、耳より情報がありましたらおしらせくださると幸いです。

<この項は書きかけです。順次追記します。>

背景(back ground)

C/C++でコンパイルエラーが出ると、途方にくれることがしばしばあります。
何回かに1回は、該当するエラーが検索できます。
ただ、条件が違っていて、そこでの修正方法では目的を達成しないこともしばしばです。いろいろな条件のコンパイルエラーとその対応方法について、広く記録することによって、いつか同じエラーに遭遇した時にやくに立つことを目指しています。

過去に何度か、自分のネットでの記録に助けられたことがあります。

また
https://researchmap.jp/joub9b3my-1797580/#_1797580
に記載したサイトのお世話になっています。

作業方針(sequence)

clangでは--std=c11, -std=C17 -std=c2xの3種類
gccでは-std=c11, -std=C17 -std=c2xの3種類
でコンパイルし、

1)コンパイルエラーを収集する。
2)コンパイルエラーをなくす方法を検討する。
コンパイルエラーになる例を示すだけが目的のコードは、コンパイルエラーをなくすのではなく、コンパイルエラーの種類を収集するだけにする。
文法を示すのが目的のコード場合に、コンパイルエラーをなくすのに手間がかかる場合は、順次作業します。
3)リンクエラーをなくす方法を検討する。
文法を示すのが目的のコード場合に、リンクエラーをなくすのに手間がかかる場合は、順次作業します。
4)意味のある出力を作る。
コンパイル、リンクが通っても、意味のある出力を示そうとすると、コンパイル・リンクエラーが出て収拾できそうにない場合がある。順次作業します。

1)だけのものから4)まで進んだものと色々ある状態です。一歩でも前に進むご助言をお待ちしています。「検討事項」の欄に現状を記録するようにしています。

bash
$ docker run -v /Users/ogawakiyoshi/n4910/n3540:/Users/ogawakiyoshi/n4910/n3540 -it kaizenjapan/n3540 /bin/bash

読書感想文

CコンパイラによるC言語規格の読書感想文として掲載しています。

コンパイル実験が、CN3242に対する、gccとclangによる感想文だということご理解いただけると幸いです。

読書感想文は人間かAIだけが作るものとは限りません。
本(電子書籍を含む)を入力として、その内容に対する文字列を読書感想文として受け止めましょう。
元の文章をあり方、コンパイルできるように電子化しておくこと、コンパイラが解釈可能な断片の作り方など。

個人開発

Cコンパイラの試験を一人でもくもくとやっているのは個人開発の一つの姿です。

<この項は書きかけです。順次追記します。>

編纂器(Compiler)

clang --version

Debian clang version 14.0.6-++20220622053050+f28c006a5895-1~exp1~20220622173135.152
Target: x86_64-pc-linux-gnu Thread model: posix InstalledDir: /usr/bin

gcc --version

gcc (GCC) 12.1.0 Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

6.8 Statements and blocks, CN3054:2022 (10) p152.c

算譜(source code)

p152.c
// CN3054 Committee Draft, Standard for Programming Language C
// http://www.open-std.org/jtc1/sc22/wg14/docs/papers/2022/n3054.pdf
const char * n3054 = "6.8 Statements and blocks, CN3054:2022 (10) p152.c";
// Debian clang version 14.0.5-++20220610033153+c12386ae247c-
// g++ (GCC) 12.1.0 Copyright (C) 2022 Free Software Foundation, Inc.
// Edited by Dr. OGAWA Kiyoshi. Compile procedure and results record.
// C++N3054:2022 Standard Working Draft on ISO/IEC 14882(0) sample code compile list

#include "N3054.h"

// 6.8.3 Expression and null statements
// Syntax
//  expression-statement: expressionopt ;
// attribute-specifier-sequence expression ;
//  The attribute specifier sequence appertains to the expression. The expression in an expression statement is evaluated as a void expression for its side effects.195)
//  A null statement (consisting of just a semicolon) performs no operations.
//  EXAMPLE 1 If a function call is evaluated as an expression statement for its side effects only, the discarding of its value can be made explicit by converting the expression to a void expression by means of a cast:
// 195)Such as assignments, and function calls which have side effects.
// Semantics
int p(int); /* ... */
int expr =0;
int n=0;
int j=1;
void f(int) {}; // added by Dr. O.K. 20221120
void f2(void);// added by Dr. O.K. 20221120
void f2() { // added by Dr. O.K. 20221120
    (void)p(0);
//  EXAMPLE 2 In the program fragment a null statement is used to supply an empty loop body to the iteration statement. Forward references: iteration statements (6.8.5).
// 6.8.4 Selection statements
// Syntax
//  selection-statement:
// if ( expression ) secondary-block
// if ( expression ) secondary-block else secondary-block switch ( expression ) secondary-block
// Semantics
//  A selection statement selects among a set of secondary blocks depending on the value of a controlling expression.
// 6.8.4.1 The if statement
// Constraints
//  The controlling expression of an if statement shall have scalar type. Semantics
//  In both forms, the first substatement is executed if the expression compares unequal to 0. In the else form, the second substatement is executed if the expression compares equal to 0. If the first substatement is reached via a label, the second substatement is not executed.
//  An else is associated with the lexically nearest preceding if that is allowed by the syntax.
// 6.8.4.2 The switch statement
// Constraints
//  The controlling expression of a switch statement shall have integer type.
//  If a switch statement has an associated case or default label within the scope of an identifier with a variably modified type, the entire switch statement shall be within the scope of that identifier.196)
//  The expression of each case label shall be an integer constant expression and no two of the case constant expressions associated to the same switch statement shall have the same value after conversion. There may be at most one default label associated to a switch statement. (Any enclosed switch statement may have a default label or case constant expressions with values that duplicate case constant expressions in the enclosing switch statement.)
// Semantics
//  A switch statement causes control to jump to, into, or past the statement that is the switch body, depending on the value of a controlling expression, and on the presence of a default label and the values of any case labels on or in the switch body. A case or default label is accessible only within the closest enclosing switch statement.
//  The integer promotions are performed on the controlling expression. The constant expression in each case label is converted to the promoted type of the controlling expression. If a converted value matches that of the promoted controlling expression, control jumps to the statement or declaration
// 196)That is, the declaration either precedes the switch statement, or it follows the last case or default label associated with the switch that is in the block containing the declaration.
    char *s;
    /* ... */
    while (*s++ != '\0')
        ;
// following the matched case label. Otherwise, if there is a default label, control jumps to the statement or declaration following the default label. If no converted case constant expression matches and there is no default label, no part of the switch body is executed. Implementation limits
//  As discussed in 5.2.4.1, the implementation may limit the number of case values in a switch statement.
//  EXAMPLE In the artificial program fragment
    switch (expr) {
        int i = 4;
        f(i);
    case 0:
        i = 17;
    /* falls through into default code */
    default:
        printf("%d\n", i);
    }
//     the object whose identifier is i exists with automatic storage duration (within the block) but is never initialized, and thus if the controlling expression has a nonzero value, the call to the printf function will access an object with an indeterminate representation. Similarly, the call to the function f cannot be reached.
// 6.8.5 Iteration statements
// Syntax
//  iteration-statement:
// while ( expression ) secondary-block
// do secondary-block while ( expression ) ;
// for ( expressionopt ; expressionopt ; expressionopt ) secondary-block
// for ( declaration expressionopt ; expressionopt ) secondary-block
// Constraints
//  The controlling expression of an iteration statement shall have scalar type.
//  The declaration part of a for statement shall only declare identifiers for objects having storage class auto or register. Semantics
//  An iteration statement causes a secondary block called the loop body to be executed repeatedly until the controlling expression compares equal to 0. The repetition occurs regardless of whether the loop body is entered from the iteration statement or by a jump197).
//  An iteration statement may be assumed by the implementation to terminate if its controlling expression is not a constant expression198), and none of the following operations are performed in its body, controlling expression or (in the case of a for statement) its expression-3199):
// — input/output operations
// — accessing a volatile object
// — synchronization or atomic operations.
// 197)Code jumped over is not executed. In particular, the controlling expression of a for or while statement is not evaluated before entering the loop body, nor is clause-1 (6.8.5.3) of a for statement.
// 198)An omitted controlling expression is replaced by a nonzero constant, which is a constant expression.
// 199)This is intended to allow compiler transformations such as removal of empty loops even when termination cannot be proven.
//  The evaluation of the controlling expression takes place before each execution of the loop body.
// 6.8.5.2 The do statement
//  The evaluation of the controlling expression takes place after each execution of the loop body.
// 6.8.5.3 The for statement
//  The statement for (clause-1; expression-2; expression-3) statement behaves as follows: The expression expression-2 is the controlling expression that is evaluated before each execution of the loop body. The expression expression-3 is evaluated as a void expression after each execution of the loop body. If clause-1 is a declaration, the scope of any identifiers it declares is the remainder of the declaration and the entire loop, including the other two expressions; it is reached in the order of execution before the first evaluation of the controlling expression. If clause-1 is an expression, it is evaluated as a void expression before the first evaluation of the controlling expression.200)
//  Both clause-1 and expression-3 can be omitted. An omitted expression-2 is replaced by a nonzero constant.
// 6.8.6 Jump statements
//             Syntax
//  jump-statement:
// Semantics
// goto identifier ; continue ; break ; return expressionopt ;
//  A jump statement causes an unconditional jump to another place.
// 6.8.6.1 The goto statement
// Constraints
// The identifier in a goto statement shall name a label located somewhere in the enclosing function. A goto statement shall not jump from outside the scope of an identifier having a variably modified type to inside the scope of that identifier.
// Semantics
//  A goto statement causes an unconditional jump to the statement prefixed by the named label in the enclosing function.
//  EXAMPLE 1 It is sometimes convenient to jump into the middle of a complicated set of statements. The following outline presents one possible approach to a problem based on these three assumptions:
// 1. The general initialization code accesses objects only visible to the current function.
// 2. The general initialization code is too large to warrant duplication.
// 3. The code to determine the next operation is at the head of the loop. (To allow it to be reached by continue statements, for example.)
    /* ... */
// 200)Thus, clause-1 specifies initialization for the loop, possibly declaring one or more variables for use in the loop; the controlling expression, expression-2, specifies an evaluation made before each iteration, such that execution of the loop continues until the expression compares equal to 0; and expression-3 specifies an operation (such as incrementing) that is performed after each iteration.
    int need_to_reinitialize = 1;
    goto first_time;
    for (;;) {
// determine next operation /* ... */
        if (need_to_reinitialize) {
// reinitialize-only code
            /* ... */
first_time:
// general initialization code /* ... */
            continue;
        }
// handle other operations
        /* ... */
    }
// EXAMPLE 2 A goto statement is not allowed to jump past any declarations of objects with variably modified types. A jump within the scope, however, is permitted.
    goto lab3;
    {
        double a[n];
        a[j] = 4.4;
lab3:
        a[j] = 3.3;
        goto lab4;
        a[j] = 5.5;
lab4:
        a[j] = 6.6;
    }
    goto lab4;
// invalid: going INTO scope of VLA.
// valid: going WITHIN scope of VLA.
// invalid: going INTO scope of VLA.
// 6.8.6.2 The continue statement
// Constraints
//  A continue statement shall appear only in or as a loop body. Semantics
//  A continue statement causes a jump to the loop-continuation portion of the smallest enclosing iteration statement; that is, to the end of the loop body. More precisely, in each of the statements unless the continue statement shown is in an enclosed iteration statement (in which case it is interpretedwithinthatstatement),itisequivalenttogoto contin;201).
// 6.8.6.3 The break statement
// Constraints
//  A break statement shall appear only in or as a switch body or loop body.
// 201)Following the contin: label in the 2nd example is a null statement. The null statement in the first and third example is implied by the label (6.8.2).
    while (/* ... */) { /* ... */
        continue;
/* ... */ contin:
    }
    do {
        /* ... */
        continue;
        /* ... */
contin:
        ;
    } while (/* ... */);
    for (/* ... */) { /* ... */
        continue;
        /* ... */
contin:
    }
} //added by Dr. O.K 20221120
//  A break statement terminates execution of the smallest enclosing switch or iteration statement.
// 6.8.6.4 The return statement
// Constraints
//  A return statement with an expression shall not appear in a function whose return type is void. A return statement without an expression shall only appear in a function whose return type is void.
// Semantics
//  A return statement terminates execution of the current function and returns control to its caller. A function may have any number of return statements.
//  If a return statement with an expression is executed, the value of the expression is returned to the caller as the value of the function call expression. If the expression has a type different from the return type of the function in which it appears, the value is converted as if by assignment to an object having the return type of the function.202)
//  EXAMPLE In:
struct s {
    double i;
} f5(void);// change function name by Dr. O.K. 20221120
union {
    struct {
        int f1;
        struct s f2;
    } u1;
    struct {
        struct s f3;
        int f4;
    } u2;
} g;
struct s f5(void)//change
{ }
return g.u1.f2;
/* ... */
g.u2.f3 = f5();// change
// there is no undefined behavior, although there would be if the assignment were done directly (without using a function call to fetch the value).
// 202)The return statement is not an assignment. The overlap restriction of 6.5.16.1 does not apply to the case of function return. The representation of floating-point values can have wider range or precision than implied by the type; a cast can be used to remove this extra range and precision.
int main() {
//    PR3(st.a,st.b,st.c,d);
    printf("%s\n", n3054);
    return EXIT_SUCCESS;
}

編纂・実行結果(compile and go)

bash
$ clang p152.c -std=11 -o p152l -I. -Wall
p152.c:24:11: warning: omitting the parameter name in a function definition is a C2x extension [-Wc2x-extensions]
void f(int){};// added by Dr. O.K. 20221120
          ^
p152.c:145:21: error: expected expression
    while (/* ... */) { /* ... */
                    ^
p152.c:148:1: error: expected statement
}
^
p152.c:153:1: error: redefinition of label 'contin'
contin:;
^
p152.c:147:11: note: previous definition is here
/* ... */ contin:
          ^
p152.c:154:19: error: expected expression
} while (/* ... */);
                  ^
p152.c:155:21: error: expected expression
      for (/* ... */) { /* ... */
                    ^
p152.c:155:21: error: expected ';' in 'for' statement specifier
p152.c:159:1: error: expected statement
}             
^
p152.c:158:1: error: redefinition of label 'contin'
contin:
^
p152.c:147:11: note: previous definition is here
/* ... */ contin:
          ^
p152.c:133:1: error: cannot jump from this goto statement to its label
goto lab4;
^
p152.c:124:8: note: jump bypasses initialization of variable length array
double a[n];
       ^
p152.c:122:10: error: cannot jump from this goto statement to its label
         goto lab3; 
         ^
p152.c:124:8: note: jump bypasses initialization of variable length array
double a[n];
       ^
p152.c:180:1: error: expected identifier or '('
return g.u1.f2;
^
p152.c:182:1: error: unknown type name 'g'
g.u2.f3 = f5();// change
^
p152.c:182:2: error: expected identifier or '('
g.u2.f3 = f5();// change
 ^
1 warning and 13 errors generated.

$ clang p152.c -std=17 -o p152l -I. -Wall
p152.c:24:11: warning: omitting the parameter name in a function definition is a C2x extension [-Wc2x-extensions]
void f(int){};// added by Dr. O.K. 20221120
          ^
p152.c:145:21: error: expected expression
    while (/* ... */) { /* ... */
                    ^
p152.c:148:1: error: expected statement
}
^
p152.c:153:1: error: redefinition of label 'contin'
contin:;
^
p152.c:147:11: note: previous definition is here
/* ... */ contin:
          ^
p152.c:154:19: error: expected expression
} while (/* ... */);
                  ^
p152.c:155:21: error: expected expression
      for (/* ... */) { /* ... */
                    ^
p152.c:155:21: error: expected ';' in 'for' statement specifier
p152.c:159:1: error: expected statement
}             
^
p152.c:158:1: error: redefinition of label 'contin'
contin:
^
p152.c:147:11: note: previous definition is here
/* ... */ contin:
          ^
p152.c:133:1: error: cannot jump from this goto statement to its label
goto lab4;
^
p152.c:124:8: note: jump bypasses initialization of variable length array
double a[n];
       ^
p152.c:122:10: error: cannot jump from this goto statement to its label
         goto lab3; 
         ^
p152.c:124:8: note: jump bypasses initialization of variable length array
double a[n];
       ^
p152.c:180:1: error: expected identifier or '('
return g.u1.f2;
^
p152.c:182:1: error: unknown type name 'g'
g.u2.f3 = f5();// change
^
p152.c:182:2: error: expected identifier or '('
g.u2.f3 = f5();// change
 ^
1 warning and 13 errors generated.

$ clang p152.c -std=2x -o p152l -I. -Wall
p152.c:145:21: error: expected expression
    while (/* ... */) { /* ... */
                    ^
p152.c:148:1: error: expected statement
}
^
p152.c:153:1: error: redefinition of label 'contin'
contin:;
^
p152.c:147:11: note: previous definition is here
/* ... */ contin:
          ^
p152.c:154:19: error: expected expression
} while (/* ... */);
                  ^
p152.c:155:21: error: expected expression
      for (/* ... */) { /* ... */
                    ^
p152.c:155:21: error: expected ';' in 'for' statement specifier
p152.c:159:1: error: expected statement
}             
^
p152.c:158:1: error: redefinition of label 'contin'
contin:
^
p152.c:147:11: note: previous definition is here
/* ... */ contin:
          ^
p152.c:133:1: error: cannot jump from this goto statement to its label
goto lab4;
^
p152.c:124:8: note: jump bypasses initialization of variable length array
double a[n];
       ^
p152.c:122:10: error: cannot jump from this goto statement to its label
         goto lab3; 
         ^
p152.c:124:8: note: jump bypasses initialization of variable length array
double a[n];
       ^
p152.c:180:1: error: expected identifier or '('
return g.u1.f2;
^
p152.c:182:1: error: unknown type name 'g'
g.u2.f3 = f5();// change
^
p152.c:182:2: error: expected identifier or '('
g.u2.f3 = f5();// change
 ^
13 errors generated.

$ gcc p152.c -std=11 -o p152g -I. -Wall
p152.c: In function 'f2':
p152.c:122:10: error: jump into scope of identifier with variably modified type
  122 |          goto lab3;
      |          ^~~~
p152.c:126:1: note: label 'lab3' defined here
  126 | lab3:
      | ^~~~
p152.c:124:8: note: 'a' declared here
  124 | double a[n];
      |        ^
p152.c:124:8: warning: variable 'a' set but not used [-Wunused-but-set-variable]
p152.c:133:1: error: jump into scope of identifier with variably modified type
  133 | goto lab4;
      | ^~~~
p152.c:130:1: note: label 'lab4' defined here
  130 | lab4:
      | ^~~~
p152.c:124:8: note: 'a' declared here
  124 | double a[n];
      |        ^
p152.c:145:21: error: expected expression before ')' token
  145 |     while (/* ... */) { /* ... */
      |                     ^
p152.c:153:1: error: duplicate label 'contin'
  153 | contin:;
      | ^~~~~~
p152.c:147:11: note: previous definition of 'contin' with type 'void'
  147 | /* ... */ contin:
      |           ^~~~~~
p152.c:154:19: error: expected expression before ')' token
  154 | } while (/* ... */);
      |                   ^
p152.c:155:21: error: expected expression before ')' token
  155 |       for (/* ... */) { /* ... */
      |                     ^
p152.c:155:21: error: expected expression before ')' token
p152.c:158:1: error: duplicate label 'contin'
  158 | contin:
      | ^~~~~~
p152.c:147:11: note: previous definition of 'contin' with type 'void'
  147 | /* ... */ contin:
      |           ^~~~~~
p152.c:147:11: warning: label 'contin' defined but not used [-Wunused-label]
p152.c: At top level:
p152.c:180:1: error: expected identifier or '(' before 'return'
  180 | return g.u1.f2;
      | ^~~~~~
p152.c:182:2: error: expected '=', ',', ';', 'asm' or '__attribute__' before '.' token
  182 | g.u2.f3 = f5();// change
      |  ^
p152.c: In function 'f2':
p152.c:58:5: warning: statement will never be executed [-Wswitch-unreachable]
   58 | int i = 4;
      |     ^
p152.c: In function 'f5':
p152.c:179:10: warning: control reaches end of non-void function [-Wreturn-type]
  179 |        { }
      |          ^

$ gcc p152.c -std=c17 -o p152g -I. -Wall
p152.c: In function 'f2':
p152.c:122:10: error: jump into scope of identifier with variably modified type
  122 |          goto lab3;
      |          ^~~~
p152.c:126:1: note: label 'lab3' defined here
  126 | lab3:
      | ^~~~
p152.c:124:8: note: 'a' declared here
  124 | double a[n];
      |        ^
p152.c:124:8: warning: variable 'a' set but not used [-Wunused-but-set-variable]
p152.c:133:1: error: jump into scope of identifier with variably modified type
  133 | goto lab4;
      | ^~~~
p152.c:130:1: note: label 'lab4' defined here
  130 | lab4:
      | ^~~~
p152.c:124:8: note: 'a' declared here
  124 | double a[n];
      |        ^
p152.c:145:21: error: expected expression before ')' token
  145 |     while (/* ... */) { /* ... */
      |                     ^
p152.c:153:1: error: duplicate label 'contin'
  153 | contin:;
      | ^~~~~~
p152.c:147:11: note: previous definition of 'contin' with type 'void'
  147 | /* ... */ contin:
      |           ^~~~~~
p152.c:154:19: error: expected expression before ')' token
  154 | } while (/* ... */);
      |                   ^
p152.c:155:21: error: expected expression before ')' token
  155 |       for (/* ... */) { /* ... */
      |                     ^
p152.c:155:21: error: expected expression before ')' token
p152.c:158:1: error: duplicate label 'contin'
  158 | contin:
      | ^~~~~~
p152.c:147:11: note: previous definition of 'contin' with type 'void'
  147 | /* ... */ contin:
      |           ^~~~~~
p152.c:147:11: warning: label 'contin' defined but not used [-Wunused-label]
p152.c: At top level:
p152.c:180:1: error: expected identifier or '(' before 'return'
  180 | return g.u1.f2;
      | ^~~~~~
p152.c:182:2: error: expected '=', ',', ';', 'asm' or '__attribute__' before '.' token
  182 | g.u2.f3 = f5();// change
      |  ^
p152.c: In function 'f2':
p152.c:58:5: warning: statement will never be executed [-Wswitch-unreachable]
   58 | int i = 4;
      |     ^
p152.c: In function 'f5':
p152.c:179:10: warning: control reaches end of non-void function [-Wreturn-type]
  179 |        { }
      |          ^

$ gcc p152.c -std=c2x -o p152g -I. -Wall
p152.c: In function 'f2':
p152.c:122:10: error: jump into scope of identifier with variably modified type
  122 |          goto lab3;
      |          ^~~~
p152.c:126:1: note: label 'lab3' defined here
  126 | lab3:
      | ^~~~
p152.c:124:8: note: 'a' declared here
  124 | double a[n];
      |        ^
p152.c:124:8: warning: variable 'a' set but not used [-Wunused-but-set-variable]
p152.c:133:1: error: jump into scope of identifier with variably modified type
  133 | goto lab4;
      | ^~~~
p152.c:130:1: note: label 'lab4' defined here
  130 | lab4:
      | ^~~~
p152.c:124:8: note: 'a' declared here
  124 | double a[n];
      |        ^
p152.c:145:21: error: expected expression before ')' token
  145 |     while (/* ... */) { /* ... */
      |                     ^
p152.c:153:1: error: duplicate label 'contin'
  153 | contin:;
      | ^~~~~~
p152.c:147:11: note: previous definition of 'contin' with type 'void'
  147 | /* ... */ contin:
      |           ^~~~~~
p152.c:154:19: error: expected expression before ')' token
  154 | } while (/* ... */);
      |                   ^
p152.c:155:21: error: expected expression before ')' token
  155 |       for (/* ... */) { /* ... */
      |                     ^
p152.c:155:21: error: expected expression before ')' token
p152.c:158:1: error: duplicate label 'contin'
  158 | contin:
      | ^~~~~~
p152.c:147:11: note: previous definition of 'contin' with type 'void'
  147 | /* ... */ contin:
      |           ^~~~~~
p152.c:147:11: warning: label 'contin' defined but not used [-Wunused-label]
p152.c: At top level:
p152.c:180:1: error: expected identifier or '(' before 'return'
  180 | return g.u1.f2;
      | ^~~~~~
p152.c:182:2: error: expected '=', ',', ';', 'asm' or '__attribute__' before '.' token
  182 | g.u2.f3 = f5();// change
      |  ^
p152.c: In function 'f2':
p152.c:58:5: warning: statement will never be executed [-Wswitch-unreachable]
   58 | int i = 4;
      |     ^
p152.c: In function 'f5':
p152.c:179:10: warning: control reaches end of non-void function [-Wreturn-type]
  179 |        { }
      |    

検討事項(agenda)

コンパイルエラーを取るか、コンパイルエラーの理由を解説する。

応用例1 MISRA C/C++

MISRA C まとめ #include

MISRA C++ 5-0-16

応用例2 CERT C/C++

SEI CERT C++ Coding Standard AA. Bibliography 確認中。

MISRA C/C++, AUTOSAR C++, CERT C/C++とC/C++工業標準をコンパイルする

自己参考資料(self reference)

関連する自己参照以外は、こちらの先頭に移転。

C言語(C++)に対する誤解、曲解、無理解、爽快。

C2011コンパイル一覧@researchmap

https://researchmap.jp/jownvh0ye-1797580/#_1797580

[C][C++]の国際規格案の例題をコンパイルするときの課題7つ。

C Puzzle Bookの有り難み5つ、C言語規格及びCコンパイラの特性を認識

dockerにclang

docker gnu(gcc/g++) and llvm(clang/clang++)

コンパイル用shell script C版(clangとgcc)とC++版(clang++とg++)

astyle 使ってみた

<この記事は個人の過去の経験に基づく個人の感想です。現在所属する組織、業務とは関係がありません。>

文書履歴(document history)

ver. 0.01 初稿  20221120

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0