0

Using clang-format I often encounter issues like:

// I want this
umbra_map_ops.flags = UMBRA_MAP_CREATE_SHADOW_ON_TOUCH |
                      UMBRA_MAP_SHADOW_SHARED_READONLY;  

// Formatting gives me this
umbra_map_ops.flags =
    UMBRA_MAP_CREATE_SHADOW_ON_TOUCH | UMBRA_MAP_SHADOW_SHARED_READONLY;

// I want this
drmf_status_t status = umbra_read_shadow_memory(
    umbra_map, app, sz, &sz, (byte *)result);    

// Or this
drmf_status_t status = umbra_read_shadow_memory(umbra_map, app, sz, 
                                                &sz, (byte *)result);    

// Formatting gives me this
drmf_status_t status =
    umbra_read_shadow_memory(umbra_map, app, sz, &sz, (byte *)result);

Code looks awful. How to get rid of that behavior?

Clang format file


Update

Now I'm using clang-format: off. But I would prefer something different.

Original code:

static bool
ds_mem_init(client_id_t id)
{
    umbra_map_options_t umbra_map_ops;

    umbra_map_ops.scale = UMBRA_MAP_SCALE_SAME_1X;
    umbra_map_ops.flags = UMBRA_MAP_CREATE_SHADOW_ON_TOUCH | /* clang-format: disable */
                          UMBRA_MAP_SHADOW_SHARED_READONLY;  /* clang-format: disable */

    ...
}

static dr_signal_action_t
event_signal_instrumentation(void *drcontext, dr_siginfo_t *info)
{
    bool res = handle_special_shadow_fault(
        drcontext, info->raw_mcontext, info->access_address); /* clang-format: disable */

    return res ? DR_SIGNAL_SUPPRESS : DR_SIGNAL_DELIVER;
}
poul1x
  • 103
  • 1
  • 9
  • It's worth mentioning that not including a setting is not the same as ignoring it. You're just using whatever Webkit uses in that case. You really should just export Webkit's file, and look at every setting. Many of them have interactions with other settings. I also don't get the same results as you. Your last example fits in the 90 column limit you specified, so it's a one-liner. Same with your bitwise OR, it's a one-liner. – sweenish Jan 04 '21 at 20:42
  • Using your linked-to clang-format file (it should be a part of the question), formatting gives two one liners where you're complaining about what clang-format gives you. (my WSL2 has LLVM 7) Since it's not the same. If I change the column limit to 80, I get the results you are showing. Please make up your mind. – sweenish Jan 04 '21 at 20:47
  • @sweenish These example lines are likely indented some amount (inside a function, an inner block of code, etc). OP you should provide exact examples for what you want. – Kevin Jan 04 '21 at 20:54
  • @Kevin Fair point. – sweenish Jan 04 '21 at 20:55
  • Ok, I've posted original code. I hoped to find quick solution, but not to inspect all WebKit rules one by one – poul1x Jan 04 '21 at 21:14
  • One quick solution is: `AllowAllArgumentsOnNextLine: false`. I don't know what version of LLVM you need to get that option. Documentation for 7 & 8 don't show it. The system I'm on has a Debian WSL2 and it's on LLVM 7. The first option is likely a combo that I am now unsure I can find due to the age of my version. – sweenish Jan 04 '21 at 21:32

1 Answers1

1

For the first case, you need:

AlignOperands: Align
PenaltyBreakAssignment: 6

The first should be straightforward; it is exactly for what you want. The second is needed to tweak clang-format to not prefer breaking the assignment and having the right hand operand on single line. You may need to experiment with different values since this is relative to other penalties. 6 was the lowest penalty that passed on your example code.

For the second case, you can bump up the penalty further, while lowering a conflicting penalty. The "assignment" penalty seems to apply here although technically it is not an assignment but copy initialisation:

PenaltyBreakAssignment: 20
PenaltyBreakBeforeFirstCallParameter: 0

For what it's worth, these penalties default to 2 for assignment and 19 for first call param at least in the version of clang-format that I tested.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • Thanks, that really works! Finally, I set these settings: `AlignOperands: true` to align binary operators, `PenaltyBreakAssignment: 50` to avoid '=' on the next line – poul1x Jan 05 '21 at 01:14