2015-12-19 4 views
6

私はgccの前処理に関するドキュメントを読んでいる、私は次の文(here)読み:終了(GNUのドキュメント)

> echo -n "int main(void) {return 0;}" > test.c 
> gcc -Wall -Wextra -Werror test.c 
を:私は実行して警告を生成しようと

If the last line of any input file lacks an end-of-line marker, the end of the file is considered to implicitly supply one. The C standard says that this condition provokes undefined behavior, so GCC will emit a warning message.

しかしprobはない、それはコンパイルする。私は改行文字として改行マーカーを理解していますが、それは他のものと思われます。

警告はどのように生成できますか?

+3

'gcc -W'。なぜあなたは警告なしでGCCを実行しますか? –

+0

おそらく誰もこの状況を気にしません。 – bolov

+0

@KerrekSBこれは本当ですが、彼の例で '-W'や' -Wall'を使っても、彼が記述している文書化された警告は得られません。最低でも 'gcc'バージョン4.7.2ではありません。 – lurker

答えて

5

gccから削除されたようです。

これを参照してください: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=14331#c19

2007-05-31

PR preprocessor/14331 
    * lex.c (_cpp_get_fresh_line): Don't warn if no newline at EOF. 
+2

元々、警告はオプションになる予定でした。最終パッチはそれを無条件に削除します。動作は定義されていないので、警告を有効にするオプションを選択することをお勧めします。 –

+0

バックスラッシュで終わるファイルの場合、 'min.c:1:29:warning:バックスラッシュ - 改行(ファイルの最後に')の警告( '-Werror'でエラーに変換可能)改行。改行を伴わないバックスラッシュがある場合、 'min.c:1:1:error:stray '\' in program'(無条件にエラー)を取得します。最後にバックスラッシュ - 改行改行がある場合は、問題はありません(エラーがないか、それを与える原因になります)。 –

+0

Cの標準は5.1.1.2翻訳の段階で言及しています。¶2 - 改行文字の直後にあるバックスラッシュ文字( '\\')の各インスタンスは削除され、物理ソース行をスプライシングして論理ソース行を形成します。物理的なソース行の最後のバックスラッシュのみが、そのようなスプライスの一部になることができます。空でないソースファイルは、改行文字で終わるものとします。このようなスプライスが発生する前に、直前にバックスラッシュ文字を付加しないでください。_ –

1

C11(N1570)、§5.1.1.2、状態:

A source file that is not empty shall end in a new-line character, which shall not be immediately preceded by a backslash character before any such splicing takes place.

ので、ファイルので改行を持っていないが、上記の違反"shall"制約。

clangはそれについて警告を行います。

$ printf "int main(void) {return 0;}" > test.c 
$ clang -Weverything test.c 
test.c:1:27: warning: no newline at end of file [-Wnewline-eof] 
int main(void) {return 0;} 
         ^
1 warning generated. 

しかし、制約違反がundefined behaviourです。

C11(N1570)、§4、状態:GCCはいずれかを発行することを必要とされていないよう

If a ‘‘shall’’ or ‘‘shall not’’ requirement that appears outside of a constraint or runtime constraint is violated, the behavior is undefined. Undefined behavior is otherwise indicated in this International Standard by the words ‘‘undefined behavior’’ or by the omission of any explicit definition of behavior. There is no difference in emphasis among these three; they all describe ‘‘behavior that is undefined’’.

だからGCCは(@nnnで述べたように)警告を取るために自由を取りました未定義の動作の診断。

関連する問題