eclipse con 2015: codan - a c/c++ code analysis framework for cdt

37
Codan - a Code Analysis Framework for CDT Elena Laskavaia 2015 If (getuid()!=0 && geteuid==0) { ErrorF(“only root”); exit(1); }

Upload: elena-laskavaia

Post on 19-Jul-2015

129 views

Category:

Software


7 download

TRANSCRIPT

Page 1: Eclipse Con 2015: Codan - a C/C++ Code Analysis Framework for CDT

Codan - a Code Analysis Framework

for CDT

Elena Laskavaia

2015 If (getuid()!=0 && geteuid==0) {

ErrorF(“only root”);

exit(1);

}

Page 2: Eclipse Con 2015: Codan - a C/C++ Code Analysis Framework for CDT

Why Static Analysis?

Defect Detection

Potential Problems

Code Style

Security Violations

Defect Detection

Unused Code

Code Patterns

Metrics Violations

Reverse Engineering

Model Visualization

Cross-Reference

Metrics

Forward Engineering

Refactoring

Context-Assist

Code Generation

Analysis of source code without running the program

Page 3: Eclipse Con 2015: Codan - a C/C++ Code Analysis Framework for CDT

Cost of fixing one bug

Stage Bug Life Cost

As you type 1 s 1 cent

Developer build 10 sec 10 cent

Developer testing 10 min $3

SCM check in 4 h $10

Integration build 1 d $40

Integration testing 10 days $200

In the field 30 days $1000+

In outer space 3 years $100 million*

1

10

100

1000

10000

Dev Unit QA User Live

Cost

Page 4: Eclipse Con 2015: Codan - a C/C++ Code Analysis Framework for CDT

Defect

DetectionEclipse-CDT

Codan

Codan

Page 5: Eclipse Con 2015: Codan - a C/C++ Code Analysis Framework for CDT

UX: Klocwork C/C++

Page 6: Eclipse Con 2015: Codan - a C/C++ Code Analysis Framework for CDT

UX: PMD Java

Page 7: Eclipse Con 2015: Codan - a C/C++ Code Analysis Framework for CDT

UX: Find Bugs Java

Page 8: Eclipse Con 2015: Codan - a C/C++ Code Analysis Framework for CDT

UX: UCDetector Java

Page 9: Eclipse Con 2015: Codan - a C/C++ Code Analysis Framework for CDT

Basic Architecture

Checkers

Code

Models

Problem

MarkersTriggers

Marker

Presentation

Quick Fixes

Preferences(Configuration)

Preference

Editor

Page 10: Eclipse Con 2015: Codan - a C/C++ Code Analysis Framework for CDT

Codan Problem Markers

• Codan problem markers – categories, editor annotations

• Quick Fix• Problem Details view

• Menu: Customize Problem...

• Menu: Show in -> Problem Details

Page 11: Eclipse Con 2015: Codan - a C/C++ Code Analysis Framework for CDT

Preference Editor

• Enablement

• Severity

• Message

• Parameters

• Scope

• Launching Triggers

Page 12: Eclipse Con 2015: Codan - a C/C++ Code Analysis Framework for CDT

Launch Triggers

Run on demand from context menuRun as you type

Run with Build

Page 13: Eclipse Con 2015: Codan - a C/C++ Code Analysis Framework for CDT

Writing a Good Checker

Framework worth nothing without checkers

Framework is bad if checkers are bad

What does it take to write a good checker?

Page 14: Eclipse Con 2015: Codan - a C/C++ Code Analysis Framework for CDT

Good Defect Detection Tool

• Be part of the processIntegrated

• Ignore defects, change severity, parameterizeCustomizable

• Fix the code - if canAuto-correcting

• Bad description damages tool reputationSelf-explaining

• No code modifications: exceptions, historyHide false positives

• Reconfigure itself based on defect density Adaptable

• Not: laggy, annoying, noisy, wrong Not burden

Page 15: Eclipse Con 2015: Codan - a C/C++ Code Analysis Framework for CDT

Checker Design Cycle

• Unit tests

• “Field” test

• Profiling

• Customization

• Properties

• Checker

• Quick Fix

• Error Parsers

• Problem Details

• Idea

• Presentation

• Code Model

• Good fit?

Design Develop

TestTune

Page 16: Eclipse Con 2015: Codan - a C/C++ Code Analysis Framework for CDT

Design

Define GoalBrainstorm

