microsoft / microsoft/vscode-cpptools

#ifdef and #if defined not parsed correctly

Open
#12,867 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Language Service Visual Studio Code
Dominant language
TypeScript
Stars
6.2k
Forks
1.7k
Avg merge
14h 46m
Merged PRs (30d)
61

Description

Environment
  • OS and Version: Windows 11
  • VS Code Version: 1.94.2
  • C/C++ Extension Version: 1.22.9
  • If using SSH remote, specify OS of remote machine: N/A
Bug Summary and Steps to Reproduce

Bug Summary:
The below screenshots show the issue. The #ifdef section is properly being greyed out, but the parenthesis and curly brackets are not being paired properly. I've reproduced this on Windows 11 with the MSVC compiler, as well as on WSL with gcc. It appears to be OS/compiler agnostic. This seems similar to #12326, but not an exact match. Note that the behavion is the same whether #ifdef or #if defined is used.

Image

Image

Steps to reproduce:

Open the following code in VSCode with the default c_cpp_properties.json:

void foo( void ) {
if( 1
#ifdef NOT_DEFINED_SYMBOL
    )
    {
#else
    )
    {
#endif
    }
}

Expected behavior:

Configuration and Logs

c_cpp_properties.json:

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "10.0.18362.0",
            "compilerPath": "cl.exe",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "windows-msvc-x64"
        }
    ],
    "version": 4
}

C/C++: Log Diagnostics

-------- Diagnostics - 10/21/2024, 11:28:00 AM
Version: 1.22.9
Current Configuration:
{
    "name": "Win32",
    "includePath": [
        "c:/Users/mammen/Documents/Code/c_cpp_vscode_bug_repro/**"
    ],
    "defines": [
        "_DEBUG",
        "UNICODE",
        "_UNICODE"
    ],
    "windowsSdkVersion": "10.0.18362.0",
    "compilerPath": "cl.exe",
    "cStandard": "c17",
    "cppStandard": "c++17",
    "intelliSenseMode": "windows-msvc-x64",
    "compilerPathIsExplicit": true,
    "cStandardIsExplicit": true,
    "cppStandardIsExplicit": true,
    "intelliSenseModeIsExplicit": true,
    "compilerPathInCppPropertiesJson": "cl.exe",
    "mergeConfigurations": false,
    "browse": {
        "path": [
            "c:/Users/mammen/Documents/Code/c_cpp_vscode_bug_repro/**",
            "${workspaceFolder}"
        ],
        "limitSymbolsToIncludedHeaders": true
    }
}
Modified Settings:
{
    "C_Cpp.errorSquiggles": "enabled",
    "C_Cpp.vcFormat.indent.braces": true,
    "C_Cpp.vcFormat.indent.multiLineRelativeTo": "outermostParenthesis",
    "C_Cpp.vcFormat.indent.preserveWithinParentheses": true,
    "C_Cpp.vcFormat.indent.caseLabels": true,
    "C_Cpp.vcFormat.indent.caseContentsWhenBlock": true,
    "C_Cpp.vcFormat.indent.gotoLabels": "leftmostColumn",
    "C_Cpp.vcFormat.newLine.beforeOpenBrace.type": "newLine",
    "C_Cpp.vcFormat.newLine.beforeOpenBrace.function": "newLine",
    "C_Cpp.vcFormat.newLine.beforeOpenBrace.block": "newLine",
    "C_Cpp.vcFormat.newLine.scopeBracesOnSeparateLines": true,
    "C_Cpp.vcFormat.space.withinParameterListParentheses": true,
    "C_Cpp.vcFormat.space.afterKeywordsInControlFlowStatements": false,
    "C_Cpp.vcFormat.space.withinControlFlowStatementParentheses": true,
    "C_Cpp.vcFormat.space.withinExpressionParentheses": true,
    "C_Cpp.vcFormat.space.beforeBlockOpenBrace": false,
    "C_Cpp.vcFormat.space.withinSquareBrackets": true,
    "C_Cpp.vcFormat.space.withinLambdaBrackets": true,
    "C_Cpp.vcFormat.space.pointerReferenceAlignment": "center",
    "C_Cpp.vcFormat.wrap.preserveBlocks": "never",
    "C_Cpp.clang_format_style": "Visual Studio",
    "C_Cpp.formatting": "vcFormat",
    "C_Cpp.commentContinuationPatterns": [
        {
            "begin": "/*",
            "continue": "*"
        }
    ],
    "C_Cpp.codeAnalysis.runAutomatically": false,
    "C_Cpp.vcpkg.enabled": false,
    "C_Cpp.loggingLevel": "Debug",
    "C_Cpp.experimentalFeatures": "enabled"
}
Additional Tracked Settings:
{
    "editorTabSize": 4,
    "editorInsertSpaces": true,
    "editorAutoClosingBrackets": "languageDefined",
    "filesEncoding": "utf8",
    "filesAssociations": {
        "*.h": "c",
        "*.inc": "asm-collection",
        "*.sct": "linkerscript",
        "*.men": "cmm",
        "*.S": "arm",
        "*.s": "asm-collection"
    },
    "filesExclude": {
        "**/.git": true,
        "**/.svn": true,
        "**/.hg": true,
        "**/CVS": true,
        "**/.DS_Store": true,
        "**/Thumbs.db": true
    },
    "filesAutoSaveAfterDelay": false,
    "editorInlayHintsEnabled": true,
    "editorParameterHintsEnabled": true,
    "searchExclude": {
        "**/node_modules": true,
        "**/bower_components": true,
        "**/*.code-search": true
    },
    "workbenchSettingsEditor": "ui"
}
cpptools version (native): 1.22.8.0
Current database path: C:\USERS\MAMMEN\APPDATA\LOCAL\MICROSOFT\VSCODE-CPPTOOLS\4E74F6FAD2317DE57C3390195BD9E427\.BROWSE.VC.DB
Translation Unit Mappings:
[ C:\Users\mammen\Documents\Code\c_cpp_vscode_bug_repro\test.c - source TU]:
Translation Unit Configurations:
[ C:\Users\mammen\Documents\Code\c_cpp_vscode_bug_repro\test.c ]
    Process ID: 40028
    Memory Usage: 50 MB
    Compiler Path: C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.29.30133\bin\Hostx64\x64\cl.exe
    Includes:
    System Includes:
        C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.29.30133\include
        C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.29.30133\atlmfc\include
        C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\um
        C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\ucrt
        C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\shared
        C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\winrt
        C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\cppwinrt
    Defines:
        _DEBUG
        UNICODE
        _UNICODE
    Standard Version: ms_c17
    IntelliSense Mode: windows-msvc-x64
