C++ False positive with Win32 API call (dynamically loaded function)

I’m using SonarLint 7.8.0.88494 and I get a ““using” should be preferred for type aliasing” cpp:S5416 Tags: cppcoreguidelines design since-c++11

The problem is that the code in question is a typedef for a dynamically loaded Win32 API function and the typedef clearly specifies “stdcall” calling convention (that’s not permitted with the “using” statement). The function is actually the D3DCompile2 function (d3dcompiler.h) - Win32 apps | Microsoft Learn.

The relevant code is this:

typedef HRESULT(__stdcall* pD3DCompile2g)(
    _In_reads_bytes_(SrcDataSize) LPCVOID pSrcData,
    _In_ SIZE_T SrcDataSize,
    _In_opt_ LPCSTR pSourceName,
    _In_reads_opt_(_Inexpressible_(pDefines->Name != NULL)) CONST D3D_SHADER_MACRO* pDefines,
    _In_opt_ ID3DInclude* pInclude,
    _In_ LPCSTR pEntrypoint,
    _In_ LPCSTR pTarget,
    _In_ UINT Flags1,
    _In_ UINT Flags2,
    _In_ UINT SecondaryDataFlags,
    _In_reads_bytes_opt_(SecondaryDataSize) LPCVOID pSecondaryData,
    _In_ SIZE_T SecondaryDataSize,
    _Out_ ID3DBlob** ppCode,
    _Always_(_Outptr_opt_result_maybenull_) ID3DBlob** ppErrorMsgs);

Hello @jpmugaas and welcome to our community!

I believe this is inaccurate as the following should be equivalent:

typedef HRESULT(__stdcall* pfun1)(int* param);

using pfun2 = HRESULT (__stdcall*)(int* param);

Does your compiler reject the second form? If so, can you share the error message and information about your compiler?

Thanks

1 Like

The compiler accepted the second form and I now use that for the other statements in my header file. I didn’t know about that syntax and one version of SonarQUbe had offered to fix it but it did not compile (I don’t recall when).

Thank you @jpmugaas, this is valuable feedback!

I was able to reproduce an issue with the quick fix. I recorded it in [CPP-5345] - Jira.

Cheers

I have decided that because of the issues discussed in the bug report that it probably would be best if I switched back to using typedef’s for those Win32 API functions.

It seems to me that the rule should state that API functions are an exception if they have their own calling conventions.

1 Like

Hello @jpmugaas,

We actually discussed the topic the other day with some colleagues and reached a similar conclusion. The arguments we considered are available in the ticket and the fix should be available in our next release.

Thanks again for sharing your point of view with us.

Cheers

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.