splitting parameters rather than declaration - clang-format

I'm trying to get clang-format to do my bidding, and stumbling on one point. I want my C function declaration to be formatted as:
sensor_t *sensor_enable_alarm(sensor_t *sensor,
float lo_thresh,
float hi_thresh);
but with my current settings, clang-format wants to format it as:
sensor_t *
sensor_enable_alarm(sensor_t *sensor, float lo_thresh, float hi_thresh);
I've initialized my .clang-format from llvm via:
clang-format -style=llvm -dump-config > .clang-format
and modified just the BinPackParameters to be false:
BinPackParameters: false
What additional tweaks to .clang-format do I need?

This can be done by setting PenaltyReturnTypeOnItsOwnLine to a larger value. The default value for LLVM style is 60. Try 200, which is the default for Google, Mozilla, and Chromium styles.
For example, clang-format -style='{BasedOnStyle: LLVM, BinPackParameters: false, PenaltyReturnTypeOnItsOwnLine: 200}' yourfile.cpp gives this output:
sensor_t *sensor_enable_alarm(sensor_t *sensor,
float lo_thresh,
float hi_thresh);
As always, see the documentation for more information. However, unfortunately, there really isn't much there to explain the "Penalty" values.

Related

How to avoid formatting for XML, SQL etc. raw strings in clang-format?

I have some C++ code with embedded XML like this:
QLatin1String test()
{
return QLatin1String(R"XML(
<ui language="c++">
<widget class="Test" name="dialogbuttonbox">
<property name="text">
<string>DialogButtonBox</string>
</property>
</widget>
</ui>)XML");
}
When formatting this with
clang-format --style=WebKit test1.cpp
Then the xml will be formatted to be broken, because of the definition of RawStringFormat:
If no style has been defined in the .clang-format file for the
specific language, a predefined style given by ‘BasedOnStyle’ is used.
If ‘BasedOnStyle’ is not found, the formatting is based on llvm style.
So I tried the following within .clang-format:
RawStringFormats:
- Language: None
Delimiters: ['XML']
DisableFormat: true
Which results into the following error:
.clang-format:177:15: error: unknown enumerated scalar - Language: None
What I really want to avoid is to add comments to the source code to switch formatting off/on:
// clang-format off
// clang-format on
So my question is how to switch off formatting for XML, SQL or whatever is not supported by clang-format raw strings?
Also what about the above unknown enumerated scalar message? Does it mean that Language: None ist not allowed? So maybe I have a feature request?
I had a similar issue.
My fix was to split the XML sections into their own files.
The clang formater then doesn't touch them and your standard VS Code formater will be used.
I then needed to set the maxLineWidth to 0 which means no new lines because of column length.
.vscode/settings.json
"xml.format.maxLineWidth": 0

Change only a specific option with clang-format in C++ source file

I need to change only PointerAlignment with clang-format and leave all the other options unchanged. Is it possible?
I tried the following file:
BasedOnStyle: Google
IndentWidth: 4
Language: Cpp
# Force pointers to the type for C++.
DerivePointerAlignment: false
PointerAlignment: Left
it changes PointerAlignment, but applies Google style. Also I tried
BasedOnStyle: none
but it does not work.

Setting EnableHipHopSyntax to True with HHVM

When I run my code, I get the following error:
Syntax only allowed with -v Eval.EnableHipHopSyntax=true in /var/web/site/myfile.php on line 26
myfile.php has a function at that line that has:
public static function set (
string $theme // <str> The theme to set as active.
, string $style = "default" // <str> The style that you want to set.
, string $layout = "default" // <str> The layout that you want to assign.
): string // RETURNS <str>
The bottom line, ): string" is the appropriate syntax for the hack language, but for some reason HHVM decided to brilliantly disable its own syntax by default.
I can't seem to find any documentation with HHVM that indicates how to set that config file. How can one go about this process?
Edit:
It turns out my HHVM conversion tool was not converting <?php to <?hh as I had instructed it to, due to having converted itself. In other words, it was attempting to convert <?hh to <?hh, which did me no good.
I had mistakenly assumed that HHVM was disabling it for <?hh tags, which was not the case.
This syntax is part of Hack, but you have a PHP file. If you change the opening tag from <?php to <?hh, it'll work.
Alternatively, you can add hhvm.enable_hip_hop_syntax = true to /etc/hhvm/php.ini.

time.h functions do not see the header file

I am using structures and methods listed below and including sys/time.h or time.h do not change anything. What can be the problem?
struct tm theTime;
strptime((char *)nodeValue, "%a %b %d %H:%M:%S +0000 %Y", &theTime);
time_t epochTime = timegm(&theTime);
I am using XCode and compile for armv7. (Before, this was working perfectly. I don't know what's changed since before..)
I get the following errors:
Variable has incomplete type 'struct tm'
Implicit declaration of function 'strptime' is invalid in C99
Implicit declaration of function 'timegm' is invalid in C99
Enable the declarations by defining feature test macro -D_POSIX_C_SOURCE=200809L at compilation time or before including the header:
#define _POSIX_C_SOURCE 200809L
#include <time.h>
See here for more information on feature test macro _POSIX_C_SOURCE.
I have included another header path which also includes time.h and which's content is irrelevant. I fixed it and everything works again.
So, in situations like this, it is a good idea to check header paths to see if there is any other file with the same name but in another directory.
#ouah's answer is great and it is a good tip, however it is not the direct solution of my problem.

How to get Doxygen to recognize custom latex command

Is there a way to use extra latex packages and/or extra latex commands with Doxygen code documentation system. For example I define the shortcut in a custom sty file.
\newcommand{\tf}{\Theta_f}
Then I use it about 300 time in the code, which is across about a dozen files.
/*! Stochastic approximation of the latent response*/
void dual_bc_genw(
//...
double const * const psi, ///< \f$ \psi = B\tf \f$
//...
){/* lots of brilliant code */}
But how do I get the system to recognize the extra package.
Name your style file in the EXTRA_PACKAGES tag in your configuration file.

Resources