Lint-like checks and beyond - LLVMllvm.org/devmtg/2014-04/PDFs/Talks/clang-tidy LLVM Euro...

28
clang-tidy Lint-like checks and beyond (Daniel Jasper - [email protected])

Transcript of Lint-like checks and beyond - LLVMllvm.org/devmtg/2014-04/PDFs/Talks/clang-tidy LLVM Euro...

Page 1: Lint-like checks and beyond - LLVMllvm.org/devmtg/2014-04/PDFs/Talks/clang-tidy LLVM Euro 2014.pdf · Goal Lint - a C program verifier: Flag code that is “.. likely to be bugs,

clang-tidy Lint-like checks and beyond

(Daniel Jasper - [email protected])

Page 2: Lint-like checks and beyond - LLVMllvm.org/devmtg/2014-04/PDFs/Talks/clang-tidy LLVM Euro 2014.pdf · Goal Lint - a C program verifier: Flag code that is “.. likely to be bugs,

Goal● Lint - a C program verifier:

Flag code that is “.. likely to be bugs, to be non-portable, or to be wasteful.” (lint man page)

● With all the knowledge from clang, detect:● Bug prone coding patterns● Enforce coding conventions● Advocate modern and maintainable code

Page 3: Lint-like checks and beyond - LLVMllvm.org/devmtg/2014-04/PDFs/Talks/clang-tidy LLVM Euro 2014.pdf · Goal Lint - a C program verifier: Flag code that is “.. likely to be bugs,

Example: Unnecessary copies

vector<string> SomeStrings = ...;for (string SomeString : SomeStrings) { someFunction(SomeString);}

Page 4: Lint-like checks and beyond - LLVMllvm.org/devmtg/2014-04/PDFs/Talks/clang-tidy LLVM Euro 2014.pdf · Goal Lint - a C program verifier: Flag code that is “.. likely to be bugs,

Example: Unnecessary copies

map<string, set<int>> SomeStringMap;for (const pair<string, set<int>> &e : SomeStringMap) { v.push_back(&e.second);}

Page 5: Lint-like checks and beyond - LLVMllvm.org/devmtg/2014-04/PDFs/Talks/clang-tidy LLVM Euro 2014.pdf · Goal Lint - a C program verifier: Flag code that is “.. likely to be bugs,

Example: Unnecessary copies

vector<string> SomeStrings = ...;for (auto SomeString : SomeStrings) { someFunction(SomeString);}

Page 6: Lint-like checks and beyond - LLVMllvm.org/devmtg/2014-04/PDFs/Talks/clang-tidy LLVM Euro 2014.pdf · Goal Lint - a C program verifier: Flag code that is “.. likely to be bugs,

Why not build warnings into Clang?

Page 7: Lint-like checks and beyond - LLVMllvm.org/devmtg/2014-04/PDFs/Talks/clang-tidy LLVM Euro 2014.pdf · Goal Lint - a C program verifier: Flag code that is “.. likely to be bugs,

Why not build warnings into Clang?

Clang warning clang-tidy error

Page 8: Lint-like checks and beyond - LLVMllvm.org/devmtg/2014-04/PDFs/Talks/clang-tidy LLVM Euro 2014.pdf · Goal Lint - a C program verifier: Flag code that is “.. likely to be bugs,

False positivesvector<int> v;assert(!v.empty());for (int i = 0; i < v.size() - 1; ++i) { llvm::outs() << v[i];}

Page 9: Lint-like checks and beyond - LLVMllvm.org/devmtg/2014-04/PDFs/Talks/clang-tidy LLVM Euro 2014.pdf · Goal Lint - a C program verifier: Flag code that is “.. likely to be bugs,

Encapsulated implementation

include/clang/Sema/Sema.h (8.2k LOC)

Implemented in 35 files(lib/Sema/ - 146k LOC)

E.g. -Wunused-private-field:Sema.cpp,SemaDeclCXX.cpp,SemaExprMember.cpp

Page 10: Lint-like checks and beyond - LLVMllvm.org/devmtg/2014-04/PDFs/Talks/clang-tidy LLVM Euro 2014.pdf · Goal Lint - a C program verifier: Flag code that is “.. likely to be bugs,

General applicability

● Lint checks ..○ .. can be project specific

○ .. can be more expensive than Clang’s compilation (e.g. doing static code analysis)

○ .. are not needed during every compilation

Page 11: Lint-like checks and beyond - LLVMllvm.org/devmtg/2014-04/PDFs/Talks/clang-tidy LLVM Euro 2014.pdf · Goal Lint - a C program verifier: Flag code that is “.. likely to be bugs,

(clang-tidy)Clang Tool

Architecture

clang-tidy core

module A

module B

check 1

check 2

check N...

check 1

check 2

check N...

...

static analyzer

clang-formatcompilation database

Page 12: Lint-like checks and beyond - LLVMllvm.org/devmtg/2014-04/PDFs/Talks/clang-tidy LLVM Euro 2014.pdf · Goal Lint - a C program verifier: Flag code that is “.. likely to be bugs,

Clang-tidy check

Architecture

callback logic

AST matchers

Preprocessor hooks

diagnostics

Page 13: Lint-like checks and beyond - LLVMllvm.org/devmtg/2014-04/PDFs/Talks/clang-tidy LLVM Euro 2014.pdf · Goal Lint - a C program verifier: Flag code that is “.. likely to be bugs,

AST matchers

● Syntax tree (AST) matchers● A matcher finds specific entries of the AST● A callback gets invoked on every match

○ Can access the AST

a.cc AST Matcher callbackn

Page 14: Lint-like checks and beyond - LLVMllvm.org/devmtg/2014-04/PDFs/Talks/clang-tidy LLVM Euro 2014.pdf · Goal Lint - a C program verifier: Flag code that is “.. likely to be bugs,

AST matchers - ExampleMatch calls to method Get on class Elements

e = elements.Get(42);f = fish->Get(23);f.Cook();feed();

Page 15: Lint-like checks and beyond - LLVMllvm.org/devmtg/2014-04/PDFs/Talks/clang-tidy LLVM Euro 2014.pdf · Goal Lint - a C program verifier: Flag code that is “.. likely to be bugs,

AST matchers - ExampleMatch calls to method Get on class Elements

e = elements.Get(42);f = fish->Get(23);f.Cook();feed();

callExpr()

Page 16: Lint-like checks and beyond - LLVMllvm.org/devmtg/2014-04/PDFs/Talks/clang-tidy LLVM Euro 2014.pdf · Goal Lint - a C program verifier: Flag code that is “.. likely to be bugs,

AST matchers - ExampleMatch calls to method Get on class Elements

e = elements.Get(42);f = fish->Get(23);f.Cook();feed();

callExpr( callee())

Page 17: Lint-like checks and beyond - LLVMllvm.org/devmtg/2014-04/PDFs/Talks/clang-tidy LLVM Euro 2014.pdf · Goal Lint - a C program verifier: Flag code that is “.. likely to be bugs,

AST matchers - ExampleMatch calls to method Get on class Elements

e = elements.Get(42);f = fish->Get(23);f.Cook();feed();

callExpr( callee(methodDecl()))

Page 18: Lint-like checks and beyond - LLVMllvm.org/devmtg/2014-04/PDFs/Talks/clang-tidy LLVM Euro 2014.pdf · Goal Lint - a C program verifier: Flag code that is “.. likely to be bugs,

AST matchers - ExampleMatch calls to method Get on class Elements

e = elements.Get(42);f = fish->Get(23);f.Cook();feed();

callExpr( callee(methodDecl(hasName("Get"))))

Page 19: Lint-like checks and beyond - LLVMllvm.org/devmtg/2014-04/PDFs/Talks/clang-tidy LLVM Euro 2014.pdf · Goal Lint - a C program verifier: Flag code that is “.. likely to be bugs,

AST matchers - ExampleMatch calls to method Get on class Elements

e = elements.Get(42);f = fish->Get(23);f.Cook();feed();

callExpr( callee(methodDecl(hasName("Get"))), thisPointerType())

Page 20: Lint-like checks and beyond - LLVMllvm.org/devmtg/2014-04/PDFs/Talks/clang-tidy LLVM Euro 2014.pdf · Goal Lint - a C program verifier: Flag code that is “.. likely to be bugs,

AST matchers - ExampleMatch calls to method Get on class Elements

