[cfe-users] clang-format: break before brace in multiline conditionals

Nikos Chantziaras via cfe-users cfe-users at lists.llvm.org
Thu Aug 1 22:43:32 PDT 2019


Is there a way to make clang-format either add a line break before the 
brace in this case, or at least indent the continuation line by a higher 
amount of spaces than normal?

This is my issue:

   if (auto it = files_by_name_.find(file_name);
       it != files_by_name_.end()) {
       file_name = it->second;
       logWarn("File {} was not found by checksum, but a file with the "
               "same name exists. Will use that instead.",
               file_name);
   }

This is difficult to read, as it's not obvious where the if condition 
ends and where the code begins without taking a closer look first. In 
this example, one might think the if just controls the log message.

I'd either like this instead:

   if (auto it = files_by_name_.find(file_name);
       it != files_by_name_.end())
   {
       file_name = it->second;
       logWarn("File {} was not found by checksum, but a file with the "
               "same name exists. Will use that instead.",
               file_name);
   }

So even though normally I don't want a newline before the opening brace, 
if the initializer and/or condition doesn't fit on one line, then there 
should be a newline before the brace. It seems clang-format only offers 
an all-or-nothing option (BraceWrapping AfterControlStatement) where 
either every brace gets a line break, or none of them do.

If that's not possible, then at least is there a way to get this instead:

   if (auto it = files_by_name_.find(file_name);
           it != files_by_name_.end()) {
       file_name = it->second;
       logWarn("File {} was not found by checksum, but a file with the "
               "same name exists. Will use that instead.",
               file_name);
   }

Meaning, add extra indentation for continuation lines in if conditions.



More information about the cfe-users mailing list