版權(C)2023 Jiacai Cui [email protected]
這是一個簡單的CPP靜態分析框架,在南京大學的軟件工程實驗室課程中開發了。
該項目是個人的,並根據GNU通用公共許可證分發。
該項目的設計受到了Tai-E的啟發,Tai-E是Java的靜態分析框架,請查看其技術報告以獲取更多詳細信息。
該CPP靜態分析儀使用Clang作為前端。因此,您需要在本地系統上安裝LLVM和Clang,以構建和運行此項目。它是根據LLVM 16.0.2開發的,根據LLVM 16.0.2和17.0.0進行了測試,但最新版本也應該是可以的。
建議使用預編譯的二進製文件安裝LLVM,而不是手動構建。
這是設置該項目適當環境的方法。
使用自製者作為軟件包經理,運行
brew install cmake ninja llvm doxygen然後,檢查您已安裝的CMAKE,NINJA,LLVM和CLANG的版本
cmake --version
ninja --version
llvm-config --version
clang --version
doxygen --version使用LLVM APT源,運行
sudo apt update
sudo apt install lsb-release wget software-properties-common gnupg zlib1g zlib1g-dev git cmake ninja-build build-essential doxygen graphviz
wget https://apt.llvm.org/llvm.sh
chmod +x llvm.sh
./llvm.sh 17 all然後,檢查您已安裝的CMAKE,NINJA,LLVM和CLANG的版本
cmake --version
ninja --version
clang-17 --version
llvm-config-17 --version
doxygen --versiongit clone https://github.com/JacyCui/cpp-static-analyzer.git在項目根目錄中,運行
mkdir build
cd build
cmake -G=Ninja ..
ninja編譯後,在項目根目錄中運行
./build/tests/tests然後,您應該看到以下類似的內容,這意味著傳遞了627個測試斷言。
# a lot of log information here ...
===============================================================================
[doctest] test cases: 35 | 35 passed | 0 failed | 0 skipped
[doctest] assertions: 627 | 627 passed | 0 failed |
[doctest] Status: SUCCESS !編譯後,在項目根目錄中運行
./build/tools/reaching-definition-analyzer --source-dir=resources/dataflow/ReachDef這將運行resources/dataflow/ReachDef目錄中所有源文件的到達定義分析。
./build/tools/reaching-definition-analyzer --help
A Simple CPP Reaching Definition Static Analyzer
Copyright (c) 2023-2023
Usage: ./build/tools/reaching-definition-analyzer/reaching-definition-analyzer [OPTIONS]
Options:
-h,--help Print this help message and exit
-S,--source-dir TEXT REQUIRED
directory of all source files
-I,--include-dir TEXT directory of all header files
--std,--standard TEXT c++ language standard (support all standards that clang supports)同樣,在項目根目錄中,運行
./build/tools/live-variable-analyzer --source-dir=resources/dataflow/LiveVar這將運行resources/dataflow/LiveVar目錄中所有源文件的實時變量分析。
./build/tools/live-variable-analyzer --help
A Simple CPP Live Variable Static Analyzer
Copyright (c) 2023-2023
Usage: ./build/tools/live-variable-analyzer [OPTIONS]
Options:
-h,--help Print this help message and exit
-S,--source-dir TEXT REQUIRED
directory of all source files
-I,--include-dir TEXT directory of all header files
--std,--standard TEXT c++ language standard (support all standards that clang supports)STEP01 :將此存儲庫作為項目存儲庫的子模塊。
git submodule add https://github.com/JacyCui/cpp-static-analyzer.git path/to/put/this/project步驟02 :鏈接到您的CMakeLists.txt中的libanalyzer 。
# suppose your target is called your_target
add_subdirectory ( path /to/put/this/project)
target_include_directories (your_target
PUBLIC path /to/put/this/project/ include
)
target_link_libraries (your_target
libanalyzer
)步驟03 :使用源代碼中提供的API。在達到定義的測試中提供了一個示例用法。
您可以通過運行在構建目錄中本地構建HTML Doxygen API文檔
# in the build directory
ninja libanalyzer-api-doc並且您會找到位於build/docs/api-doc/html目錄的HTML文檔。
您可以通過在本地Web瀏覽器中打開build/docs/api-doc/html/index.html來閱讀它。
請注意,該文檔不包括在默認構建目標中。
如果需要閱讀,則必須像上面一樣明確構建它。