microsoft / vscode-cpptools

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

Incorrect errors when __attribute__ is used on enum values in gcc mode #4885

Open tristan957 opened 4 years ago

tristan957 commented 4 years ago

Type: LanguageService

Describe the bug

There is an identifier exported from GLib, G_PARAM_STATIC_STRINGS (a macro), that is marked as undefined (has red squiggles) even though I can Ctrl+click it to show the definition. Seeing the definition shows no errors. The file that the macro is defined also reports an unusual error though, where an ending } is expected. Not sure if this is throwing off the language server, but something is off.

To Reproduce

repository in question: https://git.sr.ht/~tristan957/harvest-almanac (also mirrored on my GitHub if more convenient) example of usage: https://git.sr.ht/~tristan957/harvest-almanac/tree/d5ebeb13fb59b3c99e3c43c290b7b7e7aed51020/harvest-glib/harvest-project.c#L334

c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/harvest-glib",
                "/usr/include/gtk-3.0",
                "/usr/include/glib-2.0",
                "/usr/lib64/glib-2.0/include",
                "/usr/include/pango-1.0",
                "/usr/include/cairo",
                "/usr/include/gdk-pixbuf-2.0",
                "/usr/include/atk-1.0",
                "/usr/include/libhandy-0.0",
                "/usr/include/json-glib-1.0",
                "/usr/include/libsoup-2.4",
                "/usr/include/libsecret-1",
                "/usr/include/harfbuzz",
                "/home/tristan957/.local/include",
                "${workspaceFolder}/build/harvest-glib"
            ],
            "defines": [
                "__HARVEST_HEADER_INTERNAL__"
            ],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "gcc-x64",
            "compileCommands": "${workspaceFolder}/build/compile_commands.json"
        }
    ],
    "version": 4
}
  1. Open the repo in VSCode
  2. Go to harvest-glib/harvest-project.c line 334, see error on line 335 (any .c file should show the same error fyi)
  3. Notice squiggles
  4. Ctrl+click (now viewing gparam.h) (https://gitlab.gnome.org/GNOME/glib/blob/master/gobject/gparam.h#L175)
  5. Notice error on line 159 of file (https://gitlab.gnome.org/GNOME/glib/blob/master/gobject/gparam.h#L159)
  6. Follow the definition of GLIB_DEPRECATED_ENUMERATOR_IN_2_26 to gversionmacros.h and then to gmacros.h
  7. See that GLIB_DEPRECATED_ENUMERATOR_IN_2_26 eventually gets defined as __attribute__((__deprecated__))

Expected behavior Not misreport a defined identifier.

tristan957 commented 4 years ago

Important to note, my program currently compiles as is. Instructions to build can be found in the CONTRIBUTING.md page if needed.

DeltaRazero commented 4 years ago

For me it appears that any identifier that is #include'ed is reported as undefined, even though that's not the case

sean-mcmanus commented 4 years ago

Your project has a lot of dependencies. Can you build the source file that has the IntelliSense error with the -E flag so that it's output to a preprocessed file?

Also, if you could run the C/C++: Log Diagnostics command with the reproing file active and check to see if the includePath/defines are as expected that could help diagnose if there's a configuration issue or an IntelliSense parsing bug.

tristan957 commented 4 years ago

output.txt

-------- Diagnostics - 1/27/2020, 9:51:12 PM
Version: 0.26.3
Current Configuration:
{
    "name": "Linux",
    "includePath": [
        "${workspaceFolder}/harvest-glib",
        "/usr/include/gtk-3.0",
        "/usr/include/glib-2.0",
        "/usr/lib64/glib-2.0/include",
        "/usr/include/pango-1.0",
        "/usr/include/cairo",
        "/usr/include/gdk-pixbuf-2.0",
        "/usr/include/atk-1.0",
        "/usr/include/libhandy-0.0",
        "/usr/include/json-glib-1.0",
        "/usr/include/libsoup-2.4",
        "/usr/include/libsecret-1",
        "/usr/include/harfbuzz",
        "/home/tristan957/.local/include",
        "${workspaceFolder}/build/harvest-glib"
    ],
    "defines": [
        "__HARVEST_HEADER_INTERNAL__"
    ],
    "compilerPath": "/usr/bin/gcc",
    "cStandard": "c11",
    "cppStandard": "c++17",
    "intelliSenseMode": "gcc-x64",
    "compileCommands": "${workspaceFolder}/build/compile_commands.json",
    "compilerArgs": [],
    "browse": {
        "path": [
            "${workspaceFolder}/harvest-glib",
            "/usr/include/gtk-3.0",
            "/usr/include/glib-2.0",
            "/usr/lib64/glib-2.0/include",
            "/usr/include/pango-1.0",
            "/usr/include/cairo",
            "/usr/include/gdk-pixbuf-2.0",
            "/usr/include/atk-1.0",
            "/usr/include/libhandy-0.0",
            "/usr/include/json-glib-1.0",
            "/usr/include/libsoup-2.4",
            "/usr/include/libsecret-1",
            "/usr/include/harfbuzz",
            "/home/tristan957/.local/include",
            "${workspaceFolder}/build/harvest-glib",
            "${workspaceFolder}"
        ],
        "limitSymbolsToIncludedHeaders": true
    }
}
Translation Unit Mappings:
[ /home/tristan957/Projects/harvest-almanac/harvest-glib/harvest-project.c ]:
    /home/tristan957/Projects/harvest-almanac/harvest-glib/harvest-project.c
Translation Unit Configurations:
[ /home/tristan957/Projects/harvest-almanac/harvest-glib/harvest-project.c ]:
    Process ID: 3698
    Memory Usage: 41 MB
    Compiler Path: /usr/bin/gcc
    Includes:
        /home/tristan957/Projects/harvest-almanac/build/harvest-glib/ffd5316@@harvest-glib@sha
        /home/tristan957/Projects/harvest-almanac/harvest-glib
        /usr/include/glib-2.0
        /usr/lib64/glib-2.0/include
        /usr/include/libmount
        /usr/include/blkid
        /usr/include/json-glib-1.0
        /usr/include/libsoup-2.4
        /usr/include/libxml2
        /home/tristan957/Projects/harvest-almanac/build/data
        /home/tristan957/Projects/harvest-almanac/build/harvest-almanac
        /home/tristan957/Projects/harvest-almanac/build/harvest-glib
        /usr/lib/gcc/x86_64-redhat-linux/9/include
        /usr/local/include
        /usr/include
    Defines:
        _FILE_OFFSET_BITS=64
        __HARVEST_COMPILATION__
        G_ENABLE_DEBUG
    Standard Version: c11
    IntelliSense Mode: gcc-x64
    Other Flags:
        --gcc
        --gnu_version=90201
Total Memory Usage: 41 MB
sean-mcmanus commented 4 years ago

Thanks a lot for reporting this and providing all the info. I've filed a bug with the VS C++ team at https://developercommunity.visualstudio.com/content/problem/899823/cc-intellisense-reports-incorrect-errors-when-attr.html . If you have a Microsoft account you can upvote that.

The cause is the usage of __attribute__ on enum values, which causes the following enum definitions to not be parsed correctly.

sean-mcmanus commented 4 years ago

It's also possible your repro may need https://developercommunity.visualstudio.com/content/problem/796872/cc-intellisense-in-gccclang-mode-shows-attribute-c.html (https://github.com/microsoft/vscode-cpptools/issues/4503) to be fixed as well, because I see the deprecated attribute taking an argument in one case.

tristan957 commented 4 years ago

@sean-mcmanus thanks. I will make sure to cast a vote for both issues. Issues aren't game breaking, so no problem at all.