CLion Macro Rearrangement - c

I use a library called e4c in a C project, which allows try...catch syntax in C. However, CLion rearrange function always messes the indentation.
Is there any way to tell CLion to rearrange code before macro substitution?
edit:
For example, when the following code is rearranged:
try {
} catch (RuntimeException) {
}
It gives:
try {
} catch (RuntimeException) {
}
Because the try catch expand to 2 ifs and a while loop.

It seems that try and catch are macros, so you can find that the formatting is correct after the macros substitution.
You can mention your case in CLion tracker

Related

Astyle Incorrectly Formatting Linux Style Braces

According to the Linux kernel coding style, if only one branch of a conditional statement
is a single statement, then braces should be used in both branches. For example:
if (condition) {
do_this();
do_that();
} else {
otherwise();
}
This can be found in Section 3 of the official Linux kernel coding style document.
Astyle's latest release 3.0.1 incorrectly formats conditionals like this. For example, Astyle leaves the following untouched:
if (condition) {
do_this();
do_that();
} else
otherwise();
Is there a known fix for this in Astyle? If not, are current development efforts underway? If not, could someone point me in the right direction to get this fix integrated into the tool.
Astyle option "--style=1tbs" could be used to fix that.

Convert a Regular C styled coding file to GNU style coding standards

I have a file which I have written in "Regular C style". for example
void foo()
{
if(;;) {
....
} else {
...
}
}
wanted to change this style to GNU style coding using vim or some other plugin
void foo()
{
if(;;)
{
....
}
else
{
....
}
}
Wanted to ask if there can be a quick shortcut to do it so that it detects the loops starting braces and brings it below and indents it by 2 spaces.
Any help is greatly appreciated !
The correct tool for that job is GNU indent, which was created exactly for the purpose of re-indenting existing C code. In particular, you might want to look at the option --gnu-style.
Just a quick answer, probably not the best: you can insert the line breaks, select the whole document and re-indent with:
:1,$s/\v(\S) *\{/\1^M{/g
:1,$s/\v\} *(\S)/}^M\1/g
:1
VG=
Of course, this depends on your vim configuration (the = command, for instance, depends on your definition of equalprg). Moreover, this treats the comments like normal code.

R, SQL problems debugging a package, debug(.Call)

