checking coding rules in oo languages using...

26
Checking Coding Rules in OO Languages Using CRISP Guillem Marpons 1 Joint work with: Julio Mari˜ no Manuel Carro ´ Angel Herranz Lars- ˚ Ake Fredlund Juan Jos´ e Moreno-Navarro 1 Universidad Polit´ ecnica de Madrid [email protected] COST Action IC0701 Meeting, Madrid December 2008 Guillem Marpons et al. (UPM) Coding Rule Checking Madrid December 2008 1 / 18

Upload: others

Post on 20-Jun-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Checking Coding Rules in OO Languages Using CRISPbabel.ls.fi.upm.es/~gmarpons/pubs/COSTaction08... · Checking Coding Rules in OO Languages Using CRISP Guillem Marpons1 Joint work

Checking Coding Rules in OO Languages Using CRISP

Guillem Marpons1

Joint work with:Julio Marino Manuel Carro Angel Herranz

Lars-Ake Fredlund Juan Jose Moreno-Navarro

1Universidad Politecnica de [email protected]

COST Action IC0701 Meeting, Madrid December 2008

Guillem Marpons et al. (UPM) Coding Rule Checking Madrid December 2008 1 / 18

Page 2: Checking Coding Rules in OO Languages Using CRISPbabel.ls.fi.upm.es/~gmarpons/pubs/COSTaction08... · Checking Coding Rules in OO Languages Using CRISP Guillem Marpons1 Joint work

Motivation: C++ “Strange” Behavior

class A{public:

A();virtual void func ();

};

class B : public A{

B() : A() {}virtual void func ();

};

A::A() {func ();

}

B *d = new B();

// A::func or B::func?

Guillem Marpons et al. (UPM) Coding Rule Checking Madrid December 2008 2 / 18

Page 3: Checking Coding Rules in OO Languages Using CRISPbabel.ls.fi.upm.es/~gmarpons/pubs/COSTaction08... · Checking Coding Rules in OO Languages Using CRISP Guillem Marpons1 Joint work

Motivation: C++ “Strange” Behavior

class A{public:

A();virtual void func ();

};

class B : public A{

B() : A() {}virtual void func ();

};

A::A() {func ();

}

B *d = new B();

// A::func or B::func?

Coding Rule:

“Do not invoke virtual methods of the declared classin a constructor or destructor.”

Guillem Marpons et al. (UPM) Coding Rule Checking Madrid December 2008 2 / 18

Page 4: Checking Coding Rules in OO Languages Using CRISPbabel.ls.fi.upm.es/~gmarpons/pubs/COSTaction08... · Checking Coding Rules in OO Languages Using CRISP Guillem Marpons1 Joint work

Coding Rules

Definition

Coding Rules constrain admissible constructs of alanguage to help produce more reliable and

maintainable code.

Standard coding rule sets do exist, e.g.:

High-Integrity C++ (HICPP): general C++ applications

MISRA-C (C language): automotive industry / embedded systems

Many organisations need to write their own rule setsor adapt existing ones.

Guillem Marpons et al. (UPM) Coding Rule Checking Madrid December 2008 3 / 18

Page 5: Checking Coding Rules in OO Languages Using CRISPbabel.ls.fi.upm.es/~gmarpons/pubs/COSTaction08... · Checking Coding Rules in OO Languages Using CRISP Guillem Marpons1 Joint work

Other Tools

Proprietary tools:

Compilers: IAR Systems (C)

QA: Parasoft, Klocwork, Coverity, Semmle Code (Java)

Free software:

Checkstyle (Java)

Gendarme (ECMA CIL, Mono and .Net)

Drawbacks:

Lack of appropriate extensibility mechanisms

Ambiguity in natural language

Interoperability is difficult

Guillem Marpons et al. (UPM) Coding Rule Checking Madrid December 2008 4 / 18

Page 6: Checking Coding Rules in OO Languages Using CRISPbabel.ls.fi.upm.es/~gmarpons/pubs/COSTaction08... · Checking Coding Rules in OO Languages Using CRISP Guillem Marpons1 Joint work

Proposed Approach

1 Formalize rules in a logic-based specification languagethat is executable: CRISP

2 Use GCC for gathering necessary program information

Guillem Marpons et al. (UPM) Coding Rule Checking Madrid December 2008 5 / 18

Page 7: Checking Coding Rules in OO Languages Using CRISPbabel.ls.fi.upm.es/~gmarpons/pubs/COSTaction08... · Checking Coding Rules in OO Languages Using CRISP Guillem Marpons1 Joint work

Our Rule Checking Procedure

Coding rules(in English)

