1

In Visual Studio Code with the clang extension, when I try to format document, the formatting ignores the .clang-format file in the workspace (located 2 levels above the folder of the file I am trying to format).

The result of the formatting looks very different from the one I get if I run clang-format manually from the vscode terminal window.

The settings I use are (some irrelevant ones omitted for brevity):

{
    "[cpp]": {
        "diffEditor.codeLens": true,
        "editor.tabCompletion": "on",
        "editor.tabSize": 4
    },
    "clangd.arguments": [
        "--enable-config"
    ],
    "C_Cpp.clang_format_path": "/opt/homebrew/bin/clang-format",
    "C_Cpp.codeAnalysis.clangTidy.useBuildPath": true,
    "C_Cpp.default.browse.path": [
        "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1"
    ],
    "C_Cpp.default.compilerPath": "/opt/homebrew/opt/ccache/libexec/clang++",
    "C_Cpp.default.cppStandard": "c++20",
    "C_Cpp.default.cStandard": "c99",
    "C_Cpp.intelliSenseEngine": "Disabled",
    "C_Cpp.formatting": "clangFormat",
    "C_Cpp.loggingLevel": "Warning",
    "clangd.checkUpdates": true,
    "editor.defaultFormatter": "llvm-vs-code-extensions.vscode-clangd",
    "editor.tabCompletion": "on"
}

What am I missing?

Alex O
  • 1,429
  • 2
  • 13
  • 20
  • The one where you tell clang-format to prefer `file`. clangd doesn't play a role here at all. – sweenish Nov 04 '22 at 03:18
  • Do you have Microsoft's vscode-cpptools extension enabled as well? (I think that's what the settings that start with `C_cpp` pertain to.) If so, maybe the `C_Cpp.formatting` setting is interfering somehow? Is there a way to disable that? – HighCommander4 Nov 04 '22 at 04:32
  • Beyond that, what would be helpful for diagnosis is to share [clangd logs](https://clangd.llvm.org/troubleshooting#gathering-logs) from an editing session that includes a format, so we can verify that it's being done by clangd. clangd **should** be respecting the `.clang-format` file. – HighCommander4 Nov 04 '22 at 04:33
  • The logs did not show anything suspicious during the format, but there was a parsing error on startup (which I missed since I haven't restarted vscode for a while) – Alex O Nov 09 '22 at 22:09

1 Answers1

0

Apparently Apple Clang 14 does not support all the formatting options that LLVM Clang 14 does. Clangd failed to parse the .clang-format file and fell back to LLVM style.

Once I reconfigured the plugin to use its internal clangd rather than the Xcode one, the problem disappeared.

Alex O
  • 1,429
  • 2
  • 13
  • 20