2

I am wondering if it is possible to get clang format to have the following formatting for exception handling.

// Scenario 1: Catch and ignore exception
try
{
  DoWork();
}
catch (const SomeException&) {}

// Scenario 2: Handle exception
try
{
  DoWork();
}
catch (const SomeException& Exception)
{
  // Code handling exception.
}

I have not been able to get scenario 1 formatting on a single line it prefers to put a new line before the { and then depending on SplitEmptyFunction will be:

// Scenario 1: Catch and ignore exception (SplitEmptyFunction is true)
try
{
  DoWork();
}
catch (const SomeException&)
{}

// Scenario 1: Catch and ignore exception (SplitEmptyFunction is false)
try
{
  DoWork();
}
catch (const SomeException&)
{
}

Have I missed a setting or is this just not possible?

1 Answers1

0

For now my work around has been to keep SplitEmptyFunction as false and apply a post clang format hack to change the empty catch to be on a single line using a perl regex:

  # Hack - allow empty catch statements on a single line.
  if ($line =~ /^.+catch.+$/ &&
      $lines[$lineNo + 1] =~ /^ +\{ *$/ &&
      $lines[$lineNo + 2] =~ /^ +\} *$/)
  {
    $line = $line." {}";
    # Flag following lines when iterated over to be ignored.
    $lines[$lineNo + 1] = "clang_format_no_op_entire_statement";
    $lines[$lineNo + 2] = "clang_format_no_op_entire_statement";
  }