Total Memory Usage: 50 MB

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

Language server logging:

loggingLevel: Debug
LSP: (received) cpptools/initialize (id: 1)
LSP: (invoked) cpptools/initialize (id: 1)
cpptools version (TypeScript): 1.22.9
cpptools version (native): 1.22.8.0
Autocomplete is enabled.
Error squiggles are enabled.
Hover is enabled.
IntelliSense Engine = default.
LSP: Sending response (id: 1)
LSP: Message ignored due to no registered handler: $/setTrace
LSP: Message ignored due to no registered handler: $/setTrace
LSP: Message ignored due to no registered handler: $/setTrace
LSP: (received) cpptools/queryCompilerDefaults (id: 2)
LSP: (invoked) cpptools/queryCompilerDefaults (id: 2)
Compiler info database not connected - skipping load.
Compiler info database not connected - skipping load.
LSP: Sending response (id: 2)
LSP: (received) cpptools/queryCompilerDefaults (id: 3)
LSP: (invoked) cpptools/queryCompilerDefaults (id: 3)
LSP: (received) cpptools/didChangeCppProperties (id: 4)
LSP: (received - deferred) textDocument/didOpen: file:///c%3A/Users/mammen/Documents/Code/c_cpp_vscode_bug_repro/test.c
LSP: (received - deferred) cpptools/didChangeVisibleTextEditors
LSP: (received - deferred) cpptools/didChangeVisibleTextEditors
LSP: (received - deferred) cpptools/didChangeVisibleTextEditors
Compiler info database not connected - skipping load.
Compiler info database not connected - skipping load.
LSP: Sending response (id: 3)
LSP: (invoked) cpptools/didChangeCppProperties (id: 4)
Code browsing service initialized
Populating file name cache...
Attempting to get defaults from C compiler in "compilerPath" property: 'cl.exe'
Attempting to get defaults from C++ compiler in "compilerPath" property: 'cl.exe'
  Folder: C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO\2019\PROFESSIONAL\VC\TOOLS\MSVC\14.29.30133\ATLMFC\INCLUDE\* will be indexed
  Folder: C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO\2019\PROFESSIONAL\VC\TOOLS\MSVC\14.29.30133\INCLUDE\* will be indexed
  Folder: C:\PROGRAM FILES (X86)\WINDOWS KITS\10\INCLUDE\10.0.18362.0\CPPWINRT\ will be indexed
  Folder: C:\PROGRAM FILES (X86)\WINDOWS KITS\10\INCLUDE\10.0.18362.0\SHARED\ will be indexed
  Folder: C:\PROGRAM FILES (X86)\WINDOWS KITS\10\INCLUDE\10.0.18362.0\UCRT\ will be indexed
  Folder: C:\PROGRAM FILES (X86)\WINDOWS KITS\10\INCLUDE\10.0.18362.0\UM\ will be indexed
  Folder: C:\PROGRAM FILES (X86)\WINDOWS KITS\10\INCLUDE\10.0.18362.0\WINRT\ will be indexed
  Folder: C:\USERS\MAMMEN\DOCUMENTS\CODE\C_CPP_VSCODE_BUG_REPRO\ will be indexed
