92
Static Code Analysis for Perl @moznion

Static analysis for perl

  • Upload
    moznion

  • View
    1.287

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Static analysis for perl

Static Code Analysis for Perl

@moznion

Page 2: Static analysis for perl

Taiki Kawakami a.k.a @moznion

Sever side engineer (Java and Perl)

Author of - Perl::Lint - go-setlock

Page 3: Static analysis for perl

Taiki Kawakami a.k.a @moznion

Sever side engineer (Java and Perl)

Author of - Perl::Lint - go-setlock

Page 4: Static analysis for perl

Taiki Kawakami a.k.a @moznion

Sever side engineer (Java and Perl)

Author of - Perl::Lint - go-setlock

Page 5: Static analysis for perl

Fundamental of Static Analysis

Page 6: Static analysis for perl

Static Analysis

A method of analysis source code WITHOUT execution

Page 7: Static analysis for perl

Static AnalysisExample of advantages: - Easy to detect - unused vars - irregular coding styles - Analyze dependencies between modules/classes

Page 8: Static analysis for perl

Static AnalysisExample of advantages: - Easy to detect - unused vars - irregular coding styles - Analyze dependencies between modules/classes

BORING!

Page 9: Static analysis for perl

Static AnalysisExample of advantages: - Easy to detect - unused vars - irregular coding styles - Analyze dependencies between modules/classes

Difficult…

Page 10: Static analysis for perl

Let's Exercise

Page 11: Static analysis for perl

This code has 5 traps

Page 12: Static analysis for perl

This code has 5 traps

Page 13: Static analysis for perl

This code has 5 traps

Page 14: Static analysis for perl

This code has 5 traps

Page 15: Static analysis for perl

This code has 5 traps

Page 16: Static analysis for perl

This code has 5 traps

Page 17: Static analysis for perl

It was fun?

Page 18: Static analysis for perl

This is ridiculous code ceview

Page 19: Static analysis for perl

Probably human overlooks

Page 20: Static analysis for perl

We should focus on advanced topic on code review

Page 21: Static analysis for perl

How?

Page 22: Static analysis for perl

It is necessary clean code

Page 23: Static analysis for perl

Destroy these

Page 24: Static analysis for perl

Be maintainable code!

Page 25: Static analysis for perl

Make computer analyze them!

Page 26: Static analysis for perl

How to make static analyzer?

Page 27: Static analysis for perl

Pre-Process

Lexical Analyze

Syntactic Analyze

Source code (String)

Result

Analyze

Page 28: Static analysis for perl

Pre-Process

Lexical Analyze

Syntactic Analyze

Source code (String)

Result

Analyze

Page 29: Static analysis for perl
Page 30: Static analysis for perl

PPI::Tokenizer

Page 31: Static analysis for perl

Pre-Process

Lexical Analyze

Syntactic Analyze

Source code (String)

Result

Analyze

Page 32: Static analysis for perl

PPI::Document

Provides PDOM Structure

Page 33: Static analysis for perl

Pre-Process

Lexical Analyze

Syntactic Analyze

Source code (String)

Result

Analyze

Page 34: Static analysis for perl

“Analyze” phase checks code with using AST and tokens in accordance with rules

Page 35: Static analysis for perl

Method of some languages are different; they look byte code (e.g. Java:findbugs)

Page 36: Static analysis for perl

Perl::Critic

Page 37: Static analysis for perl

Perl::Critic is the great tool!

Page 38: Static analysis for perl

Perl::Critic checks the code conform to PBP style or not

Page 39: Static analysis for perl

Perl::Critic uses PPI as a Lexer and Parser

Page 40: Static analysis for perl

Pre-Process

Lexical Analyze

Syntactic Analyze

Source code (String)

Result

Analyze

PPI

Page 41: Static analysis for perl

Perl::Lint

Page 42: Static analysis for perl

Perl::Lint is a yet another static analyser for perl

Page 43: Static analysis for perl

This project supported by TPF

Page 44: Static analysis for perl

Perl::Critic is enough. Why Perl::Lint?

Page 45: Static analysis for perl

I want to make it faster!!!