C++ projectsource files

Coding rulesformalized

in CRISPC++

Coding rulecompiler

g++’

(project build)

Coding rulescompiled

into Prolog

Project factsin Prolog

Ciao Prologengine

Rule viola-tions report

1 Coding rule(s) written oncein the logic-based formalism

2 Extract program information(+ analysis information ifavailable) using GCC, andstore it

3 Search (using a Prologengine) for a counterexample

Guillem Marpons et al. (UPM) Coding Rule Checking Madrid December 2008 6 / 18

Page 8: Checking Coding Rules in OO Languages Using CRISPbabel.ls.fi.upm.es/~gmarpons/pubs/COSTaction08... · Checking Coding Rules in OO Languages Using CRISP Guillem Marpons1 Joint work

Our Rule Checking Procedure

Coding rules(in English)

C++ projectsource files

Coding rulesformalized

in CRISPC++

Coding rulecompiler

g++’

(project build)

Coding rulescompiled

into Prolog

Project factsin Prolog

Ciao Prologengine

Rule viola-tions report

1 Coding rule(s) written oncein the logic-based formalism

2 Extract program information(+ analysis information ifavailable) using GCC, andstore it

3 Search (using a Prologengine) for a counterexample

Guillem Marpons et al. (UPM) Coding Rule Checking Madrid December 2008 6 / 18

Page 9: Checking Coding Rules in OO Languages Using CRISPbabel.ls.fi.upm.es/~gmarpons/pubs/COSTaction08... · Checking Coding Rules in OO Languages Using CRISP Guillem Marpons1 Joint work

Our Rule Checking Procedure

Coding rules(in English)

C++ projectsource files

Coding rulesformalized

in CRISPC++

Coding rulecompiler

g++’

(project build)

Coding rulescompiled

into Prolog

Project factsin Prolog

Ciao Prologengine

Rule viola-tions report

1 Coding rule(s) written oncein the logic-based formalism

2 Extract program information(+ analysis information ifavailable) using GCC, andstore it

3 Search (using a Prologengine) for a counterexample

Guillem Marpons et al. (UPM) Coding Rule Checking Madrid December 2008 6 / 18

Page 10: Checking Coding Rules in OO Languages Using CRISPbabel.ls.fi.upm.es/~gmarpons/pubs/COSTaction08... · Checking Coding Rules in OO Languages Using CRISP Guillem Marpons1 Joint work

Our Rule Checking Procedure

Coding rules(in English)

C++ projectsource files

Coding rulesformalized

in CRISPC++

Coding rulecompiler

g++’

(project build)

Coding rulescompiled

into Prolog

Project factsin Prolog

Ciao Prologengine

Rule viola-tions report

1 Coding rule(s) written oncein the logic-based formalism

2 Extract program information(+ analysis information ifavailable) using GCC, andstore it

3 Search (using a Prologengine) for a counterexample

Guillem Marpons et al. (UPM) Coding Rule Checking Madrid December 2008 6 / 18

Page 11: Checking Coding Rules in OO Languages Using CRISPbabel.ls.fi.upm.es/~gmarpons/pubs/COSTaction08... · Checking Coding Rules in OO Languages Using CRISP Guillem Marpons1 Joint work

CRISP Building Blocks 1: Sorts

Variable, DataMember, LocalVariable

Function, MemberFunction, Constructor

Type, PointerType, Record

Scope, Namespace, Record, CompoundStatement

Operator

ArgumentTypeInFunctionType

ClassMember

Thing

Guillem Marpons et al. (UPM) Coding Rule Checking Madrid December 2008 7 / 18

Page 12: Checking Coding Rules in OO Languages Using CRISPbabel.ls.fi.upm.es/~gmarpons/pubs/COSTaction08... · Checking Coding Rules in OO Languages Using CRISP Guillem Marpons1 Joint work

CRISP Building Blocks 2: (Binary) Relations

Function calls FunctionRecord hasImmediateBase RecordVariable hasType NonFunctionTypeFunction hasType FunctionTypeThing isDefinedIn ScopeScope isNestedIn ScopeRecord hasMember MemberFunctionRecord hasMember DataMemberRecord hasBase RecordRecord isPrivateBaseOf RecordRecord isVirtualBaseOf RecordPointerType hasPointedType TypeFunctionType hasReturnType TypeRecord hasFriend RecordRecord hasFriend MemberFunctionClassMember hasVisibility Visibility

Guillem Marpons et al. (UPM) Coding Rule Checking Madrid December 2008 8 / 18

