using clang for fun and profit - examples from the chromium project

27
Using Clang for fun and profit Examples from the Chromium project Hans Wennborg hwennborg (at) google.com GOTO Aarhus 2013

Upload: vuongdieu

Post on 04-Feb-2017

227 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Using Clang for fun and profit - Examples from the Chromium project

Using Clang for fun and profitExamples from the Chromium project

Hans Wennborghwennborg (at) google.com

GOTO Aarhus 2013

Page 2: Using Clang for fun and profit - Examples from the Chromium project

Outline

ChromiumBackgroundNumbers

ClangBackgroundError MessagesWarningsTools

Conclusion

Page 3: Using Clang for fun and profit - Examples from the Chromium project

Introducing Chrome

◮ A web browser from Google

◮ First released 2008

◮ Pushing the web forward

◮ 750 M active users (May 2013)

◮ Windows, Mac, Linux, ChromeOS, Android, iOS

◮ Mostly open source.

Page 4: Using Clang for fun and profit - Examples from the Chromium project

Chromium vs. Chrome

+

BrandingQuality assuranceCrash reportsAuto updatesPDFFlashAudio/Video codecs

=

Page 5: Using Clang for fun and profit - Examples from the Chromium project

The CodeM

illio

nlin

es

Code Comments Space

0

1

2

3

4

5

6

7

8

2009 2010 2011 2012 2013

Page 6: Using Clang for fun and profit - Examples from the Chromium project

The Code (continued)

5 M lines more in third-party libraries:

◮ Blink

◮ V8

◮ angle

◮ breakpad

◮ ffmpeg

◮ flac

◮ gmock

◮ googleurl

◮ gtest

◮ hunspell

◮ icu

◮ jsoncpp

◮ leveldb

◮ libjingle

◮ libsrtp

◮ libvpx

◮ lss

◮ NaCl

◮ nss

◮ sfntly

◮ skia

◮ webrtc

◮ . . .

Page 7: Using Clang for fun and profit - Examples from the Chromium project

The PeopleCom

mitte

rsper

mon

th

0

100

200

300

400

500

600

2009 2010 2011 2012 2013

Page 8: Using Clang for fun and profit - Examples from the Chromium project

Clang

◮ Up-and-coming C++, C, Objective-C compiler

◮ Part of the LLVM project

◮ Announced 2007 by Apple

◮ Production quality compiler since ca 2010

◮ Open-source, BSD-style license

◮ Designed to be GCC compatible.

Page 9: Using Clang for fun and profit - Examples from the Chromium project

Clang in Chromium

◮ Used since 2010

◮ Work on 20% time by three Googlers

◮ Used for Mac binaries, and more.

Page 10: Using Clang for fun and profit - Examples from the Chromium project

What makes Clang interesting?

◮ Fast

◮ Good output

◮ Clear error messages

◮ High quality warnings

◮ Hackable and extendable.

Page 11: Using Clang for fun and profit - Examples from the Chromium project

The missing semicolon (GCC 4.6)

int f(int x) {

int s = 0

for (int i = 0; i < x; ++i)

s += i;

return s;

}

a.cc: In function ’int f(int)’:

a.cc:3:9: error: expected ’,’ or ’;’ before ’for’

a.cc:3:25: error: ’i’ was not declared in this scope

a.cc:3:35: error: expected ’;’ before ’)’ token

Page 12: Using Clang for fun and profit - Examples from the Chromium project

The missing semicolon (Clang)

int f(int x) {

int s = 0

for (int i = 0; i < x; ++i)

s += i;

return s;

}

a.cc:2:18: error: expected ’;’ at end of declaration

int s = 0

^

;

1 error generated.

Page 13: Using Clang for fun and profit - Examples from the Chromium project

The missing semicolon (GCC 4.9 trunk)

a.cc: In function ’int f(int)’:

a.cc:3:9: error: expected ’,’ or ’;’ before ’for’

for (int i = 0; i < x; ++i)

^

a.cc:3:25: error: ’i’ was not declared in this scope

for (int i = 0; i < x; ++i)

^

Page 14: Using Clang for fun and profit - Examples from the Chromium project

Typo correction

a.cc:5:9: error: use of undeclared identifier ’dout’;

did you mean ’cout’?

dout << "Hello, world!" << endl;

^~~~

cout

Page 15: Using Clang for fun and profit - Examples from the Chromium project

The Conditional Operator

int f(bool b, int x, int y) {

return 7 + b ? x : y;

}

Page 16: Using Clang for fun and profit - Examples from the Chromium project

The Conditional Operator

a.cc:2:16: warning: operator ’?:’ has lower precedence

than ’+’; ’+’ will be evaluated first

return 7 + b ? x : y;

~~~~~ ^

a.cc:2:16: note: place parentheses around the ’?:’

expression to evaluate it first

return 7 + b ? x : y;

^

( )

1 warning generated.

Page 17: Using Clang for fun and profit - Examples from the Chromium project

The Boolean Accident

void f(bool *delete_user_data) {

if (!migrate_data_to_new_location()) {

delete_user_data = false;

return;

}

...

}

Page 18: Using Clang for fun and profit - Examples from the Chromium project

The Boolean Accident

a.cc:5:36: warning: initialization of pointer of type

’bool *’ to null from a constant boolean expression

delete_user_data = false;

^~~~~

Page 19: Using Clang for fun and profit - Examples from the Chromium project

Hackability

◮ Clang is extendable

◮ It is a set of libraries

◮ We can use it to build tools.

Page 20: Using Clang for fun and profit - Examples from the Chromium project

Chromium Style Checker

◮ Mark functions virtual explicitly

◮ Use override for overriding functions

◮ . . .

Page 21: Using Clang for fun and profit - Examples from the Chromium project

Chromium Style Checker

a.cc:8:5: warning: [chromium-style] Overriding

method must have "virtual" keyword.

void f();

^

virtual

1 warnings generated.

Page 22: Using Clang for fun and profit - Examples from the Chromium project

clang-format

◮ Formatting is important

◮ Formatting is boring

◮ Automatic formatting of C++ is hard.

Page 23: Using Clang for fun and profit - Examples from the Chromium project

AddressSanitizer (ASan)

◮ Fast memory error detector for C, C++, etc.

◮ Compile-time instrumentation

◮ Typically ca 2x slow-down

◮ Catches many kinds of errors.

Page 24: Using Clang for fun and profit - Examples from the Chromium project

ThreadSanitizer (TSan)

◮ Data race detector for C, C++, etc.

◮ Uses Clang/LLVM to insert compile-timeinstrumentation

◮ Overhead is large but not crazy

◮ Points out racy situations.

Page 25: Using Clang for fun and profit - Examples from the Chromium project

Undefined Behavior Sanitizer (UBSan)

◮ C and C++ have a thing called undefined behaviour

◮ Division by zero, array overflow, NULL ptr deref, etc.

◮ Helps efficient language implementation

◮ Also source of subtle bugs

◮ UBSan tries to catch those bugs for you.

Page 26: Using Clang for fun and profit - Examples from the Chromium project

Fun and Profit.

Page 27: Using Clang for fun and profit - Examples from the Chromium project

References

◮ www.chromium.org

◮ clang.llvm.org

◮ blog.llvm.org/2013/09/clang-warnings.html

◮ code.google.com/p/chromium/wiki/WritingClangPlugins

◮ clang.llvm.org/docs/ClangFormat.html

◮ code.google.com/p/address-sanitizer/