【问题标题】:clang format catch with empty statement on single lineclang 格式捕获单行上的空语句
【发布时间】:2020-04-05 07:21:04
【问题描述】:

我想知道是否可以让 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】:

    目前我的解决方法是将 SplitEmptyFunction 保持为 false 并应用 post clang 格式 hack 以使用 perl 正则表达式将空捕获更改为单行:

      # 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";
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-26
      • 1970-01-01
      • 1970-01-01
      • 2016-08-09
      • 2022-10-30
      • 2022-11-08
      • 2014-11-24
      • 1970-01-01
      相关资源
      最近更新 更多