Clang

Posted by wsxq2 on 2021-08-22
TAGS:  Clang

本文最后一次编辑时间:2021-10-15 15:26:05 +0800

官方文档:Welcome to Clang’s documentation! — Clang 13 documentation

使用 Clang 交叉编译

遇到过的问题

  • How to know current -sysroot value when use gcc? 使用编译选项 -print-sysroot 即可(输出为空表示未设置,使用系统自带的编译工具链)
  • error: non-void function ‘Check_MainProcess’ should return a value [-Wreturn-type]?加编译选项 -Wno-return-type 即可
  • error: option ‘-fno-delete-null-pointer-checks’ not supported? 删除该选项即可

clang-analyzer

scan-build

参见 scan-build: running the analyzer from the command line

1
2
scan-build -v -v --use-cc x86_64-linux-gnu-gcc make ARCH=x86_64 NPROCS=1 <otherargs> &> a.log
scan-view --host=0.0.0.0 --no-browser --allow-all-hosts /tmp/scan-build-2021-08-24-183903-7534-1

CodeChecker

参见 CodeChecker: running the analyzer from the command line

clang 工具

clang-format

参见 ClangFormat — Clang 13 documentation

clang-check

参见 ClangCheck — Clang 13 documentation

基本使用方法:

1
clang-check `cat filelist.txt` |& tee a.log

clang-check 的关键在于 compile_commands.json 的构建和调整,对于某些构建系统,可能需要调整其中的 commands 部分:

1
2
%s#aarch64-linux-gnu-gcc#clang -Wno-return-type  --target=aarch64-linux-gnu --sysroot=/mnt/build/crosstools/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu/aarch64-linux-gnu/libc/#
%s/-fno-delete-null-pointer-checks//g

另外,clang 不支持全局的寄存器变量声明(目前使用的解决方法是注掉),如这样的:

1
register unsigned long current_stack_pointer asm ("sp");

但查看官方文档后才知道,上述的应该是支持的,不支持的是非只读变量(sp之外的寄存器)。而这里不支持的原因应该是 clang 版本较低导致的(3.4.2)

clang only supports global register variables when the register specified is non-allocatable (e.g. the stack pointer). Support for general global register variables is unlikely to be implemented soon because it requires additional LLVM backend support.

——引用自 Clang Compiler User’s Manual — Clang 13 documentation

clangd

1
clangd --check=a.c

clang-tidy

链接

下面总结了本文中使用的所有链接: