microsoft / vscode-cpptools

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

vcFormat incorrectly formatting templates with lambda parameters #11112

Open aandrejevas opened 1 year ago

aandrejevas commented 1 year ago

Environment

Bug Summary and Steps to Reproduce

Settings: "C_Cpp.default.cppStandard": "c++23", "C_Cpp.default.cStandard": "c17", "C_Cpp.default.intelliSenseMode": "windows-gcc-x64", "C_Cpp.default.compilerPath": "C:\msys64\ucrt64\bin\g++.exe", "C_Cpp.autoAddFileAssociations": false, "C_Cpp.suggestSnippets": false, "C_Cpp.formatting": "vcFormat", "C_Cpp.vcFormat.wrap.preserveBlocks": "allOneLineScopes", "C_Cpp.vcFormat.indent.gotoLabels": "oneLeft", "C_Cpp.vcFormat.indent.caseLabels": true, "C_Cpp.vcFormat.newLine.closeBraceSameLine.emptyFunction": true, "C_Cpp.vcFormat.newLine.closeBraceSameLine.emptyType": true, "C_Cpp.vcFormat.indent.multiLineRelativeTo": "statementBegin", "C_Cpp.vcFormat.newLine.beforeElse": false, "C_Cpp.vcFormat.newLine.beforeCatch": false, "C_Cpp.vcFormat.space.withinInitializerListBraces": false, "C_Cpp.vcFormat.indent.preserveWithinParentheses": true, "C_Cpp.vcFormat.space.pointerReferenceAlignment": "right", "C_Cpp.vcpkg.enabled": false, "C_Cpp.default.defines": [], "C_Cpp.loggingLevel": "Information", "C_Cpp.simplifyStructuredComments": false, "C_Cpp.errorSquiggles": "disabled", "C_Cpp.codeAnalysis.runAutomatically": false, "C_Cpp.dimInactiveRegions": false, "C_Cpp.intelliSenseEngine": "Tag Parser", "C_Cpp.vcFormat.indent.lambdaBracesWhenParameter": false

Expected behavior:

using lambda1 = std::type_identity_t<[] {}>;

template<class T>
using lambda2 = []<size_t> -> T { return {}; };

After formatting:

using lambda1 = std::type_identity_t < [] {} > ;

template<class T>
using lambda2 = []<size_t>->T { return {}; };

Configuration and Logs

-------- Diagnostics - 6/21/2023, 2:06:19 AM
Version: 1.16.1
Current Configuration:
{
    "name": "Win32",
    "includePath": [
        "c:\\Users\\Aleksandras\\Desktop\\programming\\Solutions\\QAP_BA/**"
    ],
    "windowsSdkVersion": "10.0.19041.0",
    "defines": [],
    "cStandard": "c17",
    "cppStandard": "c++23",
    "intelliSenseMode": "windows-gcc-x64",
    "intelliSenseModeIsExplicit": true,
    "cStandardIsExplicit": true,
    "cppStandardIsExplicit": true,
    "mergeConfigurations": false,
    "compilerPath": "C:\\msys64\\ucrt64\\bin\\g++.exe",
    "compilerPathIsExplicit": true,
    "browse": {
        "path": [
            "${workspaceFolder}/**"
        ],
        "limitSymbolsToIncludedHeaders": true
    }
}
cpptools version (native): 1.16.1.0
No active translation units.

------- Workspace parsing diagnostics -------
Number of files discovered (not excluded): 35090
Number of files parsed: 6811

Other Extensions

No response

Additional context

No response

michelleangela commented 1 year ago

@aandrejevas Could you provide logs from the language server (see https://code.visualstudio.com/docs/cpp/enable-logging-cpp#_enable-logging-for-the-language-server) while reproducing the issue? Also, could you provide an example that compiles and an example of the code before it is formatted?

aandrejevas commented 1 year ago

@michelleangela Sorry that I previously provided code that does not compile. Code before formatting (expected behavior):

#include <cstddef>
#include <type_traits>
#include <iostream>
using lambda1 = std::bool_constant<[] -> bool { return true; }()>;
template<class T>
using lambda2 = decltype([]<std::size_t> -> T { return {}; });
int main() { std::cout << "Hello\n"; }

Code after formatting:

#include <cstddef>
#include <type_traits>
#include <iostream>
using lambda1 = std::bool_constant < [] -> bool { return true; }() > ;
template<class T>
using lambda2 = decltype([]<std::size_t>->T { return {}; });
int main() { std::cout << "Hello\n"; }

I can compile the provided code: image

aandrejevas commented 1 year ago

Here is the log from the language server:

loggingLevel: Debug
cpptools version (TypeScript): 1.16.3
cpptools version (native): 1.16.3.0
Autocomplete is enabled.
Error squiggles are disabled.
Hover is enabled.
IntelliSense Engine = tag parser.
LSP: cpptools/queryCompilerDefaults (id: 1)
LSP: cpptools/queryCompilerDefaults (id: 2)
Querying compiler for default C++ language standard using command line: C:\msys64\ucrt64\bin\gcc.exe -x c++ -E -dM nul
Detected language standard version: gnu++17
Querying compiler's default target using command line: "C:\msys64\ucrt64\bin\gcc.exe" -dumpmachine
Compiler returned default target value: x86_64-w64-mingw32
Compiler query command line: C:\msys64\ucrt64\bin\gcc.exe -std=c17 -m64 -Wp,-v -E -dM -x c nul
Attempting to get defaults from C compiler in "compilerPath" property: 'C:\msys64\ucrt64\bin\gcc.exe'
Compiler query command line: C:\msys64\ucrt64\bin\gcc.exe -std=gnu++17 -m64 -Wp,-v -E -dM -x c++ nul
Attempting to get defaults from C++ compiler in "compilerPath" property: 'C:\msys64\ucrt64\bin\gcc.exe'
LSP: cpptools/didChangeCppProperties (id: 3)
LSP: textDocument/didOpen: file:///c%3A/Users/aandr/Desktop/Solutions/Test/main.cpp
LSP: cpptools/getInlayHints: file:///c%3A/Users/aandr/Desktop/Solutions/Test/main.cpp (id: 4)
LSP: cpptools/getDocumentSymbols: file:///c%3A/Users/aandr/Desktop/Solutions/Test/main.cpp (id: 5)
LSP: cpptools/getCodeActions: file:///c%3A/Users/aandr/Desktop/Solutions/Test/main.cpp (id: 6)
LSP: cpptools/getFoldingRanges: file:///c%3A/Users/aandr/Desktop/Solutions/Test/main.cpp (id: 7)
LSP: cpptools/textEditorSelectionChange
LSP: cpptools/activeDocumentChange: file:///c%3A/Users/aandr/Desktop/Solutions/Test/main.cpp
Code browsing service initialized
Querying compiler for default C++ language standard using command line: C:\msys64\ucrt64\bin\g++.exe -x c++ -E -dM nul
Detected language standard version: gnu++17
Querying compiler's default target using command line: "C:\msys64\ucrt64\bin\g++.exe" -dumpmachine
Compiler returned default target value: x86_64-w64-mingw32
Compiler query command line: C:\msys64\ucrt64\bin\g++.exe -std=c++23 -m64 -Wp,-v -E -dM -x c++ nul
Attempting to get defaults from C++ compiler in "compilerPath" property: 'C:\msys64\ucrt64\bin\g++.exe'
Compiler query command line: C:\msys64\ucrt64\bin\g++.exe -std=c17 -m64 -Wp,-v -E -dM -x c nul
Attempting to get defaults from C compiler in "compilerPath" property: 'C:\msys64\ucrt64\bin\g++.exe'
  Folder: C:/MSYS64/UCRT64/INCLUDE/ will be indexed
  Folder: C:/MSYS64/UCRT64/LIB/GCC/X86_64-W64-MINGW32/13.1.0/INCLUDE/ will be indexed
  Folder: C:/MSYS64/UCRT64/LIB/GCC/X86_64-W64-MINGW32/13.1.0/INCLUDE-FIXED/ will be indexed
  Folder: C:/USERS/AANDR/DESKTOP/SOLUTIONS/TEST/ will be indexed
Discovering files...
  Processing folder (recursive): C:/MSYS64/UCRT64/INCLUDE/
  Processing folder (recursive): C:/MSYS64/UCRT64/LIB/GCC/X86_64-W64-MINGW32/13.1.0/INCLUDE/
  Processing folder (recursive): C:/MSYS64/UCRT64/LIB/GCC/X86_64-W64-MINGW32/13.1.0/INCLUDE-FIXED/
  Processing folder (recursive): C:/USERS/AANDR/DESKTOP/SOLUTIONS/TEST/
  Discovering files: 2948 file(s) processed
  0 file(s) removed from database
Done discovering files.
Parsing open files...
Populating include completion cache.
Parsing remaining files...
  Parsing: 0 files(s) processed
Done parsing remaining files.
Done parsing open files.
LSP: cpptools/formatDocument: file:///c%3A/Users/aandr/Desktop/Solutions/Test/main.cpp (id: 8)
Formatting document: file:///C%3A/Users/aandr/Desktop/Solutions/Test/main.cpp
Formatting Engine: vcFormat
LSP: textDocument/didChange: file:///c%3A/Users/aandr/Desktop/Solutions/Test/main.cpp
LSP: $/cancelRequest (cpptools/getInlayHints, id: 4)
LSP: cpptools/getInlayHints: file:///c%3A/Users/aandr/Desktop/Solutions/Test/main.cpp (id: 9)
LSP: Request canceled by server (<unknown/completed>, code: -32802, id: 4)
LSP: cpptools/getFoldingRanges: file:///c%3A/Users/aandr/Desktop/Solutions/Test/main.cpp (id: 10)
idle loop: reparsing the active document
LSP: cpptools/getDocumentSymbols: file:///c%3A/Users/aandr/Desktop/Solutions/Test/main.cpp (id: 11)
  tag parsing file: C:\Users\aandr\Desktop\Solutions\Test\main.cpp
michelleangela commented 1 year ago

@aandrejevas Thank you for the repro. I was able to reproduce the bug locally.

Note: the two settings that are causing the formatting issue are:

It appears that vcFormat is applying incorrect formatting rules for those cases.

michelleangela commented 1 year ago

The vcFormat code case is shared with Visual Studio. I created internal Visual Studio bugs 1846085 and 1846065.