poul1x
poul1x

Reputation: 133

Clang-format: How to avoid new line breaks

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;
}

Upvotes: 1

Views: 4153

Answers (1)

eerorika
eerorika

Reputation: 238291

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.

Upvotes: 3

Related Questions