microsoft / vscode-cpptools

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

[IntelliSense C] The "defined" operator is not correctly interpreted by #if #11959

Open stb-fr opened 7 months ago

stb-fr commented 7 months ago

Environment

Bug Summary and Steps to Reproduce

​The VSCode editor doesn't seem to interpret #if statements correctly when using the special operator "defined"

Example with the following code:

#define A_DEFINED_VARIABLE          1

#define CHECK_A_DEFINED_VARIABLE    (defined(A_DEFINED_VARIABLE))

#if (defined(A_DEFINED_VARIABLE))
static int MyVar1 = 1;
#endif

#if CHECK_A_DEFINED_VARIABLE
static int MyVar2 = 2;
#endif

int main()
{
    return MyVar1 + MyVar2;
}

IntelliSense considers that MyVar1 is defined, but not MyVar2.

image

Configuration and Logs

-------- Diagnostics - 2/13/2024, 2:50:33 PM
Version: 1.18.5
Current Configuration:
{
    "name": "Win32",
    "includePath": [
        "${workspaceFolder}/**"
    ],
    "defines": [
        "_DEBUG",
        "UNICODE",
        "_UNICODE"
    ],
    "windowsSdkVersion": "10.0.22621.0",
    "compilerPath": "cl.exe",
    "cStandard": "c17",
    "cppStandard": "c++17",
    "intelliSenseMode": "windows-msvc-x64",
    "compilerPathInCppPropertiesJson": "cl.exe",
    "intelliSenseModeIsExplicit": false,
    "cStandardIsExplicit": false,
    "cppStandardIsExplicit": false,
    "mergeConfigurations": false,
    "compilerPathIsExplicit": false,
    "browse": {
        "path": [
            "${workspaceFolder}/**"
        ],
        "limitSymbolsToIncludedHeaders": true
    }
}
cpptools version (native): 1.18.3.0
Translation Unit Mappings:
[ C:\Data\Tmp\test.c ]:
    C:\Data\Tmp\test.c
Translation Unit Configurations:
[ C:\Data\Tmp\test.c ]:
    Process ID: 26476
    Memory Usage: 49 MB
    Compiler Path: C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.38.33130\bin\Hostx64\x64\cl.exe
    Includes:
        C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.38.33130\include
        C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.38.33130\atlmfc\include
        C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\um
        C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\ucrt
        C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\shared
        C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\winrt
        C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\cppwinrt
    Defines:
        _DEBUG
        UNICODE
        _UNICODE
    Standard Version: ms_c17
    IntelliSense Mode: windows-msvc-x64
Total Memory Usage: 49 MB

------- Workspace parsing diagnostics -------
Number of files discovered (not excluded): 5027

Other Extensions

No response

Additional context

No response

browntarik commented 7 months ago

Could you please provide the output from the C/C++: Log Diagnostics Command that appears after opening the file you mentioned.

image

Since I am not easily able to reproduce your scenario, my initial guess is that you somehow have an incompatible compiler and IntelliSense mode or there is an existing IntelliSense bug with one of the modes

sean-mcmanus commented 7 months ago

@stb-fr Your code gives the same error as IntelliSense when compiled with cl.exe -- did you mean to use a different compiler?

sean-mcmanus commented 7 months ago

@stb-fr Are you compiling with the /P flag? cl.exe compiles that code if that flag is used, but it seems like we're not handling that compiler arg correctly.

sean-mcmanus commented 7 months ago

@stb-fr Ignore my last comment -- the /P isn't for actually compiling.

sean-mcmanus commented 7 months ago

@stb-fr I meant to refer to /Zc:preprocessor -- if you add that to compilerArgs then IntelliSense should work correctly. If you don't intend to compile with cl.exe then you should consider changing your compilerPath.

sean-mcmanus commented 7 months ago

@stb-fr Actually, it's a bug -- when /std:c11 or /std:c17 is used, it's supposed to behave as if /Zc:preprocessor is set.

sean-mcmanus commented 7 months ago

@stb-fr Oh, but there's another bug with IntelliSense still giving an error if "/Zc:preprocessor" is used.

Also, cl.exe does give a warning warning C5105: macro expansion producing 'defined' has undefined behavior

sean-mcmanus commented 7 months ago

@stb-fr I filed a bug against our parser shared with VS at https://developercommunity.visualstudio.com/t/C-IntelliSense-preprocess-behaves-inco/10584146 .