Page 46: Static analysis for perl

Mechanism of Perl::Lint

Page 47: Static analysis for perl

Pre-Process

Lexical Analyze

Syntactic Analyze

Source code (String)

Result

Analyze

Regex

Compiler::Lexer

Perl::Lint::Policy

Page 48: Static analysis for perl

Pre-Process

Lexical Analyze

Syntactic Analyze

Source code (String)

Result

Analyze

Regex

Compiler::Lexer

Perl::Lint::Policy

Page 49: Static analysis for perl

Pre-Processing

Page 50: Static analysis for perl

## no lint

Page 51: Static analysis for perl

## no lintTo retrieve this

Page 52: Static analysis for perl

Find where (what line) is “## no lint” by regex

Page 53: Static analysis for perl

Find where (what line) is “## no lint” by regex

And compare between line number of “## no lint” and violation’s one, if match them, ignore form result!

Page 54: Static analysis for perl

Compiler::Lexer can retrieve comments by verbose mode, but it makes slower about 4 times😢 So using regex

Page 55: Static analysis for perl

Pre-Process

Lexical Analyze

Syntactic Analyze

Source code (String)

Result

Analyze

Regex

Compiler::Lexer

Perl::Lint::Policy

Page 56: Static analysis for perl

Tokenize source code by Compiler::Lexer

Page 57: Static analysis for perl
Page 58: Static analysis for perl
Page 59: Static analysis for perl

Compiler::Lexer made of C++ Really fast!

Page 60: Static analysis for perl

Stable (nowadays)

Page 61: Static analysis for perl

But…

Page 62: Static analysis for perl
Page 63: Static analysis for perl

Perl-5.22………………

Page 64: Static analysis for perl

Pre-Process

Lexical Analyze

Syntactic Analyze

Source code (String)

Result

Analyze

Regex

Compiler::Lexer

Perl::Lint::Policy

Page 65: Static analysis for perl

Compiler::Parser exists, but that doesn’t work as expected

Page 66: Static analysis for perl

Pre-Process

Lexical Analyze

Syntactic Analyze

Source code (String)

Result

Analyze

Regex

Compiler::Lexer

Perl::Lint::Policy

Page 67: Static analysis for perl

Read token list sequentially and evaluate them. Each policies are responsible for those.

Page 68: Static analysis for perl

Like this

Page 69: Static analysis for perl

Like this

Page 70: Static analysis for perl

Like this…

Page 71: Static analysis for perl

And it is necessary to analyze contents of regex (m/here!/)

Page 72: Static analysis for perl

Using Regexp::Lexer This is a module to tokenize regex

Page 73: Static analysis for perl

Example;

Page 74: Static analysis for perl

Each policies are independent, so easy to write new policy (You can write your own policy)

Page 75: Static analysis for perl

Easy and Simple: Scan tokens and write validation processing according to scanned token sequentially

Page 76: Static analysis for perl

Perl::Lint has filter system

Page 77: Static analysis for perl

Perl::Lint executes all of the policies by default. Write a black list to ignore any policy.

Page 78: Static analysis for perl

Current Status

Page 79: Static analysis for perl

Almost policies of Perl::Critic are available on Perl::Lint

Page 80: Static analysis for perl

現状のステータス

Page 81: Static analysis for perl

Documentation is lacked…

Page 82: Static analysis for perl

Application

Page 83: Static analysis for perl

Test::Perl::Lint

Testing module like a Test::Perl::Critic

Page 84: Static analysis for perl

Perl::Lint::Git

Connect git and Perl::Lint to blame the right people for violations.Connect git and Perl::Lint to blame

the right people for violations.

Page 85: Static analysis for perl

Future works

Page 86: Static analysis for perl

I should have written a parser… Compiler::Lexer::PP (?)

Page 87: Static analysis for perl

Enhance documentation

Page 88: Static analysis for perl

Bug fix

Page 89: Static analysis for perl

Support new perl notations

Page 90: Static analysis for perl

Support code climate

Page 91: Static analysis for perl

CHEATING: Run each policies with pre-fork model

Page 92: Static analysis for perl

Any Q? (If I can answer…)