Allow a lot more C99 features

Flexible array members are supported by gcc, clang and even MSVC2013.
Note, so far it was only used in the Windows-specific airpcap.h.

Trailing commas in enum declaration are already in use since for
these dissectors (commit ID is the first occurrence):
epan/dissectors/packet-gluster.h v2.1.0rc0-1070-g3b706ba
epan/dissectors/packet-ipv6.c v2.1.2rc0-81-ge07b4aa
epan/dissectors/packet-netlink.h v2.3.0rc0-389-gc0ab12b
epan/dissectors/packet-netlink-netfilter.c v2.3.0rc0-239-g1767e08
epan/dissectors/packet-netlink-route.c v2.3.0rc0-233-g2a80b40
epan/dissectors/packet-quic.c v2.3.0rc0-457-gfa320f8

Inline functions using the "inline" keyword are supported via all glib
versions we support (if it is missing, glib will define a suitable
inline macro).

Other c99 functions listed in the README.developer document were found
to be compatible with GCC 4.4.7, Clang 3.4.2 and MSVC 2013.

Change-Id: If5bab03bfd8577b15a24bedf08c03bdfbf34317a
Reviewed-on: https://code.wireshark.org/review/17421
Reviewed-by: Guy Harris <guy@alum.mit.edu>
This commit is contained in:
Peter Wu 2016-08-31 15:58:20 +02:00 committed by Guy Harris
parent 5eb9170227
commit 232a879429
5 changed files with 33 additions and 43 deletions

View File