I am trying to debug an error that I think is in a pacakge. While I am stepping through the code with the debugger there is a section of code that produces the error I am getting.
if (fetch == FALSE | nrow(data) < 1) {
stat <- .Call("RODBCExecute", attr(channel, "handle_ptr"),
data, as.integer(rows_at_time))
if (stat == -1L) {
if (errors) {
stop(paste0(RODBC::odbcGetErrMsg(channel), collapse = "\n"))
}
else {
return(stat)
}
}
.Call() sets stat = -1 and this produces the error. I tried to use debug(.Call) but this is not working. From what I understand from searching online is that the .Call() uses compiled C/C++ code. Is there anyway to debug this code any farther?
You are correct: .Call("thisCFunction",...) calls a C/C++ function, loaded in by the package from the .so file, that is called thisCFunction in it's C++ declaration.
I would not try to debug this; you would have to go through the C++ source code by hand to figure out exactly what's happening, which would require a firm understanding of not only general C/C++ but also R's API (and the author's style of programming). Not trying to be rude, but I would guess if you are unfamiliar with .Call and what it does, you're probably unfamiliar with R's API.
An easier first step would be to track down exactly what is being supplied to this function and try to figure out why it's not what RODBCExecute expects. Given that the authors have written an error message, I would check to see if that error message helps point out the issue. My guess is that the real error occurred much earlier and this results in providing invalid arguments to RODBCExecute.

Can I export the used code from a c program with many compiler flags?

I would like to condense our linux driver code in to only the code that runs on the current kernel. It has parts that are ignored by if statements all the way back to kernele 2.4.x
Have you ever heard of a way to compile the code to an output which will be the working code with out all the stuff ignored by the c compiler if else statements?
I am wondering if we can run make something or gcc something that will simply result in all the code that is used for that build.
So like if I had this .c file below, then after running the make command I should have just the code I need for the newest kernel.
example.c
static somefunction .... {
avar = 0;
#if (linux_ver >= 2.6.31)
some newer code
#elseif (linux_ver >= 2.4.24)
some older code
#else
original code
#endif
}
after extracting / condensing, example.c would simply read as below
static somefunction .... {
avar = 0;
some newer code
}
The tool you are after is sunifdef or (more recent) coan.
See also: Is there a C pre-processor which eliminates #ifdef blocks based on values defined or undefined?
That's what the preprocessor directives do already. Try running the code through gcc -E (but prepare for a lot of output, as all #includes will be inlined).

How can I autoformat/indent C code in vim?

When I copy code from another file, the formatting is messed up, like this:
fun()
{
for(...)
{
for(...)
{
if(...)
{
}
}
}
}
How can I autoformat this code in vim?
Try the following keystrokes:
gg=G
Explanation: gg goes to the top of the file, = is a command to fix the indentation and G tells it to perform the operation to the end of the file.
I like to use the program Artistic Style. According to their website:
Artistic Style is a source code indenter, formatter, and beautifier for the C, C++, C# and Java programming languages.
It runs in Window, Linux and Mac. It will do things like indenting, replacing tabs with spaces or vice-versa, putting spaces around operations however you like (converting if(x<2) to if ( x<2 ) if that's how you like it), putting braces on the same line as function definitions, or moving them to the line below, etc. All the options are controlled by command line parameters.
In order to use it in vim, just set the formatprg option to it, and then use the gq command. So, for example, I have in my .vimrc:
autocmd BufNewFile,BufRead *.cpp set formatprg=astyle\ -T4pb
so that whenever I open a .cpp file, formatprg is set with the options I like. Then, I can type gg to go to the top of the file, and gqG to format the entire file according to my standards. If I only need to reformat a single function, I can go to the top of the function, then type gq][ and it will reformat just that function.
The options I have for astyle, -T4pb, are just my preferences. You can look through their docs, and change the options to have it format the code however you like.
Here's a demo. Before astyle:
int main(){if(x<2){x=3;}}
float test()
{
if(x<2)
x=3;
}
After astyle (gggqG):
int main()
{
if (x < 2)
{
x = 3;
}
}
float test()
{
if (x < 2)
x = 3;
}
The builtin command for properly indenting the code has already been mentioned (gg=G). If you want to beautify the code, you'll need to use an external application like indent. Since % denotes the current file in ex mode, you can use it like this:
:!indent %
I find that clang-format works well.
There are some example keybindings in the clang documentation
I prefer to use the equalprg binding in vim. This allows you to invoke clang-format with G=gg or other = indent options.
Just put the following in your .vimrc file:
autocmd FileType c,cpp setlocal equalprg=clang-format
The plugin vim-autoformat lets you format your buffer (or buffer selections) with a single command: https://github.com/vim-autoformat/vim-autoformat. It uses external format programs for that, with a fallback to vim's indentation functionality.
I like indent as mentioned above, but most often I want to format only a small section of the file that I'm working on. Since indent can take code from stdin, its really simple:
Select the block of code you want to format with V or the like.
Format by typing :!indent.
astyle takes stdin too, so you can use the same trick there.
I wanted to add, that in order to prevent it from being messed up in the first place you can type :set paste before pasting. After pasting, you can type :set nopaste for things like js-beautify and indenting to work again.
Maybe you can try the followings
$indent -kr -i8 *.c
Hope it's useful for you!
Their is a tool called indent. You can download it with apt-get install indent, then run indent my_program.c.
For a good overview and demo of many of the options mentioned here, #Gavin-Freeborn has a great video on YouTube:
https://www.youtube.com/watch?v=tM_uIwSucPU
It covers some Vim plugins as well as built-in capabilities such as =, gq, and formatprg.

Resources