Issue with __VA_OPT__() keyword not expanding properly in a variadic macro
Bug type: Language Service
Describe the bug
- OS and Version: Linux Ubuntu 20.04
- VS Code Version: 1.60.2
- C/C++ Extension Version: v1.7.1
- Other extensions you installed (and if the issue persists after disabling them): Python, Pylance, PlatformIO, Shader language support for VS Code, Todo+, Jupyter (after disabling all, the problem still persists).
- If using SSH remote, specify OS of remote machine:
- A clear and concise description of what the bug is, including information about the workspace (i.e. is the workspace a single project or multiple projects, size of the project, etc).
I was trying to program a C++ variadic macro that would print an error message to
stderrlike this:
#define __pError(message, ...) \
{\
fprintf(stderr, "\x1B[31mERROR: Error at file %s, line %d:\x1B[0m ",\
__FILE__, __LINE__);\
fprintf(stderr, message __VA_OPT__(,) __VA_ARGS__);\
fprintf(stderr, "\n");\
exit(-1);\
}
I then used the macro like this:
__pError("I am going to produce an error!");
Now, this should work, but instead I get an error message: " expected a ')' " When I compile my project using g++, I don't get errored out; it compiles just fine. I think the problem is with the VA_OPT() keyword; maybe it isn't implemented?
Steps to reproduce
- Make a file, let's call it file.h
- Paste the macro definition code into said file.
- At the top, put
#pragma once,#include <stdio.h>and#include <stdlib.h>. - Make another file, let's call it main.cpp.
- Insert
#include <file.h>at the top - Update
includePathin VSCode cpp configuration files to include file.h. - Put in a main function:
int main()
{
__pError("Why am I recieving an error?");
return 0;
}
- See error
Expected behavior
There should be no error; no red squiggly lines.
Code sample and logs
- Code sample file.h:
#pragma once
#define __pError(message, ...) \
{\
fprintf(stderr, "\x1B[31mERROR: Error at file %s, line %d:\x1B[0m ",\
__FILE__, __LINE__);\
fprintf(stderr, message __VA_OPT__(,) __VA_ARGS__);\
fprintf(stderr, "\n");\
exit(-1);\
}
main.cpp:
#include "file.h"
int main()
{
__pError("Why am I recieving an error?");
return 0;
}
- Configurations in
c_cpp_properties.json
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/include"
],
"defines": ["GL_GLEXT_PROTOTYPES"],
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu17",
"cppStandard": "gnu++14",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}
- Logs from running
C/C++: Log Diagnosticsfrom the VS Code command palette
-------- Diagnostics - 10/30/2021, 6:54:58 PM
Version: 1.7.1
Current Configuration:
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/include"
],
"defines": [
"GL_GLEXT_PROTOTYPES"
],
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu17",
"cppStandard": "gnu++14",
"intelliSenseMode": "linux-gcc-x64",
"compilerPathIsExplicit": true,
"cStandardIsExplicit": true,
"cppStandardIsExplicit": true,
"intelliSenseModeIsExplicit": true,
"compilerArgs": [],
"mergeConfigurations": false,
"browse": {
"path": [
"${workspaceFolder}/include",
"${workspaceFolder}"
],
"limitSymbolsToIncludedHeaders": true
}
}
Translation Unit Mappings:
[ /home/ben/c/game/src/test.cpp ]:
/home/ben/c/game/src/test.cpp
/home/ben/c/game/include/Utils.h
Translation Unit Configurations:
[ /home/ben/c/game/src/test.cpp ]:
Process ID: 40704
Memory Usage: 14 MB
Compiler Path: /usr/bin/gcc
Includes:
/home/ben/c/game/include
/usr/include/c++/9
/usr/include/x86_64-linux-gnu/c++/9
/usr/include/c++/9/backward
/usr/lib/gcc/x86_64-linux-gnu/9/include
/usr/local/include
/usr/include/x86_64-linux-gnu
/usr/include
Defines:
GL_GLEXT_PROTOTYPES
Standard Version: c++14
IntelliSense Mode: linux-gcc-x64
Other Flags:
--g++
--gnu_version=90300
Total Memory Usage: 14 MB
------- Workspace parsing diagnostics -------
Number of files discovered (not excluded): 5969
- Logs from the language server logging
Screenshots
None should be needed, but some can be provided if necessary.
Additional context
If more context is needed, I can provide, but I don't think any more is necessary.
Hi @HilbertCurve . Thanks for reporting this. I can reproduce the issue. Although the IntelliSense implementation is shared with VS, I'm not able to repro this in VS, so it appears to be a bug in cpptools.
I've encountered the same problem, is there any progress or workaround on this issue?
Not exactly this problem but for searchability: with MSVC I was seeing this issue because I did not define /Zc:preprocessor in the compilerArgs setting.
Hey, this seems to be the same issues as here https://developercommunity.visualstudio.com/t/microsoft-visual-studio-2017-enterprise-cc-va-args/1231753?viewtype=all but it seems it still hasn't been fixed in VSCode