Skip to content

Tool to comment out all function (and method) implementations in specific files. Also tutorial for clang's libtooling for source to source transformation.

License

Notifications You must be signed in to change notification settings

mugiseyebrows/clang-mute

Repository files navigation

clang-mute

Tool to comment out all function (and method) implementations in specific files.

Also tutorial for clang's libtooling for source to source transformation.

echo 'void f(){ int x=1; }' > main.cpp
clang-mute main.cpp 
cat main.cpp
void f(){
#if 0
 int x=1; 
#endif
}

Use

clang-mute target1.cpp [target2.cpp] [-- -Iincludepath -Ddefinition]

To use on real project, for example qt application, you need to create compilation database or compile flags file to provide all necessary includepaths to the tool.

Sample session

git clone https://github.com/mugiseyebrows/cmake-qt-sample.git
cd cmake-qt-sample
mkdir build
cd build
# to fix file not found errors add missing includepaths to CMAKE_CXX_FLAGS variable
cmake -G Ninja -D CMAKE_CXX_FLAGS="-I/usr/lib/gcc/x86_64-linux-gnu/11/include" ..
# to generate ui_*.h files compile the app
ninja app
cmake . -DCMAKE_EXPORT_COMPILE_COMMANDS=1
clang-mute ../mainwindow.cpp

As alternative to compilation database you can specify includepaths and other compiler flags after --

clang-mute ../mainwindow.cpp -- -DQT_CORE_LIB -DQT_GUI_LIB -DQT_NO_DEBUG -DQT_WIDGETS_LIB -I/home/overloop/cmake-qt-sample/build -I/home/overloop/cmake-qt-sample -I/home/overloop/cmake-qt-sample/build/app_autogen/include -isystem /usr/include/x86_64-linux-gnu/qt5 -isystem /usr/include/x86_64-linux-gnu/qt5/QtWidgets -isystem /usr/include/x86_64-linux-gnu/qt5/QtGui -isystem /usr/include/x86_64-linux-gnu/qt5/QtCore -isystem /usr/lib/x86_64-linux-gnu/qt5/mkspecs/linux-g++ -I/usr/lib/gcc/x86_64-linux-gnu/11/include -fPIC

You can store flags in compile_flags.txt file, one per line.

echo -DQT_CORE_LIB -DQT_GUI_LIB -DQT_NO_DEBUG -DQT_WIDGETS_LIB -I/home/overloop/cmake-qt-sample/build -I/home/overloop/cmake-qt-sample -I/home/overloop/cmake-qt-sample/build/app_autogen/include -isystem /usr/include/x86_64-linux-gnu/qt5 -isystem /usr/include/x86_64-linux-gnu/qt5/QtWidgets -isystem /usr/include/x86_64-linux-gnu/qt5/QtGui -isystem /usr/include/x86_64-linux-gnu/qt5/QtCore -isystem /usr/lib/x86_64-linux-gnu/qt5/mkspecs/linux-g++ -I/usr/lib/gcc/x86_64-linux-gnu/11/include -fPIC | tr ' ' '\n' > compile_flags.txt
clang-mute ../mainwindow.cpp

Build

To compile you need llvm-project distribution which you can git clone from repository or curl from llvm-project releases.

See also: build.sh file and build.bat file.

Ubuntu

sudo apt install build-essential cmake ninja-build git
git clone --depth 1 https://github.com/llvm/llvm-project.git
cd llvm-project/clang/tools
git clone https://github.com/mugiseyebrows/clang-mute.git
echo "add_clang_subdirectory(clang-mute)" >> CMakeLists.txt
cd ../..
mkdir build
cd build
cmake -DLLVM_ENABLE_PROJECTS="clang" -DCMAKE_BUILD_TYPE=Release -G Ninja ../llvm
cmake --build . --target clang-mute
sudo cp bin/clang-mute /usr/bin

Windows

(Common steps for MinGW and Visual Studio)

  • Install ninja
  • Add ninja to %PATH%
  • Clone sources
git clone --depth 1 https://github.com/llvm/llvm-project.git
cd llvm-project\clang\tools
git clone https://github.com/mugiseyebrows/clang-mute.git
echo add_clang_subdirectory(clang-mute) >> CMakeLists.txt
cd ..\..
mkdir build
cd build

Visual Studio

  • Use visual studio command prompt or call vcvars64.bat
  • Build using cmake
call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvars64.bat"
cmake -DLLVM_ENABLE_PROJECTS="clang" -DCMAKE_BUILD_TYPE=Release -G Ninja ../llvm
cmake --build . --target clang-mute

MinGW

  • Add compiler to %PATH%
  • Build using cmake
cmake -DLLVM_ENABLE_PROJECTS="clang" -DCMAKE_BUILD_TYPE=Release -G Ninja -D CMAKE_C_COMPILER=gcc -D CMAKE_CXX_COMPILER=g++ ../llvm
cmake --build . --target clang-mute

See also

post on medium

About

Tool to comment out all function (and method) implementations in specific files. Also tutorial for clang's libtooling for source to source transformation.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published