Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
LLVM Cookbook

You're reading from   LLVM Cookbook Over 80 engaging recipes that will help you build a compiler frontend, optimizer, and code generator using LLVM

Arrow left icon
Product type Paperback
Published in May 2015
Publisher
ISBN-13 9781785285981
Length 296 pages
Edition 1st Edition
Tools
Arrow right icon
Toc

Table of Contents (11) Chapters Close

Preface 1. LLVM Design and Use FREE CHAPTER 2. Steps in Writing a Frontend 3. Extending the Frontend and Adding JIT Support 4. Preparing Optimizations 5. Implementing Optimizations 6. Target-independent Code Generator 7. Optimizing the Machine Code 8. Writing an LLVM Backend 9. Using LLVM for Various Useful Projects Index

Using the Clang Static Analyzer


In this recipe, you will learn about the static analysis of code, which is carried out by the Clang Static Analyzer. It is built on top of Clang and LLVM. The static analysis engine used by the Clang Static Analyzer is a Clang library, and it has the capability to be reused in different contexts by different clients.

We will take the example of the divide-by-zero defect and show you how the Clang Static Analyzer handles this defect.

Getting ready

You need to build and install LLVM along with Clang.

How to do it…

Perform the following steps:

  1. Create a test file and write the test code in it:

    $ cat sa.c
    int func() {
    int a = 0;
    int b = 1/a;
    return b;
    }
    
  2. Run the Clang Static Analyzer by passing the command-line options shown in the following command, and get the output on the screen:

    $ clang -cc1 -analyze -analyzer-checker=core.DivideZero sa.c
    sa.c:3:10: warning: Division by zero
    int b = 1/a;
            ~^~
    1 warning generated.
    

How it works…

The static analyzer core performs...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime