Clean Code

_Uncrustify_C++

Player_blue 2024. 5. 29. 16:19

Uncrustify는 내가 작성한 C++코드를 지정한 서식에 맞게 재작성 해준다.

Ubuntu에서의 설치법은 간단하며, 대부분의 경우 설치가 되어있다.

 

참고 사이트:

https://github.com/uncrustify/uncrustify

 

GitHub - uncrustify/uncrustify: Code beautifier

Code beautifier. Contribute to uncrustify/uncrustify development by creating an account on GitHub.

github.com

https://uncrustify.sourceforge.net/

 

Uncrustify - Source Code Beautifier for C-like languages

Uncrustify Source Code Beautifier for C, C++, C#, ObjectiveC, D, Java, Pawn and VALA Introduction The goals of this project are simple: Create a highly configurable, easily modifiable source code beautifier. Features Indent code, aligning on parens, assign

uncrustify.sourceforge.net

 

Option 참고:

https://chromium.googlesource.com/external/github.com/openthread/wpantund/+/d013d22dcf7610375da6abf57cbe2302f0f1712e/.uncrustify.cfg

 

.uncrustify.cfg - external/github.com/openthread/wpantund - Git at Google

 

chromium.googlesource.com

 

설치 방법: 

$ sudo apt install uncrustify

 

uncrustify.cfg를 생성하고 설정을 한 다음에 Terminal에 다음의 명령어를 치면 수정된 파일이 출력된다.

-c는 uncrustify.cfg 파일 지정을 --replace 옵션은 원본 파일을 수정한다는 의미이다.

$ uncrustify -c uncrustify.cfg --replace example.cpp

 

vscode를 사용한다면 Uncrustify에 대해 도움을 받을 수 있는 extention도 있다.

해당 extention은 vscode 내에서 Uncrustify가 가능하도록 도와준다.

vscode extention

 

이제 내 소스코드가 있는 폴더에 uncrustify.cfg 파일을 하나 생성해보자.

내부 내용은 위의 참고 사이트에서 참고 하였다.

 

일단 내용을 살펴봤을때, indent, brace등에 대한 서식 맞추기만 가능한 것 같다.

 

uncrustify.cfg 간단한 예제:

## C++에 대한 Uncrustify 설정 예시

# indent options
indent_with_tabs = 0  # 0: space, 1: tab
indent_columns = 2  # number of space
input_tab_size = 4  # select tab size

indent_namespace = 0  # false / true
indent_class = 1  # false / true

# brace
nl_if_brace = remove  # ignore/add/remove/force
nl_else_brace = remove
nl_for_brace = remove
nl_do_brace = remove
nl_while_brace = remove

sp_before_sparen = force # ignore/add/remove/force "("
sp_else_brace = force # ignore/add/remove/force "{"

sp_after_ptr_star = force
sp_before_ptr_star = force  # add & force?