Git Product home page Git Product logo

clang-tool's Introduction

clang-tool

clang-tool is a simple and powerful project template for clang-based tools using libtooling[1]. It helps getting started to write standalone tools useful for refactoring, static code analysis, auto-completion etc.

Installation

Install the necessary llvm and clang headers and libraries for you system an run:

git clone https://github.com/firolino/clang-tool
cd clang-tool
mkdir build
cd build
cmake ..
make

Usage

A simple example is included in src/transformers/functioncalltransformer.cc that rewriters all function calls from functionName() to fn_functionName().

bin/clang-tool ../examples/simple.cc -- -std=c++11

Another example can be found in finder/integervariablefinder.cc. Uncomment the intFinder in consumer.cc to print all integer variables.

FunctionCallTransformer fntransformer(context, rewriter);
fntransformer.start();
fntransformer.print(llvm::outs());

IntegerVariableFinder intFinder(context);
intFinder.start();

You can even specifiy more than one source file when calling clang-tool.

bin/clang-tool s1.cc s2.cc -- -std=c++11

More matchers for writing own finders and transformers are available under [2].

Specifiying compiler arguments

There are multiple ways to provide your projects' compiler arguments to a clang tool:

  • Direct

    As can be seen in the example above, compiler arguments can be placed directly after --.

  • Compilation Database

    If your project is CMake-based use -DCMAKE_EXPORT_COMPILE_COMMANDS=ON in your cmake call. It will generate a compile_commands.json in your build directory containing your compiler arguments etc. With the json file you don't need to provide -- anymore. clang-tool will automatically use your json file.

  • Makefiles

    If you have an ordinary Makefile based project, use rizsottos awesome tool from https://github.com/rizsotto/Bear and run bear make on a cleanded project directory. It will generate a compile_commands.json for you!

TODOs

Inlude PPCallbacks

References

[1] http://clang.llvm.org/docs/LibTooling.html

[2] http://clang.llvm.org/docs/LibASTMatchersReference.html

clang-tool's People

Contributors

firolino avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

clang-tool's Issues

build error

clang-tool/build$make
Scanning dependencies of target clang-tool
[ 11%] Building CXX object src/CMakeFiles/clang-tool.dir/main.cc.o
[ 22%] Building CXX object src/CMakeFiles/clang-tool.dir/utils/utils.cc.o
/var/tmp/clang/clang-tool/src/utils/utils.cc: In function 'bool utils::customRunToolOnCodeWithArgs(clang::FrontendAction*, const llvm::Twine&, const std::vector<std::__cxx11::basic_string >&, const llvm::Twine&, const FileContentMappings&)':
/var/tmp/clang/clang-tool/src/utils/utils.cc:28:116: error: no matching function for call to 'clang::tooling::ToolInvocation::ToolInvocation(std::vector<std::__cxx11::basic_string >, clang::FrontendAction*&, clang::FileManager*)'
28 | clang::tooling::ToolInvocation invocation(getSyntaxOnlyToolArgs(args, fileNameRef), frontendAction, files.get());
| ^
In file included from /var/tmp/clang/clang-tool/src/utils/utils.h:11,
from /var/tmp/clang/clang-tool/src/utils/utils.cc:1:
/usr/lib/llvm/11/include/clang/Tooling/Tooling.h:257:3: note: candidate: 'clang::tooling::ToolInvocation::ToolInvocation(std::vector<std::__cxx11::basic_string >, clang::tooling::ToolAction*, clang::FileManager*, std::shared_ptrclang::PCHContainerOperations)'
257 | ToolInvocation(std::vectorstd::string CommandLine, ToolAction Action,
| ^~~~~~~~~~~~~~
/usr/lib/llvm/11/include/clang/Tooling/Tooling.h:257:3: note: candidate expects 4 arguments, 3 provided
/usr/lib/llvm/11/include/clang/Tooling/Tooling.h:245:3: note: candidate: 'clang::tooling::ToolInvocation::ToolInvocation(std::vector<std::__cxx11::basic_string >, std::unique_ptrclang::FrontendAction, clang::FileManager
, std::shared_ptrclang::PCHContainerOperations)'
245 | ToolInvocation(std::vectorstd::string CommandLine,
| ^~~~~~~~~~~~~~
/usr/lib/llvm/11/include/clang/Tooling/Tooling.h:246:50: note: no known conversion for argument 2 from 'clang::FrontendAction*' to 'std::unique_ptrclang::FrontendAction'
246 | std::unique_ptr FAction, FileManager *Files,
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~
/usr/lib/llvm/11/include/clang/Tooling/Tooling.h:232:7: note: candidate: 'clang::tooling::ToolInvocation::ToolInvocation(const clang::tooling::ToolInvocation&)'
232 | class ToolInvocation {
| ^~~~~~~~~~~~~~
/usr/lib/llvm/11/include/clang/Tooling/Tooling.h:232:7: note: candidate expects 1 argument, 3 provided
make[2]: *** [src/CMakeFiles/clang-tool.dir/build.make:122: src/CMakeFiles/clang-tool.dir/utils/utils.cc.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:114: src/CMakeFiles/clang-tool.dir/all] Error 2
make: *** [Makefile:104: all] Error 2

Hello I tried to build your project but I reached an error.
Is there easy way to fix it?
I understand that a year and a half has passed since the publication and the clang api has changed, but still I cannot fix the error myself.

target pattern contains no `%'. Stop.

Running CMake 3.19.7 on macOS 11.1 (20C69). Have LLVM @ 4224a3 in the directory above which I built using CMake.

$ git clone https://github.com/firolino/clang-tool
cd clang-tool
mkdir build
cd build
cmake ..
make
Cloning into 'clang-tool'...
remote: Enumerating objects: 9, done.
remote: Counting objects: 100% (9/9), done.
remote: Compressing objects: 100% (9/9), done.
remote: Total 196 (delta 1), reused 3 (delta 0), pack-reused 187
Receiving objects: 100% (196/196), 28.97 KiB | 5.79 MiB/s, done.
Resolving deltas: 100% (93/93), done.
CMake Deprecation Warning at CMakeLists.txt:1 (cmake_minimum_required):
  Compatibility with CMake < 2.8.12 will be removed from a future version of
  CMake.

  Update the VERSION argument <min> value or use a ...<max> suffix to tell
  CMake that the project does not need compatibility with older versions.


-- The C compiler identification is AppleClang 12.0.0.12000032
-- The CXX compiler identification is AppleClang 12.0.0.12000032
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Deprecation Warning at src/CMakeLists.txt:1 (cmake_minimum_required):
  Compatibility with CMake < 2.8.12 will be removed from a future version of
  CMake.

  Update the VERSION argument <min> value or use a ...<max> suffix to tell
  CMake that the project does not need compatibility with older versions.


-- LLVM CXX flags: /bin/sh: LLVM_CONFIG_EXECUTABLE-NOTFOUND: command not found
-- LLVM LD flags:  
-- LLVM core libs: /bin/sh: LLVM_CONFIG_EXECUTABLE-NOTFOUND: command not found
-- Found LLVM: /bin/sh: LLVM_CONFIG_EXECUTABLE-NOTFOUND: command not found
-- Clang libs: 
-- Configuring done
-- Generating done
-- Build files have been written to: /muh/dir/fold/clang-llvm/clang-tool/build
src/CMakeFiles/clang-tool.dir/build.make:206: *** target pattern contains no `%'.  Stop.
make[1]: *** [src/CMakeFiles/clang-tool.dir/all] Error 2
make: *** [all] Error 2

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.