Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False problem report for C preprocessor macro #9187

Closed
nordic-krch opened this issue Apr 14, 2022 · 2 comments
Closed

False problem report for C preprocessor macro #9187

nordic-krch opened this issue Apr 14, 2022 · 2 comments
Assignees
Labels
Language Service more info needed The issue report is not actionable in its current state

Comments

@nordic-krch
Copy link

Bug type: Language Service

Describe the bug
In zephyr project https:/zephyrproject-rtos/zephyr we are using a macro which is able to paste one of two code snippets based on the flag argument. It is a preprocessor trick derived from Linux IS_ENABLED macro. Macro has following syntax:
COND_CODE_0(flag, (if_flag_code), (else_code)), e.g.

COND_CODE_0(0, (printf("000000");), (printf("1111111");))
/*Resolves to printf("0000000"); */

COND_CODE_0(1, (printf("000000");), (printf("1111111");))
/*Resolves to printf("1111111"); */

The problem with VSCode is analyzes the path that is being excluded by this macro and report warning for that. We have a use case where flag is a macro that calculates number of input arguments and calls different macros with different number of arguments. Something like:

#define MACRO_1ARG(x) printf("%d\n", x)
#define MACRO_2ARGS(x, y) printf("%d %d\n", x, y)
#define COUNT_ARGS_LESS1(...) /* Macro returns number of input arguments - 1 */
#define TEST(...) COND_CODE_0(COUNT_ARGS_LESS1(__VA_ARGS__), (MACRO_1ARG(__VA_ARGS__), MACRO_2ARGS(__VA_ARGS__))

void foo(void) {
    TEST(100);
    TEST(100, 100);
}

In case of TEST(100) arguments counting passed to COND_CODE_0 results in cutting second path (MACRO_2ARGS). Code compiles fine, there is no misuse of the macro but VSCode notifies about the issue.

  • OS and Version: Windows 10.0.19044
  • VS Code Version: 1.66.2
  • C/C++ Extension Version: 1.1.0

Steps to reproduce
Here is a code snippet which triggers this error:

#define _Z_COND_CODE_0(_flag, _if_0_code, _else_code) \
	___COND_CODE(__ZZZZ##_flag, _if_0_code, _else_code)
#define __ZZZZ0 _YYYY,

#define ___COND_CODE(one_or_two_args, _if_code, _else_code) \
	___GET_ARG2_DEBRACKET(one_or_two_args _if_code, _else_code)

#define ___GET_ARG2_DEBRACKET(ignore_this, val, ...) ___DEBRACKET val

#define ___DEBRACKET(...) __VA_ARGS__

#define TEST_MACRO_2(x, y) printk("%d %d", x, y)
/* Flag is fixed to 0, so second option is never compiled and shouldn't be evaluated by Intellisense */
#define TEST_MACRO3(...) _Z_COND_CODE_0(0, (printk("ss")), (TEST_MACRO_2(__VA_ARGS__)))
void test_macro_func(int x)
{
       /* Macro called with one parameter */
	TEST_MACRO3(x);
}

error reported:
image

Expected behavior
Intellisense should figure out that given path is stripped by the preprocessor and do not report the error.

nordic-krch added a commit to nordic-krch/zephyr that referenced this issue Apr 15, 2022
VSCode macro parser was notifying errors in logging macros. The reason
was char pointer counting macro that VSCode seen as being called with
no enough parameters. There was no issue in the macro so change is
rather to cover for VSCode annoyance.

Issue reported:github.com/microsoft/vscode-cpptools/issues/9187

Additionally, minor change added to log as CBPRINTF_MUST_RUNTIME_PACKAGE
is no longer accepting empty arguments.

Signed-off-by: Krzysztof Chruscinski <[email protected]>
@bobbrow bobbrow added bug Language Service investigate: repro This issue's repro steps needs to be investigated/confirmed labels Apr 19, 2022
@sean-mcmanus
Copy link
Collaborator

sean-mcmanus commented Apr 22, 2022

The sample code doesn't compile for me with the latest gcc, clang, or cl.exe -- which compiler (and version) and compiler args are you using to compile that?

e.g.: error: macro "TEST_MACRO_2" requires 2 arguments, but only 1 given
19 | TEST_MACRO3(x);

@sean-mcmanus sean-mcmanus self-assigned this Apr 22, 2022
@sean-mcmanus sean-mcmanus added more info needed The issue report is not actionable in its current state and removed bug investigate: repro This issue's repro steps needs to be investigated/confirmed labels Apr 22, 2022
@github-actions
Copy link

This issue has been closed because it needs more information and has not had recent activity.

@github-actions github-actions bot locked and limited conversation to collaborators Aug 6, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Language Service more info needed The issue report is not actionable in its current state
Projects
None yet
Development

No branches or pull requests

3 participants