microsoft / vscode-cpptools

Official repository for the Microsoft C/C++ extension for VS Code.
Other
5.46k stars 1.53k forks source link

Semantic token for _countof macro specifically is incorrect #12408

Closed leidegre closed 2 months ago

leidegre commented 3 months ago

Environment

Bug Summary and Steps to Reproduce

This looks correct but note the extra whitespace after the first left parenthesis.

image

In the video below you can see what happens as I just change the whitespace.

https://github.com/microsoft/vscode-cpptools/assets/63085/63108375-e1a6-4c78-bff7-d1098a57c190

image

image

It's incorrectly somehow counting part of the macro arguments as the macro itself. This seems to happen everywhere the _countof macro is used except inside other macros.

For example, here it's correct

image

But everywhere else it is not

image

Configuration and Logs

{
  "version": 4, 
  "configurations": [
    {
      "name": "Win32 (Debug)",
      "includePath": ["third-party/include", "src"],
      "defines": ["_DEBUG", "_CRT_SECURE_NO_WARNINGS"],
      "compilerArgs": ["/W4", "/wd4100", "/WX"],
      "windowsSdkVersion": "10.0.22621.0",
      "compilerPath": "cl.exe",
      "cStandard": "c17",
      "cppStandard": "c++20",
      "intelliSenseMode": "windows-msvc-x64"
    }
  ]
}

I don't have a "C/C++: Log Diagnostics" command in VS Code.

Other Extensions

No response

Additional context

No response

browntarik commented 2 months ago

Could you provide the code snippet that is causing the issue?

Does this issue persist when all other extension except for C/C++ are disabled?

leidegre commented 2 months ago

If I try to goto the definition of the _countof macro I end up here, in vcruntime.h. __crt_countof happens to be the same length as the incorrectly colored tokens, so it is somehow mistaking _countof for __crt_countof (it's five additional characters that gets colored incorrectly).

#ifdef __cplusplus
    extern "C++"
    {
        template <typename _CountofType, size_t _SizeOfArray>
        char (*__countof_helper(_UNALIGNED _CountofType (&_Array)[_SizeOfArray]))[_SizeOfArray];

        #define __crt_countof(_Array) (sizeof(*__countof_helper(_Array)) + 0)
    }
#else
    #define __crt_countof(_Array) (sizeof(_Array) / sizeof(_Array[0]))
#endif

The actual definition of _countof is in stdlib.h and looks like this

#ifndef _countof
    #define _countof __crt_countof
#endif

This happens with all other extensions disabled.

Colengms commented 2 months ago

Hi @leidegre I can repro this issue with 1.20.5, but it appears to be fixed in the latest prerelease (1.21.0). Closing as fixed.

leidegre commented 1 month ago

@Colengms Looks good on my machine now after update. Thanks!