Uncrustify: Difference between revisions

From miki
Jump to navigation Jump to search
(Created page with "Uncrustify is highly configurable source beautifier. == Reference == * [https://github.com/uncrustify/uncrustify uncrustify on GitHub] ([https://github.com/xeyownt/uncrustify...")
 
 
(10 intermediate revisions by the same user not shown)
Line 3: Line 3:
== Reference ==
== Reference ==
* [https://github.com/uncrustify/uncrustify uncrustify on GitHub] ([https://github.com/xeyownt/uncrustify My fork on GitHub])
* [https://github.com/uncrustify/uncrustify uncrustify on GitHub] ([https://github.com/xeyownt/uncrustify My fork on GitHub])
:* [https://github.com/uncrustify/uncrustify/pull/855 pull request #855 - Disable indent in #pragma asm/endasm and #asm/#endasm sections]


== Build ==
== Build ==
Line 9: Line 10:
mkdir build
mkdir build
cd build
cd build
cmake ..
cmake -DCMAKE_BUILD_TYPE=Release ..
make # or cmake --build .
make
sudo make install # or sudo cmake --install
sudo make install # or sudo cmake --install
</source>
</source>
Note that stuff like <code># or cmake --build . or cmake --build . --config Release</code> don't seem to give good result (big binaries).


On Windows, install [https://cmake.org/download/ cmake], then it works the same:
On Windows, the easiest is to install MSYS2 (http://www.msys2.org/), and then build like on Linux:
* Recent Uncrustify only build on '''Windows 10'''. On Windows 7, we get:
System is unknown to cmake, create:
Platform/MINGW64_NT-6.1-7601 to use this system,
* Install MSYS2.
* Install necessary toolchain:
pacman -S make cmake gcc
* Build Uncrustify:
<source lang=bash>
<source lang=bash>
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
make
</source>

Alternativeyly, install [https://cmake.org/download/ cmake] for Windows, and build for Visual Studio or MinGW (old version):
<source lang=bash>
# To build with latest visual Studio
md build
md build
cd build
cd build
cmake ..
cmake ..
cmake --build . --config Release
cmake --build . --config Release

# To build with MinGW toolchain
md build
cd build
cmake -G "MinGW Makefiles" .. # Do twice if complains above sh.exe in path
cmake --build . --config Release
</source>
You need a Windows compiler, like Visual Studio Express, or MinGW.

;Issues
* Compilation issues on Mingw 4.8.1 &mdash; switch to MSYS2.

== Windows binaries ==
SourceForge distributes binaries for Windows, much easier than the cmake/MSYS2/Visual mic-mac...
* https://sourceforge.net/projects/uncrustify/

== Tips ==
=== Canonical indent ===

The script below perform a canonical indentation as follows:
* Force #define on one line, and remove excess whitespace
* Force all code on one line, but
:* Respect LF in block comments or before/after line comment
:* Keep empty lines
* Indent

<source lang="bash">
find . -name "*.[ch]" -type f -print0|xargs -0 sed -ri ':b s/\\$//;T;N;s/[ \t]*\n[ \t]*/ /;t b'
find . -name "*.[ch]" -type f -print0|xargs -0 sed -ri 's/^[ \t]*#define[ \t]+([a-zA-Z0-9_]+(\([^)]*\))?[ \t])[ \t]+/#define \1/'
find . -name "*.[ch]" -type f -print0|xargs -0 sed -ri ':b /\/\/|^[ \t]*$|#/b; N;/#|\n[ \t]*\/\/|\n$/b; /\/\*/{:c /\*\//b;N;b c}; s/[ \t]*\n[ \t]*/ /;b b'
# indent 5 times for stability
for i in $(seq 1 5); do indent include/* source/*; done
</source>

== Troubleshoot ==
=== Bug - comment line not indented with tabs ===
<source lang=c>
int main(void)
{
// This line indented with space should be indented with tabs
helloWorld();

return 0;
}
</source>

=== Bug - operator[] break method alignment===
When <code>align_on_operator=true</code>, and <code>align_var_def_amp_style = 0</code>:
<source lang=c>
class Graph {
iterator end();
Tree * GetSpanningTree(void);
Node & operator[] (int id); // Break alignment
size_t size();
int Squeeze();
};
</source>

=== Uncrustify 0.65 - buggy sp_balance_nested_parens ===
<source lang="c">
srand(time(NULL)); // before
srand(time(NULL) ); // after - with sp_paren_paren = force, sp_balance_nested_paren = true
</source>
</source>
You need a Windows compiler, like Visual Studio Express.

Latest revision as of 13:05, 16 July 2021

Uncrustify is highly configurable source beautifier.

Reference

Build

Since v0.63, build with cmake. Instructions are given in the README:

mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
make                 
sudo make install    # or sudo cmake --install

Note that stuff like # or cmake --build . or cmake --build . --config Release don't seem to give good result (big binaries).

On Windows, the easiest is to install MSYS2 (http://www.msys2.org/), and then build like on Linux:

  • Recent Uncrustify only build on Windows 10. On Windows 7, we get:
System is unknown to cmake, create:
Platform/MINGW64_NT-6.1-7601 to use this system,
  • Install MSYS2.
  • Install necessary toolchain:
pacman -S make cmake gcc
  • Build Uncrustify:
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
make

Alternativeyly, install cmake for Windows, and build for Visual Studio or MinGW (old version):

# To build with latest visual Studio
md build
cd build
cmake ..
cmake --build . --config Release

# To build with MinGW toolchain
md build
cd build
cmake -G "MinGW Makefiles" ..          # Do twice if complains above sh.exe in path
cmake --build . --config Release

You need a Windows compiler, like Visual Studio Express, or MinGW.

Issues
  • Compilation issues on Mingw 4.8.1 — switch to MSYS2.

Windows binaries

SourceForge distributes binaries for Windows, much easier than the cmake/MSYS2/Visual mic-mac...

Tips

Canonical indent

The script below perform a canonical indentation as follows:

  • Force #define on one line, and remove excess whitespace
  • Force all code on one line, but
  • Respect LF in block comments or before/after line comment
  • Keep empty lines
  • Indent
find . -name "*.[ch]" -type f -print0|xargs -0 sed -ri ':b s/\\$//;T;N;s/[ \t]*\n[ \t]*/ /;t b'
find . -name "*.[ch]" -type f -print0|xargs -0 sed -ri 's/^[ \t]*#define[ \t]+([a-zA-Z0-9_]+(\([^)]*\))?[ \t])[ \t]+/#define \1/'
find . -name "*.[ch]" -type f -print0|xargs -0 sed -ri ':b /\/\/|^[ \t]*$|#/b; N;/#|\n[ \t]*\/\/|\n$/b; /\/\*/{:c /\*\//b;N;b c}; s/[ \t]*\n[ \t]*/ /;b b'
# indent 5 times for stability
for i in $(seq 1 5); do indent include/* source/*; done

Troubleshoot

Bug - comment line not indented with tabs

int main(void)
{
    // This line indented with space should be indented with tabs
	helloWorld();

	return 0;
}

Bug - operator[] break method alignment

When align_on_operator=true, and align_var_def_amp_style = 0:

class Graph {
    iterator end();
    Tree *   GetSpanningTree(void);
    Node & operator[] (int id);            // Break alignment
    size_t size();
    int    Squeeze();
};

Uncrustify 0.65 - buggy sp_balance_nested_parens

srand(time(NULL));        // before
srand(time(NULL) );        // after  - with sp_paren_paren = force, sp_balance_nested_paren = true