e = elements.Get(42);f = fish->Get(23);f.Cook();feed();

callExpr( callee(methodDecl(hasName("Get"))), thisPointerType(recordDecl()))

Page 21: Lint-like checks and beyond - LLVMllvm.org/devmtg/2014-04/PDFs/Talks/clang-tidy LLVM Euro 2014.pdf · Goal Lint - a C program verifier: Flag code that is “.. likely to be bugs,

AST matchers - ExampleMatch calls to method Get on class Elements

e = elements.Get(42);f = fish->Get(23);f.Cook();feed();

callExpr( callee(methodDecl(hasName("Get"))), thisPointerType(recordDecl( hasName("Elements"))))

Page 22: Lint-like checks and beyond - LLVMllvm.org/devmtg/2014-04/PDFs/Talks/clang-tidy LLVM Euro 2014.pdf · Goal Lint - a C program verifier: Flag code that is “.. likely to be bugs,

AST matchers - ExampleMatch calls to method Get on class Elements

e = elements.Get(42);f = fish->Get(23);f.Cook();feed();

callExpr( callee(methodDecl(hasName("Get"))), thisPointerType(recordDecl(...)), callee(memberExpr().bind("callee"))))

Page 23: Lint-like checks and beyond - LLVMllvm.org/devmtg/2014-04/PDFs/Talks/clang-tidy LLVM Euro 2014.pdf · Goal Lint - a C program verifier: Flag code that is “.. likely to be bugs,

Example: Explicit constructors

From Google’s C++ style guide:“Use the C++ keyword explicit for constructors with one argument. ...”

● Avoids accidental object construction

Page 24: Lint-like checks and beyond - LLVMllvm.org/devmtg/2014-04/PDFs/Talks/clang-tidy LLVM Euro 2014.pdf · Goal Lint - a C program verifier: Flag code that is “.. likely to be bugs,

Example: Explicit constructors class ExplicitConstructorCheck : public ClangTidyCheck {

public:

virtual void registerMatchers(ast_matchers::MatchFinder *Finder) {

Finder->addMatcher( constructorDecl().bind("constructor"), this);

}

};

Page 25: Lint-like checks and beyond - LLVMllvm.org/devmtg/2014-04/PDFs/Talks/clang-tidy LLVM Euro 2014.pdf · Goal Lint - a C program verifier: Flag code that is “.. likely to be bugs,

Example: Explicit constructors class ExplicitConstructorCheck : public ClangTidyCheck {

public:

virtual void registerMatchers(ast_matchers::MatchFinder *Finder) {

Finder->addMatcher( constructorDecl().bind("constructor"), this);

}

virtual void check(const ast_matchers::MatchFinder::MatchResult &Result) {

const CXXConstructorDecl *Ctor =

Result.Nodes.getNodeAs<CXXConstructorDecl>("constructor");

if (!Ctor->isExplicit() && !Ctor->isImplicit() &&

Ctor->getNumParams() >= 1 && Ctor->getMinRequiredArguments() <= 1) {

SourceLocation Loc = Ctor->getLocation();

diag(Loc, "Single-argument constructors must be explicit")

<< FixItHint::CreateInsertion(Loc, "explicit ");

}

}

};

Page 26: Lint-like checks and beyond - LLVMllvm.org/devmtg/2014-04/PDFs/Talks/clang-tidy LLVM Euro 2014.pdf · Goal Lint - a C program verifier: Flag code that is “.. likely to be bugs,

Demo time

Page 27: Lint-like checks and beyond - LLVMllvm.org/devmtg/2014-04/PDFs/Talks/clang-tidy LLVM Euro 2014.pdf · Goal Lint - a C program verifier: Flag code that is “.. likely to be bugs,

Missing pieces

● Many, many, many … more checks

● Cross-TU changes (A::SomeFunction() → A::someFunction())

● Per-project configuration (.clang-tidy file)

● VCS integration (Only check changed lines)

● clang-modernize integrations

● ...

Page 28: Lint-like checks and beyond - LLVMllvm.org/devmtg/2014-04/PDFs/Talks/clang-tidy LLVM Euro 2014.pdf · Goal Lint - a C program verifier: Flag code that is “.. likely to be bugs,

Thank you!(Now go and develop checks)