I think it's not supported by any compiler, but the macro expansion for p1("hello") is different than directly call printf("hello",), it's expanded into printf("hello") without trialling comma, though different compiler accepts different syntax on different platform.
The second one is supported by MSVC and clang-cl, possible Intel C++ for Visual Studio.
first one is supported by GCC, clang, Intel, and MSVC 2017/2019:
cppcheck.exe test.cpp
Checking test.cpp ...
test.cpp:6:2: error: There is an unknown macro here somewhere. Configuration is required. If __VA_OPT__ is a macro then please configure it. [unknownMacro]
p1("%s", "hello");
^
Well for the other inconsistency it seems to me the standard compliant way is to keep the comma so maybe we should keep simplecpp as it is. maybe it's best that cppcheck removes the comma. I created ticket https://trac.cppcheck.net/ticket/9754 .
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
About removing the comma.. hmm that is a problem that we have not bumped into before and it's a deprecated feature. I spontaneosly feels that the payoff would not be that high for it.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
__VA_ARGS__ is the traditional msvc way, ##__VA_ARGS__ (msvc 2017 and later is supported in most compiler), __VA_OPT__ is the standard way (c++20, c2x) .
I think __VA_ARGS__ can be reported as non-portable code (not necessary syntax error at least for msvc project), so cppcheck continue checking remaining file after the trailing comma, currently cppechek stops once a syntax error is met.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
ok thanks.. is there some compiler that allows the code
printf("hello",);
?I think it's not supported by any compiler, but the macro expansion for p1("hello") is different than directly call printf("hello",), it's expanded into printf("hello") without trialling comma, though different compiler accepts different syntax on different platform.
https://godbolt.org/z/x4X_BA
I am talking about the second macro because Cppcheck writes a syntax error when that is used:
For that macro, the preprocessor should add the trailing comma as far as I know. For instance "gcc -E" outputs a comma when that macro is used.
If it's not supported by any compiler, I am happy about the Cppcheck warning. If you only care about gcc/clang I suggest
-f -D__GNUC__
.The second one is supported by MSVC and clang-cl, possible Intel C++ for Visual Studio.
first one is supported by GCC, clang, Intel, and MSVC 2017/2019:
https://godbolt.org/z/x4X_BA
https://devblogs.microsoft.com/cppblog/msvc-preprocessor-progress-towards-conformance/
Anothor issue is C++20's
__VA_OPT__
:Cppcheck 2.0 (not tested master code):
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0306r4.html
https://devblogs.microsoft.com/cppblog/msvc-preprocessor-progress-towards-conformance/
for following code:
g++ -E test.cpp or gcc -x c -E test.cpp (with g++ 10.1 from msys2):
clang++ -E test.cpp (Clang 11 Windows default target msvc):
clang++ -E -std=c++20 test.cpp
cppcheck (master) shows syntax error for p2 and p3.
It seems to me that
__VA_OPT__
should be handled in the preprocessor directly so I created https://github.com/danmar/simplecpp/issues/191 for that.Well for the other inconsistency it seems to me the standard compliant way is to keep the comma so maybe we should keep simplecpp as it is. maybe it's best that cppcheck removes the comma. I created ticket https://trac.cppcheck.net/ticket/9754 .
About removing the comma.. hmm that is a problem that we have not bumped into before and it's a deprecated feature. I spontaneosly feels that the payoff would not be that high for it.
latest doc for gcc and msvc:
https://gcc.gnu.org/onlinedocs/cpp/Variadic-Macros.html
https://docs.microsoft.com/en-us/cpp/preprocessor/preprocessor-experimental-overview#comma-elision-in-variadic-macros
__VA_ARGS__
is the traditional msvc way,##__VA_ARGS__
(msvc 2017 and later is supported in most compiler),__VA_OPT__
is the standard way (c++20, c2x) .I think
__VA_ARGS__
can be reported as non-portable code (not necessary syntax error at least for msvc project), so cppcheck continue checking remaining file after the trailing comma, currently cppechek stops once a syntax error is met.