c++/c源代码查看辅助工具
unifdef
https://docs.oracle.com/cd/E75432_01/html/E71066/unifdef-1.html
-
解析删除C程序源中经过ifdef的行,非系统自带要安装
-
命令格式:unifdef [-clt] [-Dname] [-Uname] [-iDname] [-iUname] … [filename]
- –c 正常操作补充。将被删除或留空的行会得到保留,反之亦然
- –l 使用空白行代替删除的行
- –t 纯文本选项。unifdef禁止尝试识别注释、单引号和双引号
- –Dname 与定义符号name相关联的行(行为类似#define name为true)
- –Uname 与未定义符号name相关联的行(行为类似#define name为false)
- –iDname 忽略但输出与定义符号name相关联的行。如果要使用ifdef来分隔非C行,如构建中的注释或代码,那么必须为指明将用于该目的的符号,使其不会解析其中的引号和注释
- –iUname 忽略但输出与未定义符号name相关联的行
-
不能识别编译器预制宏定义(例如
__linux__
),全靠用户自己指定宏是否定义
stripcc
http://stripcc.sourceforge.net/stripcc_cn.html
- 非系统自带,需要源码安装
- stripcc工作原理
- 在要处理的代码(比如*.c/*.h)中合适的位置插入gcc扩展的预编译头#warning
- 进而编译整个项目获取gcc编译输出并进行分析
- 根据分析结果确定某段代码是否使用,使用则保留,未使用则去除
- 命令选项:
- -c 用来指定项目编译使用的命令,默认是“make”,如果您的项目也是“make”编译,则可略过“-c”选项,如果是其他,比如“make vmlinux”则需要指定“ -c “make vmlinux”
- -m 用来指定执行编译命令(如“make”)的目录,这对一些不是在顶层目录执行编译命令的项目非常有用
- -n 让stripcc在处理完代码之后直接退出,而不执行验证过程,用户可以手动敲入“make”等项目编译命令自行验证
- demo以googletest-release-1.5.0为例
- 在googletest-release-1.5.0\make目录下,运行make
- 返回googletest-release-1.5.0目录运行stripcc -m “make”
// googletest-release-1.5.0\src\gtest-internal-inl.h处理前片段
#if !GTEST_IMPLEMENTATION_
// A user is trying to include this from his code - just say no.
#error "gtest-internal-inl.h is part of Google Test's internal implementation."
#error "It must not be included except by Google Test itself."
#endif // GTEST_IMPLEMENTATION_
#ifndef _WIN32_WCE
#include <errno.h>
#endif // !_WIN32_WCE
#include <stddef.h>
#include <stdlib.h> // For strtoll/_strtoul64/malloc/free.
#include <string.h> // For memmove.
#include <algorithm>
#include <string>
#include <vector>
#include <gtest/internal/gtest-port.h>
#if GTEST_OS_WINDOWS
#include <windows.h> // For DWORD.
#endif // GTEST_OS_WINDOWS
#include <gtest/gtest.h> // NOLINT
#include <gtest/gtest-spi.h>
// googletest-release-1.5.0\src\gtest-internal-inl.h处理后对应片段
#include <errno.h>
#include <stddef.h>
#include <stdlib.h> // For strtoll/_strtoul64/malloc/free.
#include <string.h> // For memmove.
#include <algorithm>
#include <string>
#include <vector>
#include <gtest/internal/gtest-port.h>
#include <gtest/gtest.h> // NOLINT
#include <gtest/gtest-spi.h>
cppinsights
- 去掉语法糖,展示编译器实际代码