Showing posts with label llvm. Show all posts
Showing posts with label llvm. Show all posts

Friday, November 29, 2013

How to build and setup LLVM Scan Analyzer for Linaro Toolchain

Downloading and Installing Clang-Analyzer

Download LLVM, Clang, Clang-tools-extra and Compiler-RT from - http://llvm.org/releases/download.html.

  1. Extract the LLVM tarball. I'm going to use the variable LLVM to point to this directory going forward.
  2. Go to $LLVM/tools/src. Extract the clang release tarball. Rename the extracted directory as clang
  3. Go to $LLVM/tools/src/clang/tools/. Extract the clang-tools-extra and rename it as extra.
  4. Go to $LLVM/projects/. Extract the compiler-rt tarball and rename it as compiler-rt.
Note! - If you don't extract the clang, clang-tools-extra and compiler-rt in the respectively directories, it will not build them by default.

For more information look at http://clang.llvm.org/get_started.html. The instructions are for svn but they apply for the release tarballs as well.

Building and Installing Clang

Go to $LLVM directory and then you can configure, build and install clang.
 $ ./configure $ make $ make install
If you want to install clang in a specific directory then pass --prefix= to configure. Usually, I use /proj/staging/ to stage my builds and so, I usually build as ./configure --prefix=/proj/staging/llvm

Note! make install will not install scan-build and scan-view. They remain in $LLVM/tools/clang/tools/scan-build and $LLVM/tools/clang/tools/scan-view. So you will need to add these directories into your path.

Downloading and Installing Linaro Toolchain

Download Linaro Toolchain from - https://launchpad.net/linaro-toolchain-binaries/. Extract it to a directory of choice. I usually place it in /proj/staging/linaro-gcc. Next add the /proj/staging/linaro-gcc/bin directory to your PATH variable.

Running clang-analyzer with linaro toolchain

Now follow usual steps to cross compile your program. To enable scan-build make sure you run make via scan-build as shown in my previous post on running clang-analyzer.

Monday, February 25, 2013

static analysis with llvm

Using Scan Build

LLVM comes with a static analyzer - http://clang-analyzer.llvm.org. It works by overriding CC. So, to use it with autoconf'ed project, you need to first run it when running configure and then make. On Ubuntu you can always apt-get it, but if you are interested in building from source then take a look at my other post.
$ scan-build ./configure
$ scan-build make
In some case, esp. if you are building from source, scan-build may not find clang. So you will need to explicitly, pass the path to clang to scan-build. E.g. if clang is installed (via make install) at /proj/staging/llvm, then you will need to pass --use-analyzer to scan-build.

$ scan-build --use-analyzer=/proj/staging/llvm/bin/clang ./configure
$ scan-build --use-analyzer=/proj/staging/llvm/bin/clang make

Integrating Clang Static Analyzer into Makefile.am

Alex has a different approach to running scan build. In this post, he shows how to include a step to run the analyzer in every build. This is really neat because there is no change to your standard build flow. Every time you run make, it automatically calls the analyzer (or not if you don't include the step to all-local).

The flip side is that you need to use clang as the compiler only and not gcc or any other compiler. 

More Details on Clang Static Analyzer

More details on LLVM's static analyzer is available at http://clang-analyzer.llvm.org/scan-build.html.

Example of a Bug that Clang Static Analyzer found

I ran scan-build against wayland source some time back and caught a bug immediately - https://bugs.freedesktop.org/show_bug.cgi?id=61385.

To incorporate Alex's idea, a small patch to src/Makefile.am is required. Just add the following changes to Wayland's src/Makefile.am and then rebuild. Note that you must use clang as the compiler in this case.

+analyzer_srcs =                                      \
+    $(filter %.c, $(libwayland_util_la_SOURCES))     \
+    $(filter %.c, $(libwayland_server_la_SOURCES))   \
+    $(filter %.c, $(libwayland_client_la_SOURCES))
+
+analyzer_plists = $(analyzer_srcs:%.c=%.plist)
+
+$(analyzer_plists): %.plist: %.c
+       @echo "  CCSA  " $@
+       @$(COMPILE) --analyze $< -o $@
+
+all-local: $(analyzer_plists)
+
+MOSTLYCLEANFILES = $(analyzer_plists)