Code Analysis using Compiler Front-ends (Clang)

Slides:



Advertisements
Similar presentations
Software & Services Group, Developer Products Division Copyright© 2010, Intel Corporation. All rights reserved. *Other brands and names are the property.
Advertisements

Chapter3: Language Translation issues
Chapter 1: Introduction to Compiling
Reference Book: Modern Compiler Design by Grune, Bal, Jacobs and Langendoen Wiley 2000.
Chapter 2 A Simple Compiler
Compiler design Computer Science Rensselaer Polytechnic Lecture 1.
Initialization of Clang
COP4020 Programming Languages
Chapter 1. Introduction.
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
CSC 338: Compiler design and implementation
Compiler course 1. Introduction. Outline Scope of the course Disciplines involved in it Abstract view for a compiler Front-end and back-end tasks Modules.
CST320 - Lec 11 Why study compilers? n n Ties lots of things you know together: –Theory (finite automata, grammars) –Data structures –Modularization –Utilization.
1 COMP 3438 – Part II-Lecture 1: Overview of Compiler Design Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 8: Semantic Analysis and Symbol Tables.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
BUILD ON THE POLYGLOT COMPILER FRAMEWORK MIHAL BRUMBULLI 7th Workshop “SEERE” Montenegro-Risan 9-14 September 2007 SimJ Programming Language.
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Compiler design Lecture 1: Compiler Overview Sulaimany University 2 Oct
Introduction to Compilers. Related Area Programming languages Machine architecture Language theory Algorithms Data structures Operating systems Software.
1 Compiler Design (40-414)  Main Text Book: Compilers: Principles, Techniques & Tools, 2 nd ed., Aho, Lam, Sethi, and Ullman, 2007  Evaluation:  Midterm.
. n COMPILERS n n AND n n INTERPRETERS. -Compilers nA compiler is a program thatt reads a program written in one language - the source language- and translates.
Introduction CPSC 388 Ellen Walker Hiram College.
Chapter 1 Introduction Major Data Structures in Compiler
Programming Languages
Compiler Introduction 1 Kavita Patel. Outlines 2  1.1 What Do Compilers Do?  1.2 The Structure of a Compiler  1.3 Compilation Process  1.4 Phases.
The Model of Compilation Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
What is a compiler? –A program that reads a program written in one language (source language) and translates it into an equivalent program in another language.
Dr. Mohamed Ramadan Saady 314ALL CH1.1 Chapter 1: Introduction to Compiling.
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
©SoftMoore ConsultingSlide 1 Structure of Compilers.
1 Asstt. Prof Navjot Kaur Computer Dept PRESENTED BY.
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
MiniJava Compiler A multi-back-end JIT compiler of Java.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 1 Ahmed Ezzat.
COP4020 Programming Languages Introduction Prof. Robert van Engelen (modified by Prof. Em. Chris Lacher)
Automating Fortran - C Interoperability Prepared by: Sisi Liu (Garnet) Mentor: Dan Nagle Co-Mentor: Davide del Vento 1.
Open Source Compiler Construction (for the JVM)
Chapter 1. Introduction.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Lecture 9 Symbol Table and Attributed Grammars
System Software Theory (5KS03).
Advanced Computer Systems
01 – Overview of Compilers
Compiler Design (40-414) Main Text Book:
PRINCIPLES OF COMPILER DESIGN
Chapter 1 Introduction.
Introduction to Compiler Construction
Constructing Precedence Table
Compiler Construction (CS-636)
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Chapter 1 Introduction.
PROGRAMMING LANGUAGES
课程名 编译原理 Compiling Techniques
Introduction to C Topics Compilation Using the gcc Compiler
Chapter 1: Introduction to Compiling (Cont.)
Compiler Lecture 1 CS510.
CS 536 / Fall 2017 Introduction to programming languages and compilers
Compiler Designs and Constructions
Basic Program Analysis: AST
CSE 3302 Programming Languages
CSE401 Introduction to Compiler Construction
COP4020 Programming Languages
Moonzoo Kim School of Computing KAIST
System Programming and administration
Programming Languages 2nd edition Tucker and Noonan
Moonzoo Kim School of Computing KAIST
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Presentation transcript:

Code Analysis using Compiler Front-ends (Clang) CSE 775 – Project # 2 Code Analysis using Compiler Front-ends (Clang) Technology presentation Instructor : Dr. Jim Fawcett February 26, 2015 PRADNYA KHALATE Spring 2015

Agenda Project idea Need of code analysis Compilation steps Compiler front-ends Introduction to Clang Clang libraries Clang AST Project plan

Project Idea Intent : Explore a potentially effective way of building static code analysis tool Study APIs provided in libClang Replace the analysis engine based on our Parser with that of libclang

Project Requirements Aim : Analyze the scope structure of set of C++ source code files Identify and display all the scope types (namespace, class, struct, enum etc.) Display size and complexity of each function definition in each analyzed file

