microsoft / vscode-cpptools

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

Pragma packed not expanded correctly in structs #11195

Closed EduMenges closed 12 months ago

EduMenges commented 1 year ago

Environment

Bug Summary and Steps to Reproduce

Bug Summary: When applying a pragma in a struct definition, the extension gives the following error:

declaration of anonymous struct must be a definition C/C++(clang-diagnostic-error) declaration does not declare anything C/C++(clang-diagnostic-missing-declarations)

Steps to reproduce:

  1. Use the following macro to allow for the packed attribute to be compatible across GCC and MSVC
    #ifdef __GNUC__
    #if __cplusplus >= 201103L
    #define __begin_packed [[gnu::packed]]
    #else
    #define __begin_packed __attribute__((packed))
    #endif
    #define __end_packed
    #else // MSVC
    #define __begin_packed __pragma(pack(push, 1))
    #define __end_packed   __pragma(pack(pop))
    #endif
  2. Set up MSVC as the compiler
  3. Create a struct such as
    struct __begin_packed Packed {
    uint8_t a;
    uint32_t b;
    } __end_packed;
  4. See the already described error.

Expected behavior: The extension should apply the pragma correctly.

Configuration and Logs

c_cpp_properties.json: not needed
Log Diagnostics:
<spoiler>
-------- Diagnostics - 14/07/2023 20:30:55
Version: 1.16.3
Current Configuration:
{
    "name": "Win32",
    "includePath": [
        "c:\\Users\\br20279\\Downloads\\test_packed/**",
        "C:/vcpkg/installed/x64-windows/include",
        "C:/vcpkg/installed/x86-windows/include"
    ],
    "defines": [
        "_DEBUG",
        "UNICODE",
        "_UNICODE"
    ],
    "windowsSdkVersion": "10.0.18362.0",
    "compilerPath": "cl.exe",
    "compilerPathInCppPropertiesJson": "cl.exe",
    "cStandard": "c23",
    "cppStandard": "c++23",
    "intelliSenseMode": "windows-msvc-x64",
    "intelliSenseModeIsExplicit": true,
    "cStandardIsExplicit": true,
    "cppStandardIsExplicit": true,
    "mergeConfigurations": false,
    "compilerPathIsExplicit": false,
    "browse": {
        "path": [
            "${workspaceFolder}/**",
            "${vcpkgRoot}/x64-windows/include",
            "${vcpkgRoot}/x86-windows/include"
        ],
        "limitSymbolsToIncludedHeaders": true
    }
}
cpptools version (native): 1.16.3.0
Translation Unit Mappings:
[ C:\Users\br20279\Downloads\test_packed\packed.cpp ]:
    C:\Users\br20279\Downloads\test_packed\packed.cpp
Translation Unit Configurations:
[ C:\Users\br20279\Downloads\test_packed\packed.cpp ]:
    Process ID: 24628
    Memory Usage: 52 MB
    Compiler Path: C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Tools\MSVC\14.16.27023\bin\Hostx64\x64\cl.exe
    Includes:
        C:\vcpkg\installed\x64-windows\include
        C:\vcpkg\installed\x86-windows\include
        C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Tools\MSVC\14.16.27023\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_c++latest
    IntelliSense Mode: windows-msvc-x64
Total Memory Usage: 52 MB

------- Workspace parsing diagnostics -------
Number of files discovered (not excluded): 4762
</spoiler>
Language server logging:
<spoiler>
loggingLevel: Debug
loggingLevel foi alterado para: Debug
LSP: cpptools/textEditorSelectionChange
LSP: cpptools/activeDocumentChange: file:///c%3A/Users/br20279/Downloads/test_packed/packed.cpp
LSP: cpptools/getCodeActions: file:///c%3A/Users/br20279/Downloads/test_packed/packed.cpp (id: 211)
LSP: cpptools/textEditorSelectionChange
LSP: cpptools/getCodeActions: file:///c%3A/Users/br20279/Downloads/test_packed/packed.cpp (id: 212)
LSP: cpptools/getFoldingRanges: file:///c%3A/Users/br20279/Downloads/test_packed/packed.cpp (id: 213)
LSP: cpptools/textEditorSelectionChange
LSP: textDocument/didChange: file:///c%3A/Users/br20279/Downloads/test_packed/packed.cpp
LSP: cpptools/textEditorSelectionChange
LSP: cpptools/getCodeActions: file:///c%3A/Users/br20279/Downloads/test_packed/packed.cpp (id: 214)
LSP: cpptools/getFoldingRanges: file:///c%3A/Users/br20279/Downloads/test_packed/packed.cpp (id: 215)
LSP: cpptools/getSemanticTokens: file:///c%3A/Users/br20279/Downloads/test_packed/packed.cpp (id: 216)
LSP: cpptools/getDocumentSymbols: file:///c%3A/Users/br20279/Downloads/test_packed/packed.cpp (id: 217)
  arquivo de análise de marca: C:\Users\br20279\Downloads\test_packed\packed.cpp
    erro de análise de marca (isso pode ser ignorado, a menos que não seja possível encontrar símbolos):
    "C:\Users\br20279\Downloads\test_packed\packed.cpp", linha 24 (col. 1): erro: cadeia de caracteres entre aspas devem possuir pelo menos um caractere
