Clang Flag: -Wdangling / -Wno-dangling

From emmtrix Wiki
Jump to navigation Jump to search
The Clang compiler flags -Wdangling and -Wno-dangling are used to control warnings related to the potential misuse of references and pointers, which could lead to dangling references, pointers, or access to deallocated memory. Specifically, these flags target scenarios where objects may be destroyed at the end of a full-expression, leaving references or pointers to them invalid.

The -Wdangling group includes various subflags designed to flag different instances of dangling issues, such as -Wdangling-field, -Wdangling-initializer-list, and -Wdangling-gsl. These subflags cover a range of situations from binding references to temporary objects, initializing pointers with stack addresses, to warnings about returning addresses of stack memory or local temporaries.

As of the current state, the -Wdangling flag itself is set to be default inactive, meaning that the specific warnings it encompasses are not enabled by default. However, warnings that fall under the categories of -Wdangling-field, -Wdangling-initializer-list, -Wdangling-gsl, and -Wreturn-stack-address are activated by default. To suppress these types of warnings, developers can use the -Wno-dangling flag.

In practice, enabling these warnings helps developers to identify and correct potentially unsafe code patterns that could result in undefined behavior due to accessing invalidated memory locations. It encourages best practices in managing object lifetimes and ensures safer memory management in applications compiled with Clang.

 
AI Generated

Supergroups

Subroups

Warnings/Remarks

Default Active (Deactivate with -Wno-dangling)

warn_dangling_variable
warning:
temporary
whose address is used as value of
 
implicitly
bound to
 
reference
member of local variable
local
variable
reference
array backing
initializer list subobject of local variable
local initializer list
D
 
will be destroyed at the end of the full-expression

warn_unsupported_lifetime_extension
warning: sorry, lifetime extension of
temporary
backing array of initializer list
created by aggregate initialization using default member initializer is not supported; lifetime of
temporary
backing array
will end at the end of the full-expression

warn_bind_ref_member_to_parameter
warning: binding reference member A to stack allocated
variable
parameter
B

warn_dangling_member
warning:
reference
backing array for 'std::initializer_list'
 
subobject of
member A
binds to
is
a temporary object whose lifetime is shorter than the lifetime of the constructed object

warn_init_ptr_member_to_parameter_addr
warning: initializing pointer member A with the stack address of
variable
parameter
B

warn_new_dangling_reference warning: temporary bound to reference member of allocated object will be destroyed at the end of the full-expression
warn_new_dangling_initializer_list
warning: array backing
initializer list subobject of the allocated object
the allocated initializer list
will be destroyed at the end of the full-expression

warn_dangling_lifetime_pointer warning: object backing the pointer will be destroyed at the end of the full-expression
warn_dangling_lifetime_pointer_member warning: initializing pointer member A to point to a temporary object whose lifetime is shorter than the lifetime of the constructed object
warn_ret_addr_label warning: returning address of label, which is local
warn_ret_local_temp_addr_ref
warning: returning
address of
reference to
local temporary object

warn_ret_stack_addr_ref
warning:
address of
reference to
stack memory associated with
local variable
parameter
B returned

Default Inactive (Activate with -Wdangling)

None