llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[clang-tidy] bugprone-unchecked-optional-access ignores assert and assume

Open Febbe opened this issue 3 years ago • 9 comments

The check does not consider implicit checks like __assume or assert which explicitly tell that this value should not be uninitialized. For example, the following code snipped will rise a warning.

  for (std::optional<down_sampling::work::sample_down>&& job : jobv) {    //
    assert(job);
    job->do_something();
  }

Febbe avatar Jan 31 '23 13:01 Febbe

@llvm/issue-subscribers-clang-tidy

llvmbot avatar Jan 31 '23 14:01 llvmbot

Can you please provide more information on the configuration you are using for clang-tidy? When I run this in godbolt, it works correctly:

https://godbolt.org/z/n6PGevqrP

And similarly with a version closer to what you have above:

https://godbolt.org/z/ad6T89T1E

Might your clang-tidy be running with NDEBUG set or somesuch?

ymand avatar Feb 01 '23 16:02 ymand

Can you please provide more information on the configuration you are using for clang-tidy? When I run this in godbolt, it works correctly:

https://godbolt.org/z/n6PGevqrP

And similarly with a version closer to what you have above:

https://godbolt.org/z/ad6T89T1E

Might your clang-tidy be running with NDEBUG set or somesuch?

Mhh, maybe my clangd cache / the compile_commands.json was not updated. Will check this tomorrow. However, I would also expect an assert to be considered in release mode as well, same applies for or __assume/[[assume]] in general.

Febbe avatar Feb 01 '23 22:02 Febbe

However, I would also expect an assert to be considered in release mode as well, same applies for or __assume/[[assume]] in general. Sure, I meant whatever mode disables assert.

ymand avatar Feb 01 '23 22:02 ymand

Ok, I just deleted the cache and checked the compile_commands.json, but the warning is still there: My setup:

os: windows10 llvm: v15.0.7 Mode: Debug libstd: mscv-STL warning provider: clangd

The compile_commands.json snipped:

"command": "clang-cl.exe 
/nologo
-TP
-DASIO_STANDALONE
-DBOOST_CONTAINER_NO_LIB
-DBOOST_DATE_TIME_NO_LIB
-DBOOST_REGEX_NO_LIB
-DBOOST_SERIALIZATION_NO_LIB
-DGTEST_LANGUAGE_CXX11
-DNOMINMAX
-DQT_CORE_LIB
-DSPDLOG_COMPILED_LIB
-DUNICODE
-DWIN32
-DWIN64
-D_ENABLE_EXTENDED_ALIGNED_STORAGE
-D_UNICODE
-D_WIN64
-Dkiss_fft_scalar=double 
-imsvc\"C:\\Program Files (x86)\\Microsoft Visual Studio\\2022\\BuildTools\\VC\\Tools\\MSVC\\14.33.31629\\include\" 
-imsvc\"C:\\Program Files (x86)\\Windows Kits\\10\\include\\10.0.19041.0\\ucrt\" 
-imsvc\"C:\\Program Files (x86)\\Windows Kits\\10\\include\\10.0.19041.0\\shared\" 
-imsvc\"C:\\Program Files (x86)\\Windows Kits\\10\\include\\10.0.19041.0\\um\" 
-imsvcC:\\local\\boost_1_80_0 
-imsvcC:\\Qt\\6.3.2\\msvc2019_64\\mkspecs\\win32-msvc 
/DWIN32
/D_WINDOWS
/EHsc
/Zi
/Ob0
/Od
/RTC1
-MDd
/D_USE_MATH_DEFINES
/DNOMINMAX
/DWIN32_LEAN_AND_MEAN
/D_WIN32_WINNT=0x0601
/DBOOST_THREAD_USE_LIB
/W3
/WX
-DASIO_HAS_CO_AWAIT=1
-DASIO_HAS_STD_COROUTINE=1
-Wno-unused-command-line-argument
-Wno-error=unused-variable
-Wno-error=unused-private-field
-Wno-error=unused-function
-Wno-error=unused-but-set-variable
-Wno-error=attribute-warning
-Wno-error=deprecated-declarations
-Wno-error=ambiguous-reversed-operator
-Wno-error=assume
-Zc:__cplusplus
-permissive-
-utf-8
/MP
/bigobj
-std:c++20
/Founiversal\\CMakeFiles\\universal.dir\\src\\curve_prep_priv.cpp.obj
/FdTARGET_COMPILE_PDB
-c
-- curve_prep_priv.cpp"

May it be the msvc-STL implementation of assert?

Febbe avatar Feb 02 '23 16:02 Febbe

May it be the msvc-STL implementation of assert?

Good point! We rely on the implementation of assert for this to work correctly. In fact, we don't even handle this natively -- it falls out of the way that the CFG is built (based on the __noreturn attribute of assert). If their assert doesn't us __noreturn or the like, it won't work.

ymand avatar Feb 02 '23 17:02 ymand

The microsoft STL does not use __noreturn, but _ACRTIMP, which optionally defines __declspec(dllimport), so there is no way, to detect, if this function does not return:

//
// assert.h
//
//      Copyright (c) Microsoft Corporation. All rights reserved.
//
// Defines the assert macro and related functionality.
//
#if defined _VCRT_BUILD && !defined _ASSERT_OK
    #error assert.h not for CRT internal use
#endif

#include <corecrt.h>

#pragma warning(push)
#pragma warning(disable: _UCRT_DISABLED_WARNINGS)
_UCRT_DISABLE_CLANG_WARNINGS

_CRT_BEGIN_C_HEADER



#undef assert

#ifdef NDEBUG

    #define assert(expression) ((void)0)

#else

    _ACRTIMP void __cdecl _wassert(
        _In_z_ wchar_t const* _Message,
        _In_z_ wchar_t const* _File,
        _In_   unsigned       _Line
        );

    #define assert(expression) (void)(                                                       \
            (!!(expression)) ||                                                              \
            (_wassert(_CRT_WIDE(#expression), _CRT_WIDE(__FILE__), (unsigned)(__LINE__)), 0) \
        )

#endif



_CRT_END_C_HEADER
_UCRT_RESTORE_CLANG_WARNINGS
#pragma warning(pop) // _UCRT_DISABLED_WARNINGS

Febbe avatar Feb 02 '23 18:02 Febbe

I'm not familiar with microsoft idioms. Could the CFG be pathed to understand _ACRTIMP or are you saying that this construct is not as informative as _noreturn and so couldn't be used?

ymand avatar Mar 03 '23 17:03 ymand

I'm not familiar with microsoft idioms. Could the CFG be pathed to understand _ACRTIMP or are you saying that this construct is not as informative as _noreturn and so couldn't be used?

That define only denotes, that the function call is a call to a external possible dynamic linked library function. So the answer is, no it can't be used.

Febbe avatar Mar 09 '23 03:03 Febbe