stb-fr commented 7 months ago

@sean-mcmanus For the example, I have open a new session and I have created the example. This is the log diagnostic:

-------- Diagnostics - 2/14/2024, 8:45:56 AM
Version: 1.18.5
Current Configuration:
{
    "name": "Win32",
    "includePath": [
        "${workspaceFolder}/**"
    ],
    "defines": [
        "_DEBUG",
        "UNICODE",
        "_UNICODE"
    ],
    "windowsSdkVersion": "10.0.22621.0",
    "compilerPath": "cl.exe",
    "cStandard": "c17",
    "cppStandard": "c++17",
    "intelliSenseMode": "windows-msvc-x64",
    "compilerPathInCppPropertiesJson": "cl.exe",
    "intelliSenseModeIsExplicit": false,
    "cStandardIsExplicit": false,
    "cppStandardIsExplicit": false,
    "mergeConfigurations": false,
    "compilerPathIsExplicit": false,
    "browse": {
        "path": [
            "${workspaceFolder}/**"
        ],
        "limitSymbolsToIncludedHeaders": true
    }
}
cpptools version (native): 1.18.3.0
Translation Unit Mappings:
[ C:\Data\Tmp\test.c ]:
    C:\Data\Tmp\test.c
Translation Unit Configurations:
[ C:\Data\Tmp\test.c ]:
    Process ID: 33780
    Memory Usage: 49 MB
    Compiler Path: C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.38.33130\bin\Hostx64\x64\cl.exe
    Includes:
        C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.38.33130\include
        C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.38.33130\atlmfc\include
        C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\um
        C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\ucrt
        C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\shared
        C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\winrt
        C:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\cppwinrt
    Defines:
        _DEBUG
        UNICODE
        _UNICODE
    Standard Version: ms_c17
    IntelliSense Mode: windows-msvc-x64
Total Memory Usage: 49 MB

------- Workspace parsing diagnostics -------
Number of files discovered (not excluded): 5027

But for my project, I use the compiler IAR, but I cannot configured on VS code. I left the field "Compiler path" empty. This is the log diagnostic for on my project:

-------- Diagnostics - 2/14/2024, 8:28:48 AM
Version: 1.18.5
Current Configuration:
{
    "name": "APP_NARVA",
    "includePath": [
        "c:/Project/inc",
        "c:/Project/src"
    ],
    "defines": [
        "__intrinsic",
        "__nounwind",
        "__no_init=",
        "__packed="
    ],
    "windowsSdkVersion": "10.0.22000.0",
    "cStandard": "c17",
    "cppStandard": "c++17",
    "mergeConfigurations": false,
    "compilerPath": "",
    "browse": {
        "path": [
            "c:/Project/src"
        ],
        "limitSymbolsToIncludedHeaders": true
    },
    "compilerPathIsExplicit": true,
    "cStandardIsExplicit": true,
    "cppStandardIsExplicit": true,
    "intelliSenseModeIsExplicit": true,
    "compilerPathInCppPropertiesJson": ""
}
cpptools version (native): 1.18.3.0
Translation Unit Mappings:
[ C:\Project\src\test.c ]:
    C:\Project\src\test.c
Translation Unit Configurations:
[ C:\Project\src\test.c ]:
    Process ID: 32404
    Memory Usage: 105 MB
    Includes:
        C:\Project\inc
        C:\Project\src
    Defines:
        __intrinsic
        __nounwind
        __no_init=
        __packed=
    Standard Version: ms_c17
    IntelliSense Mode: windows-msvc-x64
Total Memory Usage: 169 MB

------- Workspace parsing diagnostics -------
Number of files discovered (not excluded): 666
Number of files parsed: 177
bobbrow commented 7 months ago

clang also complains that this has undefined behavior. It's possible that this is a language extension for IAR, but IntelliSense doesn't support extensions. This might be one of those cases where we'd recommend against using this pattern if possible given the "undefined behavior" warnings in 2 out of 3 major compilers.

stb-fr commented 7 months ago

To be sure that no extension has an impact, I have created a new profile, just with the extension C/C++. And I have the same bug.

sean-mcmanus commented 7 months ago

@stb-fr Bob was referring to an extension to the C++ language implemented by your IAR compiler, but the omission of a warning when undefined behavior is invoked doesn't require a language extension.