@ -308,7 +308,9 @@ if( CMAKE_C_COMPILER_ID MATCHES "MSVC")
# when compiling Wireshark sources. (Selected from "level 4" warnings).
## 4295: array is too small to include a terminating null character
## 4189: local variable is initialized but not referenced
set(WARNINGS_CFLAGS "/w34295 /w34189")
# Disable warnings about about use of flexible array members:
## 4200: nonstandard extension used : zero-sized array in struct/union
set(WARNINGS_CFLAGS "/w34295 /w34189 /wd4200")
set(WIRESHARK_COMMON_FLAGS
${LOCAL_CFLAGS}
@ -343,6 +345,17 @@ else()
endif()
endif()
if(CMAKE_VERSION VERSION_LESS "3.1")
# Many modern compilers use c99 by default, but for older ones
# (like GCC 4.4.7), -std=gnu99 is required to avoid errors about
# use constructs like "for (int i = 0; i < n; i++) ;"
if(CMAKE_C_COMPILER_ID MATCHES "GNU")
set(CMAKE_C_FLAGS "-std=gnu99 ${CMAKE_C_FLAGS}")
endif()
else()
set(CMAKE_C_STANDARD 99)
endif()
if(CMAKE_C_COMPILER_ID MATCHES "Clang")
set(WIRESHARK_COMMON_FLAGS ${WIRESHARK_COMMON_FLAGS}
# avoid "argument unused during compilation" warnings
@ -391,8 +404,6 @@ else()
# The Qt headers generate a ton of shortening errors on 64-bit systems
# so only enable this for C for now.
-Wshorten-64-to-32
# Clang only
-Wc99-extensions
)
set(CXX_WARN_FLAGS

View File

@ -23,8 +23,6 @@
#define AIRPCAP_H__EAE405F5_0171_9592_B3C2_C19EC426AD34__INCLUDED_
#ifdef _MSC_VER
/* This disables a VS warning for zero-sized arrays. */
#pragma warning( disable : 4200)
/* This stops VS2005 ranting against stdio. */
#pragma warning( disable : 4996)
#endif

View File

@ -69,10 +69,12 @@ AC_CONFIG_LIBOBJ_DIR([wsutil])
#
# Checks for programs used in the main build process.
#
AC_PROG_CC_STDC
if test "$ac_cv_prog_cc_stdc" = "no"
# See doc/README.developer for allowed C99 features
#
AC_PROG_CC_C99
if test "$ac_cv_prog_cc_c99" = "no"
then
AC_MSG_ERROR([The C compiler does not support standard C])
AC_MSG_ERROR([The C compiler does not support C99])
fi
AC_PROG_CPP
@ -690,9 +692,6 @@ then
fi
fi
#Clang only
AC_WIRESHARK_COMPILER_FLAGS_CHECK(-Wc99-extensions, C)
#
# Try to add some additional checks to CFLAGS.
# These are not enabled by default, because the warnings they produce

View File

@ -67,18 +67,20 @@ Wireshark runs on many platforms, and can be compiled with a number of
different compilers; here are some rules for writing code that will work
on multiple platforms.
Don't use C++-style comments (comments beginning with "//" and running
to the end of the line) in C code. Wireshark's dissectors are written in
C, and thus run through C rather than C++ compilers, and not all C
compilers support C++-style comments (GCC does, but IBM's C compiler for
AIX, for example, doesn't do so by default). C++-style comments can be
used in C++ code, of course.
In general, don't use C99 features since some C compilers used to compile
Wireshark, such as Microsoft's C compiler, don't support all C99
In general, not all C99 features can be used since some C compilers used to
compile Wireshark, such as Microsoft's C compiler, don't support all C99
features. The C99 features that can be used are:
variadic macros
- flexible array members
- compound literals
- designated initializers
- "//" comments
- mixed declarations and code
- new block scopes for selection and iteration statements (that is, declaring
the type in a for-loop like: for (int i = 0; i < n; i++) ;)
- macros with a variable number of arguments (variadic macros)
- trailing comma in enum declarations
- inline functions (guaranteed only by use of glib.h)
Don't initialize variables in their declaration with non-constant
values. Not all compilers support this. E.g. don't use
@ -88,12 +90,8 @@ use
i = somearray[2];
instead.
Don't use zero-length arrays; not all compilers support them. If an
array would have no members, just leave it out.
Don't declare variables in the middle of executable code; not all C
compilers support that. Variables should be declared outside a
function, or at the beginning of a function or compound statement.
Don't use zero-length arrays as structure members, use flexible array members
instead.
Don't use anonymous unions; not all compilers support them.
Example:
@ -296,9 +294,6 @@ not *little-endian* byte order; not all machines on which Wireshark runs
are little-endian, even though PCs are. Fetch those values using
"tvb_get_letohs()" and "tvb_get_letohl()".
Don't put a comma after the last element of an enum - some compilers may
either warn about it (producing extra noise) or refuse to accept it.
Do not use "open()", "rename()", "mkdir()", "stat()", "unlink()", "remove()",
"fopen()", "freopen()" directly. Instead use "ws_open()", "ws_rename()",
"ws_mkdir()", "ws_stat()", "ws_unlink()", "ws_remove()", "ws_fopen()",
@ -360,8 +355,6 @@ as some compilers will reject the first of those statements. Instead,
initialize the array at the point at which it's first declared, so that
the size is known.
Don't put a comma after the last tuple of an initializer of an array.
For #define names and enum member names, prefix the names with a tag so
as to avoid collisions with other names - this might be more of an issue
on Windows, as it appears to #define names such as DELETE and

View File

@ -2042,7 +2042,6 @@ while ($_ = pop @filelist)
my $fileContents = '';
my @foundAPIs = ();
my $line;
my $prohibit_cpp_comments = 1;
if ($source_dir and ! -e $filename) {
$filename = $source_dir . '/' . $filename;
@ -2059,10 +2058,6 @@ while ($_ = pop @filelist)
next;
}
# Establish or remove local taboos
if ($filename =~ m{ ui/qt/ }x) { $prohibit_cpp_comments = 0; }
if ($filename =~ m{ image/*.rc }x) { $prohibit_cpp_comments = 0; }
# Read in the file (ouch, but it's easier that way)
open(FC, $filename) || die("Couldn't open $filename");
$line = 1;
@ -2139,12 +2134,6 @@ while ($_ = pop @filelist)
#$errorCount += check_ett_registration(\$fileContents, $filename);
if ($prohibit_cpp_comments && $fileContents =~ m{ \s// }xo)
{
print STDERR "Error: Found C++ style comments in " .$filename."\n";
$errorCount++;
}
# Remove all blank lines
$fileContents =~ s{ ^ \s* $ } []xog;