Meaning of RawStringFormats and Delimeters 'pb' in clang format

Viewed 563

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.

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.

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.

Related