Page 13: Checking Coding Rules in OO Languages Using CRISPbabel.ls.fi.upm.es/~gmarpons/pubs/COSTaction08... · Checking Coding Rules in OO Languages Using CRISP Guillem Marpons1 Joint work

Example of Rule Formalization

Rule HICPP 3.3.13:

“Do not invoke virtual methods of the declared classin a constructor or destructor.”

Guillem Marpons et al. (UPM) Coding Rule Checking Madrid December 2008 9 / 18

Page 14: Checking Coding Rules in OO Languages Using CRISPbabel.ls.fi.upm.es/~gmarpons/pubs/COSTaction08... · Checking Coding Rules in OO Languages Using CRISP Guillem Marpons1 Joint work

Example of Rule Formalization

Rule HICPP 3.3.13:

“Do not invoke virtual methods of the declared classin a constructor or destructor.”

rule HICPP 3.3.13’

violated by Caller : MemberFunction; Callee : VirtualFunction

when exists R : Record such that

(

R hasMember Caller

and R hasMember Callee

and

(

Caller is Constructor

or Caller is Destructor

)

and Caller calls+ Callee

)

.

Guillem Marpons et al. (UPM) Coding Rule Checking Madrid December 2008 9 / 18

Page 15: Checking Coding Rules in OO Languages Using CRISPbabel.ls.fi.upm.es/~gmarpons/pubs/COSTaction08... · Checking Coding Rules in OO Languages Using CRISP Guillem Marpons1 Joint work

Auxiliary Sorts and Relations

relation F : Function oveloads F’ : Function

when exists S : Scope ; N : String such that

(

F isDefinedIn S

and F’ isDefinedIn S

and F hasUnqualifiedName N

and F’ hasUnqualifiedName N

and F \= F’

)

.

sort M : ClassMember is PrivateClassMember

when exists V : Visibility such that

(

M hasVisibility V and V is ‘private’

)

.

Guillem Marpons et al. (UPM) Coding Rule Checking Madrid December 2008 10 / 18

Page 16: Checking Coding Rules in OO Languages Using CRISPbabel.ls.fi.upm.es/~gmarpons/pubs/COSTaction08... · Checking Coding Rules in OO Languages Using CRISP Guillem Marpons1 Joint work

Integration into GCC

GlobalGCC Project (Eureka/ITEA). Enhance the GNU compilercollection with:

I Project-wide static analysisI Global optimizationI Coding rule checking

Huge potential user base

All facts of a project in a single fileUser interface. Two steps:

1 g++ -fipa-codingrules -fipa-codingrules-file=FILENAME

Pass appropriate CXX to ./configure2 checkrules -s RULE SET [-r RULES] FILENAME

Code available at http://www.ggcc.info

Guillem Marpons et al. (UPM) Coding Rule Checking Madrid December 2008 11 / 18

Page 17: Checking Coding Rules in OO Languages Using CRISPbabel.ls.fi.upm.es/~gmarpons/pubs/COSTaction08... · Checking Coding Rules in OO Languages Using CRISP Guillem Marpons1 Joint work

Experimental Results

PROJECT KLOC LOAD TIME # VIOLATIONS (CHECKING TIME)3.3.1 3.3.2 3.3.11 3.3.15

Bacula 20 0.24 0 (0.0) 3 (0.0) 0 (0.0) 0 (0.0)CLAM 46 1.62 1 (0.0) 15 (0.5) 115 (0.1) 0 (0.2)Firebird 439 2.61 16 (0.0) 60 (1.0) 115 (0.2) 0 (0.3)IT++ 39 0.42 0 (0.0) 6 (0.0) 12 (0.0) 0 (0.0)OGRE 209 3.05 0 (0.0) 15 (0.9) 79 (0.2) 0 (0.3)Orca 89 1.17 1 (0.0) 12 (0.4) 0 (0.1) 0 (0.2)Qt 595 10.42 15 (0.0) 75 (10.5) 1155 (1.3) 4 (1.2)

All times expressed in seconds.

Guillem Marpons et al. (UPM) Coding Rule Checking Madrid December 2008 12 / 18

Page 18: Checking Coding Rules in OO Languages Using CRISPbabel.ls.fi.upm.es/~gmarpons/pubs/COSTaction08... · Checking Coding Rules in OO Languages Using CRISP Guillem Marpons1 Joint work

Work in Progress

1 Implement / Enrich the CRISP Language

2 Implement more rules with information given by other tools

3 Open our abstract representation of programs to external tools

Guillem Marpons et al. (UPM) Coding Rule Checking Madrid December 2008 13 / 18