Static Code Analysis Analyzing code without executing it Catches lexical, syntactic and some semantic errors Lexical – malformed name e.g. int 12qz; Syntactic – missing semicolon or unbalanced braces Semantic – valid code but doesn’t do intended action e.g. if (x=1) Maintain code quality

Compiler Architecture n*m problem -> n+m solution Intermediate Representation (IR) Front-end tasks : Scanning Parsing Semantic analysis Back-end tasks : Instruction selection Code optimization Code emission Fig: Compiler design

Lexical Analysis / Tokenization A scanner groups the characters into tokens Ignores whitespace Contiguous strings are part of one token Tokens are separated by punctuation characters or whitespace or line break e.g. For x = x * (y + 1) ; tokens generated will be id(x), =, id(x), *, (, id(y),+, num(1),), ; where, ‘id’ - identifier ‘num’ - integer literal

Abstract Syntax Tree (AST) Representation of source code as a tree of nodes representing constants or variables (leaves) and operators or statements (inner nodes) “Abstract" because it doesn’t represent every detail appearing in the real syntax Example while (k < 7) { foo(k); k++; }

The LLVM project Collection of modular and reusable compiler and toolchain technologies Began as a research project at the University of Illinois in 2000 Languages with compilers that use LLVM include – Common Lisp,, Ada, D, Fortran, OpenGL Shading Language, Go, Haskell, Java bytecode, Julia, Objective-C, Swift, Python, Ruby, Rust, Scala, Lua Later development at Apple Sub-projects include – LLVM core, Clang, LLDB, libc++, libcl,

What’s Clang? C language family frontend for LLVM Designed to offer a complete replacement to the GCC Developed by Apple, along with involvement of Google, ARM, Sony, Intel Current status A production quality C, Objective-C, C++ and Objective-C++ compiler when targeting X86-32, X86-64, and ARM Great for source analysis Supports C++11

Clang features End-user features Utility & Applications Internals Fast compile and low memory use Expressive diagnostics GCC compatibility End-user features Library based architecture Support diverse clients Use of LLVM BSD license Utility & Applications Real world production quality compiler Simple and hackable code base Single unified parser for C, C++, Objective-C Internals

Clang libraries Various parts of can be cleanly divided into separate libraries Clang is divided into the following libraries and tool: libsupport - Basic support library, from LLVM. libsystem - System abstraction library, from LLVM. libbasic - Diagnostics, SourceLocations, SourceBuffer abstraction, file system caching for input source files. libast - Provides classes to represent the C AST, the C type system, builtin functions, and various helpers for analyzing and manipulating the AST (visitors, pretty printers, etc).

Clang libraries (cont.) liblex - Lexing and preprocessing, identifier hash table, pragma handling, tokens, macro expansion. libparse - Parsing. This library invokes coarse-grained 'Actions' provided by the client (e.g. libsema builds ASTs) but knows nothing about ASTs or other client-specific data structures. libsema - Semantic Analysis. This provides a set of parser actions to build a standardized AST for programs. libcodegen - Lower the AST to LLVM IR for optimization & code generation. librewrite - Editing of text buffers (important for code rewriting transformation, like refactoring). libanalysis - Static analysis support. clang - A driver program, client of the libraries at various levels.

libClang C interface to Clang Collaboration diagram for libclang: C Interface to Clang: libClang C interface to Clang

Clang AST Command clang –cc1 –ast-dump [filename] #include <iostream> int main() { std::cout << "Hello world!\n" << std::endl; return 0; } TranslationUnitDecl 0x67b5120 <<invalid sloc>> <invalid sloc> |-TypedefDecl 0x67b5410 <<invalid sloc>> <invalid sloc> implicit __builtin_va_li st 'char *' `-FunctionDecl 0x67b5480 <helloWorld.cpp:4:1, line:8:1> line:4:5 main 'int (void )' `-CompoundStmt 0x67b55c8 <line:5:1, line:8:1> `-ReturnStmt 0x67b55b8 <line:7:2, col:9> `-IntegerLiteral 0x67b5598 <col:9> 'int' 0

Classes Core classes – Decl Stmt Type Glue classes Inheritance diagram for clang::Decl: Classes Core classes – Decl Stmt Type Glue classes Figure : Inheritance diagram for Decl class

Planned Tasks Setting up Clang Study of APIs provided in Clang libraries Understand Clang AST Develop a parser program with clean interfaces which can be integrated in any other program Implement scope analysis project with the new parser

References Compiler Architecture : https://lambda.uta.edu/cse5317/notes/node5.html Source Code Parsing: http://ecs.syr.edu/faculty/fawcett/handouts/webpages/ BlogParser.htm Clang homepage: http://clang.llvm.org/index.html Clang API documentation: http://clang.llvm.org/doxygen/index.html The Clang AST: http://llvm.org/devmtg/2013-04/klimek-slides.pdf

Questions / Suggestions / Feedback ?

Thank you!