[clang格式捕获,单行带有空语句

问题描述 投票:1回答:1

我想知道是否可以使clang格式具有以下用于异常处理的格式。

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

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

我无法在单行上获得方案1的格式,它倾向于在{之前放一个新行,然后取决于SplitEmptyFunction将是:

// 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&)
{
}

我是否错过了设置,或者这是不可能的吗?

c++ format try-catch clang-format catch-block
1个回答
0
投票

目前,我的解决方法是将SplitEmptyFunction保持为false并使用后clang格式的hack来使用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";
  }
© www.soinside.com 2019 - 2024. All rights reserved.