'How to properly align types, variables, storage specifiers and values with clang-format?

I am compelled to investigate using clang-format to modify my code, and I am looking for some help with certain things.

Here is an example of our style, which I need to replicate:

/****************************************************************** CONSTANTS */

/** Default value of something. */
#define DEFAULT_SOMETHING (SOME_COMBINATION)


/************************************************ PUBLIC FUNCTION DEFINITIONS */
returntype_t
PREFIX1_PREFIX_2_functionName(type1_t const *const p_foo_bar,
                              type2_t const        bar_length,
                              type3_t       *const thingy_thing)
{
   returntype_t                        result        = RETURNTYPE_OK;
   typewithverylongname_t              some_variable = 0;
   static sometype_t      const *const p_some_ptr    =
      PLATFORM_DEFINED_ADDRESS;

   result = PREFIX_3_someFunction(p_foo_bar, &some_variable);
   ...
}


/*********************************************  INTERNAL FUNCTION DEFINITIONS */
returntype_t
PREFIX1_PREFIX_2_functionNameTooLongForMaxLineLength(
   type1_t          const *const p_foo_bar,
   type2_t          const        bar_length,
   type3_t volatile       *const thingy_thing)
{
   some_struct_t jobbler =
   {
      val1,
      val2,
      val3
   };
   ...
}

The important things here are:

  • The source is split into sections, such as Constants, Internal Data, Public Function Definitions and Internal Function Definitions; there must be a two-line space between the end of each section and the banner comment heralding the next section. How do I stop clang-format from deleting the second blank line?
  • Function parameters should begin to follow on the same line if all parameter lines can be aligned and fit between the opening bracket and the maximum line length, and otherwise should start on the next line and be indented once.
  • In variable declarations and in the function signature, types should be aligned, different levels of const and volatile should be aligned, pointer notations should be aligned and variable names should be aligned.
  • All variables must be initialised at declaration (we check this separately) so the equality symbols must be aligned and the values must be aligned unless the value is a compound, in which case it goes on subsequent lines, or is too long for the line.
  • First lines after declarations are often reassignments to the output of a function. The assignment should not be considered part of declaration/initialisation block immediately above.

Additionally, I want to be able to have no rules for certain things, and just for the formatter to leave them as they are in the code, categorically, so the code is not just filled with exclusion comments.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source