Page 19: Checking Coding Rules in OO Languages Using CRISPbabel.ls.fi.upm.es/~gmarpons/pubs/COSTaction08... · Checking Coding Rules in OO Languages Using CRISP Guillem Marpons1 Joint work

Implement / enrich the CRISP language

Quantification and true negation neededI Both performed over certain domains (sorts)I Infinite domains may appear with templates / genericsI We have an implementation of constructive intensional negation

Goals automatically reordered

Extend CRISP to other languages: Java, Ada, C, Fortran, . . .

Guillem Marpons et al. (UPM) Coding Rule Checking Madrid December 2008 14 / 18

Page 20: Checking Coding Rules in OO Languages Using CRISPbabel.ls.fi.upm.es/~gmarpons/pubs/COSTaction08... · Checking Coding Rules in OO Languages Using CRISP Guillem Marpons1 Joint work

Examples of Rules that Need Specific Analysis

Rule HICPP 3.3.13:

“Do not invoke virtual methods of the declared class in a constructor ordestructor.”

Rule HICPP 3.2.5:

“Ensure destructors release all objects owned by the object”

Rule HICPP 3.4.2:

“Do not return non-const handles to class data from const memberfunctions”

Guillem Marpons et al. (UPM) Coding Rule Checking Madrid December 2008 15 / 18

Page 21: Checking Coding Rules in OO Languages Using CRISPbabel.ls.fi.upm.es/~gmarpons/pubs/COSTaction08... · Checking Coding Rules in OO Languages Using CRISP Guillem Marpons1 Joint work

Examples of Rules that Need Specific Analysis

Rule HICPP 3.3.13:

“Do not invoke virtual methods of the declared class in a constructor ordestructor.”

Rule HICPP 3.2.5:

“Ensure destructors release all objects owned by the object”

Rule HICPP 3.4.2:

“Do not return non-const handles to class data from const memberfunctions”

Guillem Marpons et al. (UPM) Coding Rule Checking Madrid December 2008 15 / 18

Page 22: Checking Coding Rules in OO Languages Using CRISPbabel.ls.fi.upm.es/~gmarpons/pubs/COSTaction08... · Checking Coding Rules in OO Languages Using CRISP Guillem Marpons1 Joint work

Examples of Rules that Need Specific Analysis

Rule HICPP 3.3.13:

“Do not invoke virtual methods of the declared class in a constructor ordestructor.”

Rule HICPP 3.2.5:

“Ensure destructors release all objects owned by the object”

Rule HICPP 3.4.2:

“Do not return non-const handles to class data from const memberfunctions”

Guillem Marpons et al. (UPM) Coding Rule Checking Madrid December 2008 15 / 18

Page 23: Checking Coding Rules in OO Languages Using CRISPbabel.ls.fi.upm.es/~gmarpons/pubs/COSTaction08... · Checking Coding Rules in OO Languages Using CRISP Guillem Marpons1 Joint work

Integration of Information from External Analyzers

Coding rules(in English)

C++ projectsource files

Coding rulesformalized

in CRISPC++

Coding rulecompiler

g++’

(project build)

Coding rulescompiled

into Prolog

Project factsin Prolog

Ciao Prologengine

Rule viola-tions report

Guillem Marpons et al. (UPM) Coding Rule Checking Madrid December 2008 16 / 18

Page 24: Checking Coding Rules in OO Languages Using CRISPbabel.ls.fi.upm.es/~gmarpons/pubs/COSTaction08... · Checking Coding Rules in OO Languages Using CRISP Guillem Marpons1 Joint work

Integration of Information from External Analyzers

Coding rules(in English)

C++ projectsource files

Coding rulesformalized

in CRISPC++

Coding rulecompiler

g++’

(project build)

Knowledge Base about the compiled program

Ciao Prologengine

Rule viola-tions report

ExternalAnalyzer

Translation

Guillem Marpons et al. (UPM) Coding Rule Checking Madrid December 2008 16 / 18

Page 25: Checking Coding Rules in OO Languages Using CRISPbabel.ls.fi.upm.es/~gmarpons/pubs/COSTaction08... · Checking Coding Rules in OO Languages Using CRISP Guillem Marpons1 Joint work

Open the Knowledge Base to External Tools

Guillem Marpons et al. (UPM) Coding Rule Checking Madrid December 2008 17 / 18

Page 26: Checking Coding Rules in OO Languages Using CRISPbabel.ls.fi.upm.es/~gmarpons/pubs/COSTaction08... · Checking Coding Rules in OO Languages Using CRISP Guillem Marpons1 Joint work

Thanks for your Attention!

Questions?

Comments?

Guillem Marpons et al. (UPM) Coding Rule Checking Madrid December 2008 18 / 18