PresentationPick Code

ModelSketch

Is selected framework the best choice?

Page 17: Eclipse Con 2015: Codan - a C/C++ Code Analysis Framework for CDT

Problem Marker is a problem

Problem Marker – you blame developer

Defensive reaction – blame the tool

Too much red marks – turn it off

Too many f.p. – don't trust anymore

Unclear description – tool is wrong

Page 18: Eclipse Con 2015: Codan - a C/C++ Code Analysis Framework for CDT

Alternative UX

Tree

TreeMap

Charts

Unit TestCode

Formatter

Quick Fix

Search

Call Graph Description

Page 19: Eclipse Con 2015: Codan - a C/C++ Code Analysis Framework for CDT

Code Models

AST

Preprocessor

Comments

Tokens

Bindings

Control Flow Graph

Data Flow Graph*

Text

File Structure

C-Model

(Containment)

C-Index

(Cross Reference)

Page 20: Eclipse Con 2015: Codan - a C/C++ Code Analysis Framework for CDT

Code Models Visualized

Page 21: Eclipse Con 2015: Codan - a C/C++ Code Analysis Framework for CDT

dummy() {

return;

}

void some1();

void some() {

return some1();

}

int retindead() {

return 5;;

}

int infloop() {

while(1) { … }

}

void f() {

[](int r){return r;}(5);

}

int test() {

class A {

void m() { return; };

}

}

auto f() -> void

{

}

f.p. -

nobody cares

f.p. - void

expression

f.p. - missing

auto evaluation

f.p. - enclosed

functionsf.p. - lambda

f.p. - dead code

or unreachable

Return Mismatch CheckerHistory in bug reports…

• Return type of function does not match type of return expression

• Or return is missing for non-void function

Page 22: Eclipse Con 2015: Codan - a C/C++ Code Analysis Framework for CDT

Return Checker Uses Models

AST

Preprocessor

Comments

Tokens

Bindings

Control Flow Graph

Data Flow Graph*

Text

File Structure

C-Model

(Containment)

C-Index

(Cross Reference)

Page 23: Eclipse Con 2015: Codan - a C/C++ Code Analysis Framework for CDT

Internal Checker

Checker

Code

Models

Problem

MarkersTriggers

Marker

Presentation

Quick Fix

Preferences(Configuration)

Preference

Editor

Problem

Page 24: Eclipse Con 2015: Codan - a C/C++ Code Analysis Framework for CDT

Extension Point

<extension

point="org.eclipse.cdt.codan.core.checkers">

</checker>

<checker

class="org.eclipse.cdt.codan.internal.checkers.StatementHasNoEffectChecker"

id="org.eclipse.cdt.codan.internal.checkers.StatementHasNoEffectChecker"

name="StatementHasNoEffectChecker">

<problem

category="org.eclipse.cdt.codan.core.categories.ProgrammingProblems"

defaultSeverity="Warning"

id="org.eclipse.cdt.codan.internal.checkers.StatementHasNoEffectProblem"

name="Statement has no effect">

messagePattern="Statement has no effect ''{0}''"

/>

</checker>

</extension>

Extension: checkers

Problem A

Checker

Problem B…

ID

Enablement

Severity

Description

Page 25: Eclipse Con 2015: Codan - a C/C++ Code Analysis Framework for CDT

Checker Class

