1

I recently tried to build a project with clang and clang++. Up to now it has only been built with GCC.

One of the problems that I saw was with code that is of the following form

if (expression)
{
   var = var;
}

I assume that this was done to give a place in the code to put breakpoints in debug builds. In optimized builds the code should all be optimized away.

We build with -Werror and this fails to build with

error: explicitly assigning value of variable of type 'int' to itself [-Werror,-Wself-assign]

I can get rid of the warning/error with -Wno-self-assign or -Wno-error=self-assign or locally with

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wself-assign"
var = var;
#pragma clang diagnostic pop

(possibly in a MACRO).

Does anyone have any other suggestions for code without side effects that could be used for breakpoints?

2 Answers 2

0

This is what I use. It also issues an unused warning that make them easy to find.

int x = 3; 
Sign up to request clarification or add additional context in comments.

2 Comments

But -Werror will turn the unused warning into an error, which is part of the OP's problem.
add a (void) x; to supress that.
0

The standard way in C++ is to use std::breakpoint or std::breakpoint_if_debugging() which is a new C++26 feature, the latter is probably more suitable

This function standardizes many similar existing facilities: __builtin_debugtrap from LLVM, __debugbreak from Win32 API, debugger_break from boost.test, assert(false), _asm { int 3 } (MSVC) and asm("int3") (GCC/clang) for x86 targets, etc.

#if DEBUG
if (std::is_debugger_present())
    std::breakpoint();
std::breakpoint_if_debugging();
#endif

As can be seen from the quote, in LLVM you can use __builtin_debugtrap, or if you have Boost you can use debugger_break

There are other methods that you can see in Is there a portable equivalent to DebugBreak()/__debugbreak?

4 Comments

I think the goal is to have a statement where you can set a breakpoint, but are not forced to. With std::breakpoint the program will always stop when it is reached, and it doesn't go away on optimized builds.
@NateEldredge that's why I put an#if there. if (debug) {...} also works
But I think we want to be able to decide within the debugger whether to have a breakpoint there.
@NateEldredge yeah that's a valid concern. It's better to not use these at all though, because when we want to break at any line we should put a breakpoint manually at that line

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.