GithubHelp home page GithubHelp logo

nsumner / clang-plugins-demo Goto Github PK

View Code? Open in Web Editor NEW
72.0 3.0 11.0 19 KB

A simple example of defining custom plugins for clang and the clang static analyzer.

License: Other

CMake 21.77% C++ 76.03% C 2.20%
clang plugin ast static analyzer llvm

clang-plugins-demo's Introduction

These plugins and programs are demonstrations of how to create and use the plugin infrastructure of clang for analyzing ASTs or integrating with the clang static analyzer.

The AST plugin prints out the names of functions found when parsing a file. The static analyzer plugin is the SimpleStreamChecker from the clang code base (as well as How to write a checker in 24 hours by Anna Zaks and Jordan Rose). It identifies issues in handling FILEs in C program. Neither plugin is a contribution here. This project just provides an example of how to integrate such plugins into a standalone project.

Building with CMake

  1. Clone the demo repository.

     git clone https://github.com/nsumner/clang-plugins-demo.git
    
  2. Create a new directory for building.

     mkdir build
    
  3. Change into the new directory.

     cd build
    
  4. Run CMake with the path to the LLVM source.

     cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=True \
         -DLLVM_DIR=</path/to/LLVM/build>/lib/cmake/llvm/ \
         ../clang-plugins-demo
    
  5. Run make inside the build directory:

     make
    

This produces standalone tools called bin/print-functions and bin/runstreamchecker. Loadable plugin variants of the analyses are also produced inside lib/.

Note, building with a tool like ninja can be done by adding -G Ninja to the cmake invocation and running ninja instead of make.

Running

Both the AST plugins and clang static analyzer plugins can be run via standalone programs or via extra command line arguments to clang. The provided standalone variants can operate on individual files or on compilation databases, but compilation databases are somewhat easier to work with.

AST Plugins

To load and run AST plugins dynamically in clang, you can use:

    clang -fplugin=lib/libfunction-printer-plugin.so -c ../test/functions.c

To run the plugin via the standalone program:

    bin/print-functions -- clang -c ../test/functions.c

Note that this will require you to put the paths to all headers in the command line (using -I) or they will not be found. It can be simpler to instead use a compilation database:

    bin/print-functions -p compile_commands.json

Clang Static Analyzer Plugins

To load and run a static analyzer plugin dynamically in clang, use:

    clang -fsyntax-only -fplugin=lib/libstreamchecker.so \
      -Xclang -analyze -Xclang -analyzer-checker=demo.streamchecker \
      ../clang-plugins-demo/test/files.c

To run the plugin via the standalone program:

    bin/runstreamchecker -- clang -c ../clang-plugins-demo/test/files.c

Again, missing headers are likely, and using a compilation database is the preferred and simplest way to work around this issue. Note that clang comes with scripts that can build a compilation database for an existing project.

Licenses

The code for this project is released under an MIT License (included in the LICENSE file in the root directory of the project).

The code in SimpleStreamChecker.cpp comes from the clang project and is released under the University of Illinois/NCSA Open Source License.

clang-plugins-demo's People

Contributors

nsumner avatar

Stargazers

zhangpf avatar Jack Ren avatar Urs Hanselmann avatar  avatar  avatar DK_028 avatar kenluobo avatar  avatar Andrey Shcheglov avatar PiotrB avatar Chong Zhao avatar canliture avatar  avatar Vanellope avatar Shangzhi-Xu avatar Bill Sun avatar Oliver Killane avatar Chris Calo avatar bomo avatar  avatar Jinhan Kwak avatar Max avatar  avatar Hoshi Ran avatar Wang Peiran avatar  avatar  avatar ditto avatar Diogo Behrens avatar Lambda λ avatar  avatar Ryosuke Ito avatar Karno avatar Chaoshuai Li avatar dliang avatar Theodor Arsenij avatar void0red avatar x213212 avatar Georgii Buzykin avatar Yuri Gribov avatar Lime avatar  avatar  avatar Андрей avatar Haoyun Ren avatar Xummer avatar Xuzhou Zheng avatar boyuan avatar Derek avatar  avatar Jevin Sweval avatar savi2w avatar  avatar Zhenpeng Lin avatar Stefan Novak avatar Henri Hyyryläinen avatar Yushan avatar  avatar sw avatar Keyur avatar Alex Wang avatar  avatar Las avatar Harunene avatar Jordan Henkel avatar Sebastian Oeste avatar Marcus R. Brown avatar  avatar Paul Fultz II avatar ryan avatar Miguel A. Arroyo avatar Aditya Basu avatar

Watchers

Jevin Sweval avatar  avatar Ningning Shi(史宁宁) avatar

clang-plugins-demo's Issues

cmake failed

Hello, thank you for your code. But when I do as what you said, I got error, I don't know why.
This is the log.
➜ build cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=True -DLLVM_DIR=~/code/llvm_xcode/lib/cmake/llvm ..
-- Found LLVM 10.0.0svn
-- Using LLVMConfig.cmake in: ~/code/llvm_xcode/lib/cmake/llvm
-- Linker detection: ld64
LLVM STATUS:
Definitions -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS
Includes /Users/wangao/code/llvm-project/llvm/include;/Users/wangao/code/llvm_xcode/include
STATIC_INCLUDES-NOTFOUND;/Users/wangao/code/llvm_xcode/tools/clang/include
Libraries /Users/wangao/code/llvm_xcode/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)/lib
CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
STATIC_INCLUDES
used as include directory in directory /Users/wangao/Desktop/clang-plugins-demo-master
used as include directory in directory /Users/wangao/Desktop/clang-plugins-demo-master
used as include directory in directory /Users/wangao/Desktop/clang-plugins-demo-master/lib
used as include directory in directory /Users/wangao/Desktop/clang-plugins-demo-master/lib/function-printer
used as include directory in directory /Users/wangao/Desktop/clang-plugins-demo-master/lib/function-printer
used as include directory in directory /Users/wangao/Desktop/clang-plugins-demo-master/lib/function-printer
used as include directory in directory /Users/wangao/Desktop/clang-plugins-demo-master/lib/streamchecker
used as include directory in directory /Users/wangao/Desktop/clang-plugins-demo-master/lib/streamchecker
used as include directory in directory /Users/wangao/Desktop/clang-plugins-demo-master/tools
used as include directory in directory /Users/wangao/Desktop/clang-plugins-demo-master/tools/print-functions
used as include directory in directory /Users/wangao/Desktop/clang-plugins-demo-master/tools/print-functions
used as include directory in directory /Users/wangao/Desktop/clang-plugins-demo-master/tools/runstreamchecker
used as include directory in directory /Users/wangao/Desktop/clang-plugins-demo-master/tools/runstreamchecker

-- Configuring incomplete, errors occurred!
See also "/Users/wangao/Desktop/clang-plugins-demo-master/build/CMakeFiles/CMakeOutput.log".

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.