LSP: (queued) textDocument/didOpen: file:///c%3A/Users/mammen/Documents/Code/c_cpp_vscode_bug_repro/test.c
LSP: (queued) cpptools/didChangeVisibleTextEditors
LSP: (queued) cpptools/didChangeVisibleTextEditors
LSP: (queued) cpptools/didChangeVisibleTextEditors
LSP: Sending response (id: 4)
LSP: (invoked) textDocument/didOpen: file:///c%3A/Users/mammen/Documents/Code/c_cpp_vscode_bug_repro/test.c
Discovering files...
LSP: (invoked) cpptools/didChangeVisibleTextEditors
Intellisense update pending for: file:///c%3A/Users/mammen/Documents/Code/c_cpp_vscode_bug_repro/test.c
  Processing folder (non-recursive): C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO\2019\PROFESSIONAL\VC\TOOLS\MSVC\14.29.30133\INCLUDE
  Processing folder (recursive): C:\PROGRAM FILES (X86)\WINDOWS KITS\10\INCLUDE\10.0.18362.0\CPPWINRT\
  Processing folder (non-recursive): C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO\2019\PROFESSIONAL\VC\TOOLS\MSVC\14.29.30133\ATLMFC\INCLUDE
  Processing folder (recursive): C:\PROGRAM FILES (X86)\WINDOWS KITS\10\INCLUDE\10.0.18362.0\SHARED\
  Processing folder (recursive): C:\PROGRAM FILES (X86)\WINDOWS KITS\10\INCLUDE\10.0.18362.0\UCRT\
  Processing folder (recursive): C:\PROGRAM FILES (X86)\WINDOWS KITS\10\INCLUDE\10.0.18362.0\UM\
  Processing folder (recursive): C:\PROGRAM FILES (X86)\WINDOWS KITS\10\INCLUDE\10.0.18362.0\WINRT\
  Processing folder (recursive): C:\USERS\MAMMEN\DOCUMENTS\CODE\C_CPP_VSCODE_BUG_REPRO\
LSP: (received) cpptools/didChangeSettings
LSP: (invoked) cpptools/didChangeSettings
Autocomplete is enabled.
Error squiggles are enabled.
Hover is enabled.
IntelliSense Engine = default.
Enhanced Colorization is enabled.
Done populating filename cache. Elapsed time: 147 ms
  Discovering files: 5106 file(s) processed
  0 file(s) removed from database
Done discovering files.
Populating include completion cache.
Parsing remaining files...
  Parsing: 0 files(s) processed
