From: Richard Smith Date: Mon, 20 Oct 2014 23:26:58 +0000 (+0000) Subject: Switch C compilations to C11 by default. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b241411e502002395e4dcb2083bb3328dff66bd4;p=clang Switch C compilations to C11 by default. This is long-since overdue, and matches GCC 5.0. This should also be backwards-compatible, because we already supported all of C11 as an extension in C99 mode. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@220244 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/docs/UsersManual.rst b/docs/UsersManual.rst index a5d8148810..9d8e978ed3 100644 --- a/docs/UsersManual.rst +++ b/docs/UsersManual.rst @@ -73,7 +73,7 @@ Basic Usage Intro to how to use a C compiler for newbies. compile + link compile then link debug info enabling optimizations -picking a language to use, defaults to C99 by default. Autosenses based +picking a language to use, defaults to C11 by default. Autosenses based on extension. using a makefile Command Line Options @@ -1474,9 +1474,12 @@ Differences between various standard modes ------------------------------------------ clang supports the -std option, which changes what language mode clang -uses. The supported modes for C are c89, gnu89, c94, c99, gnu99 and -various aliases for those modes. If no -std option is specified, clang -defaults to gnu99 mode. +uses. The supported modes for C are c89, gnu89, c94, c99, gnu99, c11, +gnu11, and various aliases for those modes. If no -std option is +specified, clang defaults to gnu11 mode. Many C99 and C11 features are +supported in earlier modes as a conforming extension, with a warning. Use +``-pedantic-errors`` to request an error if a feature from a later standard +revision is used in an earlier mode. Differences between all ``c*`` and ``gnu*`` modes: @@ -1514,6 +1517,11 @@ Differences between ``*89`` and ``*99`` modes: in ``*89`` modes. - Some warnings are different. +Differences between ``*99`` and ``*11`` modes: + +- Warnings for use of C11 features are disabled. +- ``__STDC_VERSION__`` is defined to ``201112L`` rather than ``199901L``. + c94 mode is identical to c89 mode except that digraphs are enabled in c94 mode (FIXME: And ``__STDC_VERSION__`` should be defined!). diff --git a/lib/Basic/Targets.cpp b/lib/Basic/Targets.cpp index 2798601a51..957448a2d4 100644 --- a/lib/Basic/Targets.cpp +++ b/lib/Basic/Targets.cpp @@ -531,8 +531,8 @@ protected: // Solaris headers require _XOPEN_SOURCE to be set to 600 for C99 and // newer, but to 500 for everything else. feature_test.h has a check to // ensure that you are not using C99 with an old version of X/Open or C89 - // with a new version. - if (Opts.C99 || Opts.C11) + // with a new version. + if (Opts.C99) Builder.defineMacro("_XOPEN_SOURCE", "600"); else Builder.defineMacro("_XOPEN_SOURCE", "500"); @@ -4584,7 +4584,7 @@ public: if (Opts.FastMath || Opts.FiniteMathOnly) Builder.defineMacro("__ARM_FP_FAST"); - if ((Opts.C99 || Opts.C11) && !Opts.Freestanding) + if (Opts.C99 && !Opts.Freestanding) Builder.defineMacro("__ARM_FP_FENV_ROUNDING"); Builder.defineMacro("__ARM_SIZEOF_WCHAR_T", Opts.ShortWChar ? "2" : "4"); diff --git a/lib/Frontend/CompilerInvocation.cpp b/lib/Frontend/CompilerInvocation.cpp index 19eecdbaed..373842d238 100644 --- a/lib/Frontend/CompilerInvocation.cpp +++ b/lib/Frontend/CompilerInvocation.cpp @@ -1129,7 +1129,7 @@ void CompilerInvocation::setLangDefaults(LangOptions &Opts, InputKind IK, case IK_PreprocessedC: case IK_ObjC: case IK_PreprocessedObjC: - LangStd = LangStandard::lang_gnu99; + LangStd = LangStandard::lang_gnu11; break; case IK_CXX: case IK_PreprocessedCXX: diff --git a/test/Lexer/has_extension.c b/test/Lexer/has_extension.c index 3b08510aa4..2ab3d1df37 100644 --- a/test/Lexer/has_extension.c +++ b/test/Lexer/has_extension.c @@ -1,5 +1,5 @@ -// RUN: %clang_cc1 -E %s -o - | FileCheck --check-prefix=CHECK-PED-NONE %s -// RUN: %clang_cc1 -pedantic-errors -E %s -o - | FileCheck --check-prefix=CHECK-PED-ERR %s +// RUN: %clang_cc1 -std=c99 -E %s -o - | FileCheck --check-prefix=CHECK-PED-NONE %s +// RUN: %clang_cc1 -std=c99 -pedantic-errors -E %s -o - | FileCheck --check-prefix=CHECK-PED-ERR %s // CHECK-PED-NONE: no_dummy_extension #if !__has_extension(dummy_extension) diff --git a/test/Lexer/has_feature_c1x.c b/test/Lexer/has_feature_c1x.c index e26e309c03..cba329cfb5 100644 --- a/test/Lexer/has_feature_c1x.c +++ b/test/Lexer/has_feature_c1x.c @@ -1,5 +1,11 @@ -// RUN: %clang_cc1 -E -triple x86_64-linux-gnu -std=c1x %s -o - | FileCheck --check-prefix=CHECK-1X %s -// RUN: %clang_cc1 -E %s -o - | FileCheck --check-prefix=CHECK-NO-1X %s +// RUN: %clang_cc1 -E -triple x86_64-linux-gnu -std=c89 %s -o - | FileCheck --check-prefix=CHECK-NO-1X %s +// RUN: %clang_cc1 -E -triple x86_64-linux-gnu -std=iso9899:199409 %s -o - | FileCheck --check-prefix=CHECK-NO-1X %s +// RUN: %clang_cc1 -E -triple x86_64-linux-gnu -std=c99 %s -o - | FileCheck --check-prefix=CHECK-NO-1X %s +// RUN: %clang_cc1 -E -triple x86_64-linux-gnu -std=c11 %s -o - | FileCheck --check-prefix=CHECK-1X %s +// +// RUN: %clang_cc1 -E -triple x86_64-linux-gnu -std=gnu89 %s -o - | FileCheck --check-prefix=CHECK-NO-1X %s +// RUN: %clang_cc1 -E -triple x86_64-linux-gnu -std=gnu99 %s -o - | FileCheck --check-prefix=CHECK-NO-1X %s +// RUN: %clang_cc1 -E -triple x86_64-linux-gnu -std=gnu11 %s -o - | FileCheck --check-prefix=CHECK-1X %s #if __has_feature(c_atomic) int has_atomic(); diff --git a/test/Parser/c11-noreturn.c b/test/Parser/c11-noreturn.c index e61901dfb7..6c01b5533a 100644 --- a/test/Parser/c11-noreturn.c +++ b/test/Parser/c11-noreturn.c @@ -1,5 +1,5 @@ // RUN: %clang_cc1 -std=c11 -fsyntax-only -verify %s -// RUN: not %clang_cc1 -pedantic -fsyntax-only %s 2>&1 | FileCheck -check-prefix=CHECK-EXT %s +// RUN: not %clang_cc1 -std=c99 -pedantic -fsyntax-only %s 2>&1 | FileCheck -check-prefix=CHECK-EXT %s _Noreturn int f(); int _Noreturn f(); // expected-note {{previous}} diff --git a/test/Parser/c1x-alignas.c b/test/Parser/c1x-alignas.c index 5b29df262d..ce8436c12f 100644 --- a/test/Parser/c1x-alignas.c +++ b/test/Parser/c1x-alignas.c @@ -1,5 +1,5 @@ // RUN: %clang_cc1 -std=c11 -fsyntax-only -verify %s -// RUN: not %clang_cc1 -pedantic -fsyntax-only %s 2>&1 | FileCheck -check-prefix=CHECK-EXT %s +// RUN: not %clang_cc1 -std=c99 -pedantic -fsyntax-only %s 2>&1 | FileCheck -check-prefix=CHECK-EXT %s _Alignas(4) char c1; unsigned _Alignas(long) char c2; diff --git a/test/Preprocessor/init.c b/test/Preprocessor/init.c index b45c2c944a..1afdbcaaa9 100644 --- a/test/Preprocessor/init.c +++ b/test/Preprocessor/init.c @@ -80,7 +80,7 @@ // COMMON:#define __ORDER_LITTLE_ENDIAN__ 1234 // COMMON:#define __ORDER_PDP_ENDIAN__ 3412 // COMMON:#define __STDC_HOSTED__ 1 -// COMMON:#define __STDC_VERSION__ +// COMMON:#define __STDC_VERSION__ 201112L // COMMON:#define __STDC__ 1 // COMMON:#define __VERSION__ // COMMON:#define __clang__ 1 @@ -2546,7 +2546,7 @@ // MIPS32BE:#define __SIZE_TYPE__ unsigned int // MIPS32BE:#define __SIZE_WIDTH__ 32 // MIPS32BE:#define __STDC_HOSTED__ 0 -// MIPS32BE:#define __STDC_VERSION__ 199901L +// MIPS32BE:#define __STDC_VERSION__ 201112L // MIPS32BE:#define __STDC__ 1 // MIPS32BE:#define __UINT16_C_SUFFIX__ {{$}} // MIPS32BE:#define __UINT16_MAX__ 65535 @@ -5458,7 +5458,7 @@ // PPC-DARWIN:#define __SIZE_TYPE__ long unsigned int // PPC-DARWIN:#define __SIZE_WIDTH__ 32 // PPC-DARWIN:#define __STDC_HOSTED__ 0 -// PPC-DARWIN:#define __STDC_VERSION__ 199901L +// PPC-DARWIN:#define __STDC_VERSION__ 201112L // PPC-DARWIN:#define __STDC__ 1 // PPC-DARWIN:#define __UINT16_C_SUFFIX__ {{$}} // PPC-DARWIN:#define __UINT16_MAX__ 65535 diff --git a/test/Preprocessor/line-directive.c b/test/Preprocessor/line-directive.c index 0dd658f7f1..2ebe87e468 100644 --- a/test/Preprocessor/line-directive.c +++ b/test/Preprocessor/line-directive.c @@ -1,4 +1,4 @@ -// RUN: %clang_cc1 -fsyntax-only -verify -pedantic %s +// RUN: %clang_cc1 -std=c99 -fsyntax-only -verify -pedantic %s // RUN: not %clang_cc1 -E %s 2>&1 | grep 'blonk.c:92:2: error: ABC' // RUN: not %clang_cc1 -E %s 2>&1 | grep 'blonk.c:93:2: error: DEF' diff --git a/test/Sema/anonymous-struct-union-c11.c b/test/Sema/anonymous-struct-union-c11.c index 229ee52057..712e29e549 100644 --- a/test/Sema/anonymous-struct-union-c11.c +++ b/test/Sema/anonymous-struct-union-c11.c @@ -1,8 +1,8 @@ // Check for warnings in non-C11 mode: -// RUN: %clang_cc1 -fsyntax-only -verify -Wc11-extensions %s +// RUN: %clang_cc1 -fsyntax-only -std=c99 -verify -Wc11-extensions %s // Expect no warnings in C11 mode: -// RUN: %clang_cc1 -fsyntax-only -pedantic -Werror -std=c11 %s +// RUN: %clang_cc1 -fsyntax-only -std=c11 -pedantic -Werror %s struct s { int a; diff --git a/test/Sema/array-init.c b/test/Sema/array-init.c index 4cc5e412c2..7cb4815316 100644 --- a/test/Sema/array-init.c +++ b/test/Sema/array-init.c @@ -1,5 +1,5 @@ -// RUN: %clang_cc1 -fsyntax-only -pedantic -verify %s -// RUN: %clang_cc1 -fsyntax-only -Wgnu -Wc11-extensions -verify %s +// RUN: %clang_cc1 -std=gnu99 -fsyntax-only -pedantic -verify %s +// RUN: %clang_cc1 -std=gnu99 -fsyntax-only -Wgnu -Wc11-extensions -verify %s // REQUIRES: LP64 extern int foof() = 1; // expected-error{{illegal initializer (only variables can be initialized)}} diff --git a/test/Sema/attr-deprecated.c b/test/Sema/attr-deprecated.c index c9e3dd546c..2e3e722942 100644 --- a/test/Sema/attr-deprecated.c +++ b/test/Sema/attr-deprecated.c @@ -121,6 +121,6 @@ struct test22 { __attribute((deprecated)) foo_dep e, f; }; -typedef int test23_ty __attribute((deprecated)); // expected-note {{previous definition is here}} -typedef int test23_ty; // expected-note {{'test23_ty' has been explicitly marked deprecated here}} expected-warning {{redefinition of typedef 'test23_ty' is a C11 feature}} +typedef int test23_ty __attribute((deprecated)); +typedef int test23_ty; // expected-note {{'test23_ty' has been explicitly marked deprecated here}} test23_ty test23_v; // expected-warning {{'test23_ty' is deprecated}} diff --git a/test/Sema/types.c b/test/Sema/types.c index 6a22b20b0e..1ed8dae4c8 100644 --- a/test/Sema/types.c +++ b/test/Sema/types.c @@ -30,12 +30,12 @@ int c() { int __int128; // expected-error {{cannot combine with previous}} expected-warning {{does not declare anything}} } // __int128_t is __int128; __uint128_t is unsigned __int128. -typedef __int128 check_int_128; // expected-note {{here}} -typedef __int128_t check_int_128; // expected-note {{here}} expected-warning {{redefinition}} +typedef __int128 check_int_128; +typedef __int128_t check_int_128; // expected-note {{here}} typedef int check_int_128; // expected-error {{different types ('int' vs '__int128_t' (aka '__int128'))}} -typedef unsigned __int128 check_uint_128; // expected-note {{here}} -typedef __uint128_t check_uint_128; // expected-note {{here}} expected-warning {{redefinition}} +typedef unsigned __int128 check_uint_128; +typedef __uint128_t check_uint_128; // expected-note {{here}} typedef int check_uint_128; // expected-error {{different types ('int' vs '__uint128_t' (aka 'unsigned __int128'))}} // Array type merging should convert array size to whatever matches the target diff --git a/www/compatibility.html b/www/compatibility.html index 8bfaff191c..293be6f220 100644 --- a/www/compatibility.html +++ b/www/compatibility.html @@ -83,10 +83,10 @@