LSP: cpptools/textEditorSelectionChange
LSP: cpptools/getCodeActions: file:///c%3A/Users/br20279/Downloads/test_packed/packed.cpp (id: 218)
LSP: cpptools/getInlayHints: file:///c%3A/Users/br20279/Downloads/test_packed/packed.cpp (id: 219)
LSP: textDocument/didChange: file:///c%3A/Users/br20279/Downloads/test_packed/packed.cpp
LSP: cpptools/textEditorSelectionChange
LSP: textDocument/didChange: file:///c%3A/Users/br20279/Downloads/test_packed/packed.cpp
LSP: cpptools/textEditorSelectionChange
LSP: cpptools/getFoldingRanges: file:///c%3A/Users/br20279/Downloads/test_packed/packed.cpp (id: 220)
LSP: textDocument/willSaveWaitUntil: file:///c%3A/Users/br20279/Downloads/test_packed/packed.cpp (id: 221)
willSaveWaitUntil: 0ms
LSP: textDocument/didSave: file:///c%3A/Users/br20279/Downloads/test_packed/packed.cpp
  arquivo de análise de marca: C:\Users\br20279\Downloads\test_packed\packed.cpp
c:\Users\br20279\.vscode\extensions\ms-vscode.cpptools-1.16.3-win32-x64/bin/../LLVM/bin/clang-tidy.exe
--header-filter=(C:/Users/br20279/Downloads/test_packed|C:\\Users\\br20279\\Downloads\\test_packed)
--export-fixes=C:\Users\br20279\AppData\Local\Temp\{2432956311735928688}\fixes17124.yaml
--quiet
--use-color=false
C:/Users/br20279/Downloads/test_packed/packed.cpp
--
-std=c++2b
-xc++
-Wno-pragma-pack
-Wno-pragma-once-outside-header
-D_DEBUG
-DUNICODE
-D_UNICODE
-IC:/vcpkg/installed/x64-windows/include
-IC:/vcpkg/installed/x86-windows/include
-isystemC:/Program Files (x86)/Microsoft Visual Studio/2017/BuildTools/VC/Tools/MSVC/14.16.27023/include
-isystemC:/Program Files (x86)/Windows Kits/10/Include/10.0.18362.0/um
-isystemC:/Program Files (x86)/Windows Kits/10/Include/10.0.18362.0/ucrt
-isystemC:/Program Files (x86)/Windows Kits/10/Include/10.0.18362.0/shared
-isystemC:/Program Files (x86)/Windows Kits/10/Include/10.0.18362.0/winrt
-isystemC:/Program Files (x86)/Windows Kits/10/Include/10.0.18362.0/cppwinrt
LSP: cpptools/fileChanged: file:///c%3A/Users/br20279/Downloads/test_packed/packed.cpp
LSP: cpptools/getDocumentSymbols: file:///c%3A/Users/br20279/Downloads/test_packed/packed.cpp (id: 222)
LSP: cpptools/fileChanged: file:///c%3A/Users/br20279/Downloads/test_packed/packed.cpp
Error while processing C:\Users\br20279\Downloads\test_packed\packed.cpp.
C:\Users\br20279\Downloads\test_packed\packed.cpp:20:1: error: declaration of anonymous struct must be a definition [clang-diagnostic-error]
struct __pragma(pack(push, 1)) Packed {
^
C:\Users\br20279\Downloads\test_packed\packed.cpp:20:1: warning: declaration does not declare anything [clang-diagnostic-missing-declarations]
C:\Users\br20279\Downloads\test_packed\packed.cpp:33:19: error: invalid application of 'sizeof' to an incomplete type 'struct Packed' [clang-diagnostic-error]
    static_assert(sizeof(struct Packed) == 5);
                  ^     ~~~~~~~~~~~~~~~
C:\Users\br20279\Downloads\test_packed\packed.cpp:33:33: note: forward declaration of 'Packed'
    static_assert(sizeof(struct Packed) == 5);
                                ^
LSP: $/cancelRequest (cpptools/getInlayHints, id: 219)
LSP: cpptools/getInlayHints: file:///c%3A/Users/br20279/Downloads/test_packed/packed.cpp (id: 223)
LSP: Request canceled by server (<unknown/completed>, code: -32802, id: 219)
loop ocioso: analisando novamente o documento ativo
Verificando erros de sintaxe: C:\Users\br20279\Downloads\test_packed\packed.cpp
Enfileirando a atualização do IntelliSense para arquivos na unidade de tradução de: C:\Users\br20279\Downloads\test_packed\packed.cpp
LSP: cpptools/getInlayHints: file:///c%3A/Users/br20279/Downloads/test_packed/packed.cpp (id: 224)
LSP: Request canceled by server (cpptools/getInlayHints, code: -32802, id: 223)
enviando 1 alterações para o servidor
enviando 1 alterações para o servidor
enviando 1 alterações para o servidor
Contagem de rabiscos de erro: 1
LSP: Request canceled by server (cpptools/getSemanticTokens, code: -32802, id: 216)
Atualize o tempo do IntelliSense (s): 0.393
Verificando erros de sintaxe: C:\Users\br20279\Downloads\test_packed\packed.cpp
Enfileirando a atualização do IntelliSense para arquivos na unidade de tradução de: C:\Users\br20279\Downloads\test_packed\packed.cpp
Contagem de rabiscos de erro: 1
Atualize o tempo do IntelliSense (s): 0.286
LSP: cpptools/getSemanticTokens: file:///c%3A/Users/br20279/Downloads/test_packed/packed.cpp (id: 225)
LSP: Message ignored due to no registered handler: $/setTrace
LSP: cpptools/didChangeSettings
O preenchimento automático está habilitado.
Os rabiscos de erro serão habilitados se todas as dependências de cabeçalho forem resolvidas.
O Hover está habilitado.
A extensão usará o Analisador de Marca para IntelliSense quando #includes não for resolvido.
Mecanismo IntelliSense = default.
A Colorização Avançada está habilitada.
LSP: cpptools/getFoldingRanges: file:///c%3A/Users/br20279/Downloads/test_packed/packed.cpp (id: 226)
LSP: cpptools/getFoldingRanges: file:///c%3A/Users/br20279/Downloads/test_packed/packed.cpp (id: 227)
LSP: cpptools/textEditorSelectionChange
LSP: cpptools/activeDocumentChange: file:///c%3A/Users/br20279/Downloads/test_packed/packed.cpp
LSP: textDocument/hover: file:///c%3A/Users/br20279/Downloads/test_packed/packed.cpp (id: 228)
LSP: cpptools/getInlayHints: file:///c%3A/Users/br20279/Downloads/test_packed/packed.cpp (id: 229)
</spoiler>

Other Extensions

No response

Additional context

The complete code for testing is

#include <cstdint>

#ifdef __GNUC__
#if __cplusplus >= 201103L
#define __begin_packed [[gnu::packed]]
#else
#define __begin_packed __attribute__((packed))
#endif
#define __end_packed
#else // MSVC
#define __begin_packed __pragma(pack(push, 1))
#define __end_packed   __pragma(pack(pop))
#endif

struct Normal {
    uint8_t a;
    uint32_t b;
};

struct __pragma(pack(push, 1)) Packed {
    uint8_t a;
    uint32_t b;
} __end_packed;

struct After {
    uint8_t a;
    uint32_t b;
};

int main() {
    static_assert(sizeof(struct Normal) == 8);
    static_assert(sizeof(struct Packed) == 5);
    static_assert(sizeof(struct After) == 8);
}
michelleangela commented 1 year ago

@EduMenges The following errors are from Clang-Tidy (the executable c:\Users\br20279\.vscode\extensions\ms-vscode.cpptools-1.16.3-win32-x64/bin/../LLVM/bin/clang-tidy.exe seen from the logs that the extension uses for "linter" tool) which may have limitations when supporting some code that are specific to compilers other than Clang, such as MSVC for this example.

Errors from Clang-Tidy and not the extension's IntelliSense.

declaration of anonymous struct must be a definition C/C++(clang-diagnostic-error)
declaration does not declare anything C/C++(clang-diagnostic-missing-declarations)

The extension has code analysis settings (see C_Cpp.codeAnalysis.clangTidy*) to configure Clang-Tidy. Some of the settings include disabling some Clang-Tidy checks or directly passing args to the Clang-Tidy executable to further configure Clang-Tidy.

If Clang-Tidy is not supposed to show errors for the sample code, then Clang-Tidy may have a bug for checking code specific to MSVC.

github-actions[bot] commented 12 months ago

This issue has been closed because the described behavior was determined to be by design.