Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meaning of RawStringFormats and Delimeters 'pb' in clang format

I cannot understand how does the following affect the formatting behavior in the clang format file:

RawStringFormats: 
  - Delimiters:       [pb]
    Language:        TextProto
    BasedOnStyle:    google

Are these settings correct for a c++ project? (e.g. shouldn't the language be cpp?)

What does it mean 'pb' for Delimeters and how is it different from 'cc' and 'cpp'?

I tried to look for information inside clang documentations but it just adds to my confusion.

like image 887
Saeid Avatar asked Mar 14 '21 19:03

Saeid


People also ask

What is clang-format style?

Clang-Format Style Options are flags that are supported by the ClangFormat tool, which became the de-facto standard to format C++ code. Clang offers the option to use one of the predefined styles (LLVM, Google, Chromium, Mozilla, WebKit, Microsoft) or to create a custom configuration by using the given flags.

How do you format a clang file?

To automatically format a file according to Electron C++ code style, run clang-format -i path/to/electron/file.cc . It should work on macOS/Linux/Windows. The workflow to format your changed code: Make codes changes in Electron repository.

What is clang-format default style?

clang-format file uses YAML format: key1: value1 key2: value2 # A comment. ... The configuration file can consist of several sections each having different Language: parameter denoting the programming language this section of the configuration is targeted at.

Can clang-format break code?

Short answer: YES. The clang-format tool has a -sort-includes option. Changing the order of #include directives can definitely change the behavior of existing code, and may break existing code.


Video Answer


2 Answers

[pb] as the delimiter there stands for ProtocolBuffers or Protobuf in short. The RawStringFormat formatting option is specifically for acting as a hint to detect and format code fragments that appear as raw strings within the file (for supported languages); It uses the Delimiters or the EnclosingFunctions to detect and reformat them using the specified formatting style.

It could be useful even in a C++ project, if you want the formatting to be applied to raw strings in the file. For example, often C++ projects that uses protobuf input and output has test code that might look like:

InputProto ConstructInput() {
  InputProto input;
  Input *in = input.mutable_input();
  in->set_id(10);
  in->set_name("Test");
  return in;
}
TEST(TestFoo, ComputesOut) {
OutputProto out;
out.set_result(200);

EXPECT_THAT(
    GetOutput(ConstructInput()),
    EqualsProto(outcome));
}

Protobuf allows you to write the same in a more human readable fashion using text format like so:

TEST(TestFoo, ComputesOut) {
EXPECT_THAT(
    GetOutput(PARSE_TEXT_PROTO(R"pb(
        input {
            id: 10
            name: "Test"
        }
      )")),
    EqualsProto(PARSE_TEXT_PROTO(R"pb(
        result: 200
      )"))
    );
}

And your formatting options would detect the text string within the pb delimiter as protobuf code and reformat it using the google formatting style.

like image 127
jester Avatar answered Oct 19 '22 04:10

jester


Your settings are for formatting Google Protocol Buffers (https://developers.google.com/protocol-buffers) or short Protobuf.

If your C++ projects contains Protobuf descriptions the 'google' style formatting is applied.

I assume [pb] is short for Protobuf.

like image 1
Markus Schumann Avatar answered Oct 19 '22 05:10

Markus Schumann