llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.77k stars 11.89k forks source link

clang-format: Function-try blocks are formatted incorrectly #20989

Open llvmbot opened 10 years ago

llvmbot commented 10 years ago
Bugzilla Link 20615
Version trunk
OS Linux
Reporter LLVM Bugzilla Contributor

Extended Description

Example:

  int main(int argv, char *argv[])
  try {
      // something
      return 0;
  } catch (std::exception const &e) {
      // something else
      return 1;
  }

Attach

// {BasedOnStyle: LLVM}

  int main(int argv, char *argv[]) try {
    // something
    return 0;
  } catch (std::exception const &e) {
    // something else
    return 1;
  }

Stroustrup

// {BasedOnStyle: LLVM, BreakBeforeBrace: Stroustrup}

  int main(int argv, char *argv[]) try {
    // something
    return 0;
  }
  catch (std::exception const &e) {
    // something else
    return 1;
  }

Allman

// {BasedOnStyle: LLVM, BreakBeforeBrace: Allman}

  int main(int argv, char *argv[]) try
  {
    // something
    return 0;
  }
  catch (std::exception const &e)
  {
    // something else
    return 1;
  }

I believe there should be break before the try keyword.

mydeveloperday commented 2 years ago

Do we think this is subjective or objective? I honestly don't think we'd bother to fix this? who writes functions without braces?

ace575 commented 8 months ago

This is completely subjective. After all, the function-try-block is an alternate function body, part of the function definition and therefore is closer to noexcept and const. It is not inside the function body.

They can be useful for avoiding indenting the entire body of a function an extra level of indentation just to make a function noexcept.

Why was the behavior here arbitrarily changed? https://discourse.llvm.org/t/a-question-regarding-formatting-of-function-try-block/49857

This seems like an area where a .clang-format option should be available to determine where to put the 'try' for Allman style braces: https://godbolt.org/z/KTGdYPxGd

sylveon commented 1 month ago

I'm of the opinion it should be part of the same line as a function (like other qualifiers), and am disappointed there isn't an option to modify this behavior.