microsoft / vscode-cpptools

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

`std::source_location::function_name` gets inconsistent results with GCC/Clang #12781

Open guyutongxue opened 1 week ago

guyutongxue commented 1 week ago

Environment

Bug Summary and Steps to Reproduce

Bug Summary:

In GCC/Clang, C++20 std::source_location::function_name expands to __PRETTY_FUNCTION__, that includes parameter type, template arguments and return type etc. But C/C++ extension just contains function name, i.e. __func__.

Steps to reproduce:

  1. Configured to a C++20-ready GCC/Clang compiler;
  2. Enter following code
#include <source_location>
#include <string_view>

void foo(int) {
  constexpr std::string_view function_name = std::source_location::current().function_name();
  static_assert(function_name == std::string_view{__PRETTY_FUNCTION__});
  static_assert(function_name != std::string_view{__func__});
}

截图 2024-09-29 14-49-24

Expected behavior:

All above static_asserts should succeeded instead of failed. Godbolt

Configuration and Logs

c_cpp_properties.json:

{
  "version": 4,
  "configurations": [
    {
      "name": "Linux",
      "compilerPath": "/home/linuxbrew/.linuxbrew/bin/g++-14",
      "cppStandard": "c++23"
    }
  ]
}

C/C++ diagnostic logs:

-------- Diagnostics - 2024/9/29 14:58:23
Version: 1.22.6
Current Configuration:
{
    "name": "Linux",
    "compilerPath": "/home/linuxbrew/.linuxbrew/bin/g++-14",
    "cppStandard": "c++23",
    "compilerPathIsExplicit": true,
    "cStandardIsExplicit": true,
    "cppStandardIsExplicit": true,
    "intelliSenseModeIsExplicit": false,
    "compilerPathInCppPropertiesJson": "/home/linuxbrew/.linuxbrew/bin/g++-14",
    "cStandard": "c17",
    "mergeConfigurations": false,
    "browse": {
        "path": [
            "${workspaceFolder}"
        ],
        "limitSymbolsToIncludedHeaders": true
    }
}
Modified Settings:
{
    "C_Cpp.default.compilerPath": "/home/linuxbrew/.linuxbrew/bin/g++-14",
    "C_Cpp.default.cStandard": "c17",
    "C_Cpp.default.cppStandard": "c++23",
    "C_Cpp.clang_format_fallbackStyle": "{ BasedOnStyle: Google, AccessModifierOffset: -2, IndentWidth: 2, TabWidth: 2, ColumnLimit: 80, PointerAlignment: Left, AllowShortFunctionsOnASingleLine: Empty, AllowShortCaseLabelsOnASingleLine: true, AllowShortIfStatementsOnASingleLine: WithoutElse, AllowAllArgumentsOnNextLine: true }"
}
Additional Tracked Settings:
{
    "editorTabSize": 2,
    "editorInsertSpaces": true,
    "editorAutoClosingBrackets": "languageDefined",
    "filesEncoding": "utf8",
    "filesAssociations": {
        "*.bf": "brainfuck",
        "*.itexi": "texinfo",
        "*.json5": "jsonc",
        "string_view": "cpp"
    },
    "filesExclude": {
        "**/.git": true,
        "**/.svn": true,
        "**/.hg": true,
        "**/CVS": true,
        "**/.DS_Store": true,
        "**/Thumbs.db": true,
        "**/*.dSYM": true
    },
    "filesAutoSaveAfterDelay": false,
    "editorInlayHintsEnabled": true,
    "editorParameterHintsEnabled": true,
    "searchExclude": {
        "**/node_modules": true,
        "**/bower_components": true,
        "**/*.code-search": true
    },
    "workbenchSettingsEditor": "ui"
}
cpptools version (native): 1.22.6.0
Current database path: /home/guyutongxue/.cache/vscode-cpptools/2555eb2c5ee6a91cb654bde2b78a9981/.browse.VC.db
Translation Unit Mappings:
[ /home/guyutongxue/temp/cpp/source_location.cpp - source TU]:
Translation Unit Configurations:
[ /home/guyutongxue/temp/cpp/source_location.cpp ]
    Process ID: 1156233
    Memory Usage: 57 MB
    Compiler Path: /home/linuxbrew/.linuxbrew/bin/g++-14
    Includes:
    System Includes:
        /home/linuxbrew/.linuxbrew/Cellar/gcc/14.2.0/include/c++/14
        /home/linuxbrew/.linuxbrew/Cellar/gcc/14.2.0/include/c++/14/x86_64-pc-linux-gnu
        /home/linuxbrew/.linuxbrew/Cellar/gcc/14.2.0/include/c++/14/backward
        /home/linuxbrew/.linuxbrew/Cellar/gcc/14.2.0/lib/gcc/current/gcc/x86_64-pc-linux-gnu/14/include
        /home/linuxbrew/.linuxbrew/Cellar/gcc/14.2.0/lib/gcc/current/gcc/x86_64-pc-linux-gnu/14/include-fixed/x86_64-linux-gnu
        /home/linuxbrew/.linuxbrew/Cellar/gcc/14.2.0/lib/gcc/current/gcc/x86_64-pc-linux-gnu/14/include-fixed
        /home/linuxbrew/.linuxbrew/Cellar/gcc/14.2.0/include
        /home/linuxbrew/.linuxbrew/include
        /usr/include/x86_64-linux-gnu
        /usr/include
    Standard Version: c++23
    IntelliSense Mode: linux-gcc-x64
    Other Flags:
        --g++
        --gnu_version=140200
Total Memory Usage: 57 MB

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

Language Server Log

Other Extensions

N/A

Additional context

No response

sean-mcmanus commented 1 day ago

__PRETTY_FUNCTION__ is a g++/clang++ language extension and our compiler doesn't support that currently. A similar issue is https://github.com/microsoft/vscode-cpptools/issues/9968 .

sean-mcmanus commented 1 day ago

Actually, it looks like our compiler does have support for this, so we need to investigate why it isn't working as expected.