Remove line break after single-statement if with clang-format - c++

I am trying to get clang-format to leave if-statements with one statement on the same line.
Example input:
if(is_finished) break;
if(foo) {
// do something
}
clang-format output:
if(is_finished)
break;
if(foo) {
// do something
}
Wanted output:
if(is_finished) break;
if(foo) {
// do something
}
None of the space related options seem to match this style.
current config:
---
Language: Cpp
BasedOnStyle: LLVM
IndentWidth: 8
UseTab: ForIndentation
SpaceBeforeParens: Never
BraceWrapping:
AfterControlStatement: false

The relevant config option is AllowShortIfStatementsOnASingleLine.
The choices are:
Never
WithoutElse
Always
https://clang.llvm.org/docs/ClangFormatStyleOptions.html

AllowShortIfStatementsOnASingleLine
And
AllowShortBlocksOnASingleLine
The first one does what you want, AllowShortBlocksOnASingleLine will also allow code such as
if (expression) { Something(); }

Related

place else on new line in Visual Studio Code

how can I place my else on new line, for example, I want to change
if () {
} else {
}
into
if() {
}
else {
}
I have edit theC_Cpp: Clang_format_fallback Style and entered { BasedOnStyle:Microsoft, IndentWidth: 4, ColumnLimit: 0}
Looks like vscode's formatter uses clang-format by default, so if you didn't change it, we can refer to the clang-format options directly, can you try to append the following key-value (BeforeElse) pair within your braces?
BraceWrapping: [BeforeElse: true]
If that doesn't work, then you should try to use the traditional .clang-format file which vscode also supports.

Wrong RegEx match in Dart

The code itself: (you can see it on DartPad)
void main() {
print(new RegExp("[0-9]|'|\"|\.").hasMatch('g')); // should return false
print(new RegExp("[0-9]|'|\"|\.").hasMatch('0')); // return correctly true
}
Output:
true
true
With the same version on regex101 but with JS, the return value is correct.
Is there something I'm missing with my RegExp or should I report a bug?
Either you use a raw string
print(new RegExp(r'''[0-9]|'|"|\.''').hasMatch('g'));
(''' is to avoid a conflict with " inside the string)
or escape \
print(new RegExp("[0-9]|'|\"|\\.").hasMatch('g'));
DartPad example

Uncrustify: remove space opening/closing a block

How can I clean up blocks like the following:
if(a)
{
foo();
}
into
if(a)
{
foo();
}
Check these options:
# Whether to remove blank lines after '{'
eat_blanks_after_open_brace = true # false/true
# Whether to remove blank lines before '}'
eat_blanks_before_close_brace = true # false/true
But some other options such as mod_full_brace_if and nl_if_leave_one_liners can also have an impact. Could convert it to if(a) foo(); for example.

Qt / QRegExp regular expression for brackets and new lines or "\r"?

I'm confused with capturing text using Qt QRegExp. I want a grab data from PHP code as simple:
public function asr($x)
{
echo "index";
}
Explanations:
public function <can be more spaces or new lines...> asr($x) <can be more spaces or/and new lines or/and tabs...>
{
echo "index";
}
What I want to grab from PHP file? This is one...
public function asr($x)
{
And how I'm now grabbed?
QRegExp reg("(public|private|protected|static|final)(.*)function(.*)[\(](.*)[\)](.*)[\n](.*)‌​[\{]", Qt::CaseInsensitive);
Explanations:
When I try add [\{] or (\{) to grab a braces from PHP file it will not grab a data:
Results only:
public function asr($x)
Instead:
public function asr($x) {
Note: between ($x) and { can be newline \n or some spaces as \s.
This will be as (\))(\n|\s) but no luck :(
Resolved results with example code:
if (file.open(QIODevice::ReadOnly))
{
QTextStream in(&file);
// regex for function with new line
QRegExp reg("((.*)public|private|protected|static|final)( *)function(.*)[(](.*)", Qt::CaseInsensitive);
// capture to list
QStringList list = reg.capturedTexts();
qDebug() << list;
while ( !in.atEnd() )
{
QString line_read = in.readLine();
//qDebug() << line_read; // read lines
qDebug() << reg.exactMatch(line_read); // find matches
}
}
}
FINAL RESULTS:
false
false
false
false
false
false
false
false
true // means found on this line for QRegExp policies
false
false
false
false
true
false
false
false
false
true
false
false
false
false
true
false
false
false
false
false
false
false
You have to use \\ for a backslash inside a "".
Edit:
You also don't have to escape (){ inside a []
QRegExp function("(public|private|protected|static|final)( *)function(.*)[(](.*)[)](.*)[{]", Qt::CaseInsensitive);
auto b = function.exactMatch( "public function ($d) \n {" );
b = function.exactMatch( "public function ($d){" );
works for me. The whitespace is consumed by (.*)
I know what is problem, reading line by line instead of all lines on file and then should replace text in file. I should not reading a file using in.readLine(), so I'm finding { on this line, but not exists because braces { are on next line!

Boost.Test application debugging

When debugging a C++ Boost.Test application inside VS2010 (VS2008), how to make the debugger stop at Boost.Test assertion failure points?
I haven't tried this myself, but in theory you'd want to set a breakpoint somewhere in the check_impl function (in the boost_unit_test_library source), probably in the non-PASS cases of its final case statement.
In practice I always just find myself just running the tests again (or the specific problem test, selected with --run_test=...) with a breakpoint on the offending check.
Note that a failing BOOST_REQUIRE results in a throw, so if you enable VS' break-on-C++-exceptions in the debugging options that'll break on those nicely (but not BOOST_CHECKs, which just return and carry on).
I put breakpoints in check_impl(), as suggested by #timday.
Here is an extract from Boost 1.57.0, file boost/test/impl/test_tool.ipp, lines 355 to 373:
switch( tl ) {
case PASS:
framework::assertion_result( true );
return true;
case WARN:
return false; // ADD BREAKPOINT HERE
case CHECK:
framework::assertion_result( false );
return false; // ADD BREAKPOINT HERE
case REQUIRE:
framework::assertion_result( false );
framework::test_unit_aborted( framework::current_test_case() );
throw execution_aborted(); // ADD BREAKPOINT HERE
}
assertion.hpp
template class binary_expr:
assertion_result evaluate( bool no_message = false ) const
{
assertion_result const expr_res( value() );
if( no_message || expr_res )
return expr_res; <<<<<<<< SUCCESS
BRK wrap_stringstream buff;
report( buff.stream() );
return tt_detail::format_assertion_result( buff.stream().str(), expr_res.message() );
}