Done parsing remaining files.
LSP: (received) cpptools/didChangeSettings
LSP: (invoked) cpptools/didChangeSettings
Autocomplete is enabled.
Error squiggles are enabled.
Hover is enabled.
IntelliSense Engine = default.
Enhanced Colorization is enabled.
LSP: (received) cpptools/didChangeSettings
LSP: (invoked) cpptools/didChangeSettings
Autocomplete is enabled.
Error squiggles are enabled.
Hover is enabled.
IntelliSense Engine = default.
Enhanced Colorization is enabled.
LSP: (received) cpptools/getCodeActions: file:///c%3A/Users/mammen/Documents/Code/c_cpp_vscode_bug_repro/test.c (id: 5)
LSP: (invoked) cpptools/getCodeActions: file:///c%3A/Users/mammen/Documents/Code/c_cpp_vscode_bug_repro/test.c (id: 5)
LSP: Sending response (id: 5)
LSP: Message ignored due to no registered handler: $/setTrace
LSP: (received) cpptools/didChangeSettings
LSP: (invoked) cpptools/didChangeSettings
Autocomplete is enabled.
Error squiggles are enabled.
Hover is enabled.
IntelliSense Engine = default.
Enhanced Colorization is enabled.
LSP: (received) cpptools/getDocumentSymbols: file:///c%3A/Users/mammen/Documents/Code/c_cpp_vscode_bug_repro/test.c (id: 6)
LSP: (invoked) cpptools/getDocumentSymbols: file:///c%3A/Users/mammen/Documents/Code/c_cpp_vscode_bug_repro/test.c (id: 6)
LSP: $/cancelRequest (<unknown/completed>, id: 5)
LSP: Sending response (id: 6)
LSP: (received) cpptools/getCodeActions: file:///c%3A/Users/mammen/Documents/Code/c_cpp_vscode_bug_repro/test.c (id: 7)
LSP: (invoked) cpptools/getCodeActions: file:///c%3A/Users/mammen/Documents/Code/c_cpp_vscode_bug_repro/test.c (id: 7)
LSP: Sending response (id: 7)
LSP: (received) cpptools/getFoldingRanges: file:///c%3A/Users/mammen/Documents/Code/c_cpp_vscode_bug_repro/test.c (id: 8)
LSP: (invoked) cpptools/getFoldingRanges: file:///c%3A/Users/mammen/Documents/Code/c_cpp_vscode_bug_repro/test.c (id: 8)
LSP: Sending response (id: 8)
LSP: (received) cpptools/getDocumentSymbols: file:///c%3A/Users/mammen/Documents/Code/c_cpp_vscode_bug_repro/test.c (id: 9)
LSP: (invoked) cpptools/getDocumentSymbols: file:///c%3A/Users/mammen/Documents/Code/c_cpp_vscode_bug_repro/test.c (id: 9)
LSP: Sending response (id: 9)
IntelliSense update scheduled and TU acquisition started for: file:///c%3A/Users/mammen/Documents/Code/c_cpp_vscode_bug_repro/test.c
Resolving recursive includes...
Done resolving recursive includes.
sending compilation args for C:\Users\mammen\Documents\Code\c_cpp_vscode_bug_repro\test.c
  system include: C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO\2019\PROFESSIONAL\VC\TOOLS\MSVC\14.29.30133\INCLUDE
  system include: C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO\2019\PROFESSIONAL\VC\TOOLS\MSVC\14.29.30133\ATLMFC\INCLUDE
  system include: C:\PROGRAM FILES (X86)\WINDOWS KITS\10\INCLUDE\10.0.18362.0\UM
  system include: C:\PROGRAM FILES (X86)\WINDOWS KITS\10\INCLUDE\10.0.18362.0\UCRT
  system include: C:\PROGRAM FILES (X86)\WINDOWS KITS\10\INCLUDE\10.0.18362.0\SHARED
  system include: C:\PROGRAM FILES (X86)\WINDOWS KITS\10\INCLUDE\10.0.18362.0\WINRT
  system include: C:\PROGRAM FILES (X86)\WINDOWS KITS\10\INCLUDE\10.0.18362.0\CPPWINRT
  define: _DEBUG
  define: UNICODE
  define: _UNICODE
  stdver: ms_c17
  intelliSenseMode: windows-msvc-x64
Update IntelliSense time (sec): 0.847
LSP: (received) cpptools/getFoldingRanges: file:///c%3A/Users/mammen/Documents/Code/c_cpp_vscode_bug_repro/test.c (id: 10)
LSP: (invoked) cpptools/getFoldingRanges: file:///c%3A/Users/mammen/Documents/Code/c_cpp_vscode_bug_repro/test.c (id: 10)
LSP: Sending response (id: 10)
LSP: (received) cpptools/getDocumentSymbols: file:///c%3A/Users/mammen/Documents/Code/c_cpp_vscode_bug_repro/test.c (id: 11)
LSP: (invoked) cpptools/getDocumentSymbols: file:///c%3A/Users/mammen/Documents/Code/c_cpp_vscode_bug_repro/test.c (id: 11)
LSP: Sending response (id: 11)
LSP: $/cancelRequest (<unknown/completed>, id: 10)
LSP: (received) cpptools/getFoldingRanges: file:///c%3A/Users/mammen/Documents/Code/c_cpp_vscode_bug_repro/test.c (id: 12)
LSP: (invoked) cpptools/getFoldingRanges: file:///c%3A/Users/mammen/Documents/Code/c_cpp_vscode_bug_repro/test.c (id: 12)
LSP: Sending response (id: 12)
Other Extensions

No response

Additional context

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by opening the reproduced case in test.c with the supplied c_cpp_properties.json and verify the bracket-pairing behavior for both #ifdef and #if defined. Trace the C/C++ extension's preprocessing or parsing entry point that handles these directives; done means parentheses and braces are paired correctly in the shown conditional code.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, typescript
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.