LibToolingã使ã£ã¦ãã¼ã«ãä½ã£ã¦ã¿ãã
ãã1æååéããã®ãâ¦ï¼
LLVM IRçæã®ã©ã¤ãã©ãªãLibToolingã«ããã¿ããã ã£ãã®ã§ï¼ä½¿ã£ã¦ã¿ããã¨ã«ãã¾ããï¼
homebrewã§å
¥ããã®ã ã¨ãã«ããç´ãã®ã©ãããããããããããªããªã£ãã®ã§ï¼ç°å¢æ§ç¯ããç´ããã¨ã«ãã¾ããâ¦ï¼
ç°å¢æ§ç¯ã¯ããã§æå¾ã«ãããï¼
åå ¥ã£ã¦ãã®ãæ¶ãï¼
% brew uninstall llvm
ï¼ãã£ããå
ã¯æ¬ã«è¼ã£ã¦ãéãã«ãã£ãã ãï¼
ã½ã¼ã¹ã³ã¼ãåå¾ãã¦ãã¦æ£ãããã£ã¬ã¯ããªã«é
ç½®ï¼
% mkdir ~/llvm34
% cd ~/llvm34
% svn co http://llvm.org/svn/llvm-project/llvm/tags/REREASE_34/final llvm
% cd ~/llvm34/llvm/tools
% svn co http://llvm.org/svn/llvm-project/cfe/tags/REREASE_34/final clang
% cd ~/llvm34/llvm/projects
% svn co http://llvm.org/svn/llvm-project/compiler-rt/tags/REREASE_34/final compiler-rt
CMakeã§ãã«ã
% mkdir ~/llvm34/build_ninja
% cd ~/llvm34/build_ninja
% cmake -G Ninja ../llvm
% ninja
.zshrcã«PATHè¨å®
ããã§ç°å¢æ§ç¯ã¯ã§ãã¾ããï¼makeã«æ¯ã¹ã¦ninjaæ©ãã£ãã§ãï¼
ãã¼ã«ãä½ã£ã¦ã¿ã
ãã¦ï¼æ¬é¡ã®LibToolingã使ã£ããã¼ã«ãä½ã£ã¦ã¿ã¾ãï¼
Tutorial for building tools using LibTooling and LibASTMatchers â Clang 3.4 documentation
ãã®ãã¼ã¸ã® Step 1: Create a ClangTool ã«æ²¿ã£ã¦ããã¾ããï¼
% cd ~/llvm34/llvm/tools/clang
% mkdir tools/extra/loop-convert
% echo 'add_subdirectory(loop-convert)' >> tools/extra/CMakeLists.txt
/llvm34/llvm/tools/clang/tools/extra/CMakeLists.txt ã«
add_subdirectory(tool-template)
ã追å ãããï¼
ããã§è¿½å ããloop-convertãã£ã¬ã¯ããªã®ã½ã¼ã¹ã³ã¼ãããã«ãããããã«æå®ãã¦ããï¼
次ã«ï¼CMakeLists.txtãä½ã
% vim tools/extra/loop-convert/CMakeLists.txt
CMakeLists.txtã®ä¸èº«ã¯ï¼
set(LLVM_LINK_COMPONENTS support) set(LLVM_USED_LIBS clangTooling clangBasic clangAST) add_clang_executable(loop-convert LoopConvert.cpp ) target_link_libraries(loop-convert clangTooling clangBasic clangASTMatchers )
æå¾ã«ï¼tools/extra/loop-convert/LoopConvert.cppãä½ãï¼
// Declares clang::SyntaxOnlyAction. #include "clang/Frontend/FrontendActions.h" #include "clang/Tooling/CommonOptionsParser.h" #include "clang/Tooling/Tooling.h" // Declares llvm::cl::extrahelp. #include "llvm/Support/CommandLine.h" using namespace clang::tooling; using namespace llvm; // CommonOptionsParser declares HelpMessage with a description of the common // command-line options related to the compilation database and input files. // It's nice to have this help message in all tools. static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage); // A help message for this specific tool can be added afterwards. static cl::extrahelp MoreHelp("\nMore help text..."); int main(int argc, const char **argv) { CommonOptionsParser OptionsParser(argc, argv); ClangTool Tool(OptionsParser.getCompilations(), OptionsParser.getSourcePathList()); return Tool.run(newFrontendActionFactory<clang::SyntaxOnlyAction>()); }
ã§ï¼ãã«ããã£ã¬ã¯ããªã«ãã£ã¦ï¼ãã«ãããï¼
% cd ~/build_ninja/build
% ninja
ããã§ï¼llvm34/build_ninja/binã«loop-convertã追å ããã¦ããï¼
ä½ã£ããã¼ã«ã使ããããã«ãªãã¾ããï¼
使ã£ã¦ã¿ã¾ãï¼
% cat "int main() { return 0; }" > test.cpp
% bin/loop-convert test.cpp --
ä¸ã®ã³ãã³ãã£ã¦ä½ã表ãã¦ããã ããâ¦ï¼
ã¨ã©ã¼åºããã©ï¼test.cppã«æ¸ãè¾¼ãã§ãã¨ããèªèã§ããã®ããªâ¦ï¼
使ãã¦ãã®ãâ¦â¦ï¼
ã¨ããããï¼ãããªãããã§CMakeFileä½ã£ã¦ãã«ãããã°ãããã ãªã¨ãããã¨ã¯ãããã¾ããã
ãã¾ã
ä»å使ãããã£ãã®ã¯LibToolingã§ããï¼åå試ããlibClangã®python bindingã使ã£ã¦ã¿ã¾ããï¼
PYTHONPATHã¨LD_LIBRARY_PATHãè¨å®
% export PYTHONPATH=~/llvm34/llvm/tools/clang/bindings/python
% export LD_LIBRARY_PATH=$(llvm-config --libdir):$LD_LIBRARY_PATH
ããã§ï¼ç¡äºãã¾ãããã¾ããï¼