GithubHelp home page GithubHelp logo

shining1984 / clang-plugins-demo Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nsumner/clang-plugins-demo

0.0 0.0 0.0 19 KB

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

License: Other

C++ 76.03% C 2.20% CMake 21.77%

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.

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.