C99 inline functions

-

By default, Clang builds C code according to the C99 standard, -which provides different semantics for the inline keyword -than GCC's default behavior. For example, consider the following -code:

+

By default, Clang builds C code in GNU C11 mode, so it uses standard C99 +semantics for the inline keyword. These semantics are different +from those in GNU C89 mode, which is the default mode in versions of GCC +prior to 5.0. For example, consider the following code:

 inline int add(int i, int j) { return i + j; }
 
@@ -110,10 +110,10 @@ Undefined symbols:
       _main in cc-y1jXIr.o
 
-

By contrast, GCC's default behavior follows the GNU89 dialect, -which is the C89 standard plus a lot of extensions. C89 doesn't have -an inline keyword, but GCC recognizes it as an extension -and just treats it as a hint to the optimizer.

+

By contrast, GNU C89 mode (used by default in older versions of GCC) is the +C89 standard plus a lot of extensions. C89 doesn't have an inline +keyword, but GCC recognizes it as an extension and just treats it as a hint to +the optimizer.

There are several ways to fix this problem:

@@ -130,12 +130,12 @@ and just treats it as a hint to the optimizer.

for a function to be inlined, nor does it guarantee that it will be. Some compilers ignore it completely. Clang treats it as a mild suggestion from the programmer. - +
  • Provide an external (non-inline) definition of add somewhere else in your program. The two definitions must be equivalent!
  • -
  • Compile with the GNU89 dialect by adding +
  • Compile in the GNU C89 dialect by adding -std=gnu89 to the set of Clang options. This option is only recommended if the program source cannot be changed or if the program also relies on additional C89-specific behavior that cannot