public class StatementHasNoEffectChecker extends AbstractIndexAstChecker {

private static final String ER_ID = "org.eclipse.cdt.codan.internal.checkers.StatementHasNoEffectProblem";

public void processAst(IASTTranslationUnit ast) {

ast.accept(new CheckStmpVisitor());

}

class CheckStmpVisitor extends ASTVisitor {

CheckStmpVisitor() {

shouldVisitStatements = true;

}

public int visit(IASTStatement stmt) {

if (stmt instanceof IASTExpressionStatement) {

if (hasNoEffect(((IASTExpressionStatement) stmt).getExpression())) {

reportProblem(ER_ID, stmt);

}

return PROCESS_SKIP;

}

return PROCESS_CONTINUE;

}…

See full code of this checker in codan subtree of project:

org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/StatementHasNoEffectChecker.java

Page 26: Eclipse Con 2015: Codan - a C/C++ Code Analysis Framework for CDT

External Tool Integration

• Triggers (choose one)

– Integrate into build system and parse output

– Invoke from checker and parse output

• Severity Mapping

• Problem Preference editor (or not)

• Extra details (hyperlinks)

• Tool configuration (preference page)

Page 27: Eclipse Con 2015: Codan - a C/C++ Code Analysis Framework for CDT

External Tool Invoke

CheckerProblem

MarkersTriggers

Error

Parser

Tool

Problem

Details

Problem

Quick Fix

Page 28: Eclipse Con 2015: Codan - a C/C++ Code Analysis Framework for CDT

External Tool Built-in

Problem

Markers

Preferences(Configuration)

Preference

Editor

BuildError

Parser

Build

Magic

Listener Tool

Configuration

Problem

Problem

Details

Page 29: Eclipse Con 2015: Codan - a C/C++ Code Analysis Framework for CDT

External Invoke Code

public void processUnit(ITranslationUnit unit) {

IScannerInfo scannerInfo = unit.getScannerInfo(true);

List<String> res = getCompilerOptionsList(scannerInfo); // -I.. –D…

res.add("-c");res.add("-o/dev/null");res.add("-O2");res.add("-Wall");// default flags

res.add(unit.getFile().getLocation().toPortableString());// file path

String args[] = res.toArray(new String[res.size()]);

try {

externalToolInvoker.launchOnBuildConsole(

unit.getResource().getProject(),

new IConsoleParser[] { getConsoleParser(unit) }, // parser converts patterns to markers

"check",

getToolPath(), args, new String[] {}, getWorkingDirectory(), // command line

new NullProgressMonitor());

} catch (CoreException | InvocationFailure e) {

Activator.log(e);

}

}

Page 30: Eclipse Con 2015: Codan - a C/C++ Code Analysis Framework for CDT

APIs

CDT

CodanBase Checkers

Base Quick Fix

Problem Details

Control Flow Graph

Error Parser Utils

Launch Utils

CDT

C-ASTComments

Includes

Marcos

Tokens

AST

Bindings

CDT

CoreC-Element

Containment

Cross References

Error Parsers

Scanner Discovery

AST-Rewrite

Eclipse

Platform

Property Change Listeners

Problem Markers

Editor Annotations

Resources

Page 31: Eclipse Con 2015: Codan - a C/C++ Code Analysis Framework for CDT

Test

Run Static Analysis

• On checker’s code!

Write Junits

• True Positives

• True Negatives

• Error recovery

• Use Code Coverage

Field Testing

• Large Code Base

• C++ not only C

• Inspect for f.p.

Page 32: Eclipse Con 2015: Codan - a C/C++ Code Analysis Framework for CDT

Junit Test and Coverage

// main() {

// int a,b;

//

// b+a; // error here

// }

public void testBinaryExpression() {

checkSampleAbove();

}

// main() {

// int a,b;

//

// a=b+a; // no error here

// }

public void testNormalAssignment() {

checkSampleAbove();

}

Page 33: Eclipse Con 2015: Codan - a C/C++ Code Analysis Framework for CDT

Tune

Profile

• Performance

• Memory

• Stats

Customize

• Variations

• Split Problems

• Exception Parameters

• Adaptation

• F.P. Reduction

Properties

• Severity

• Enablement

• Category

• Description

• Launch Triggers

Page 34: Eclipse Con 2015: Codan - a C/C++ Code Analysis Framework for CDT

Adding problem parameters

public void initPreferences(IProblemWorkingCopy problem) {

super.initPreferences(problem);

if (problem.getId().equals(RET_NO_VALUE_ID)) {

addPreference(problem, PARAM_IMPLICIT,

“Also check functions with implicit return value”,

Boolean.FALSE);

}

}

Page 35: Eclipse Con 2015: Codan - a C/C++ Code Analysis Framework for CDT

Creative Use

Quick Fix Only

• Error pattern to trigger quick fix from compiler errors

Problem Details Only

• Hyperlink to generic search

Compile as you type

• As computers getting faster… c++ compilers too!

Tool chain warning configuration

• Modify build configuration as user changes error profile

Editor Markup

• Use highlighting annotations instead of regular once

Headless

• Reuse checkers to run headless

Page 36: Eclipse Con 2015: Codan - a C/C++ Code Analysis Framework for CDT

Questions??

Page 37: Eclipse Con 2015: Codan - a C/C++ Code Analysis Framework for CDT

+1 0 -1

Sign in: www.eclipsecon.org

Evaluate the sessions