Using Clang for fun and profit Examples from the Chromium project - - PowerPoint PPT Presentation

using clang for fun and profit
SMART_READER_LITE
LIVE PREVIEW

Using Clang for fun and profit Examples from the Chromium project - - PowerPoint PPT Presentation

Using Clang for fun and profit Examples from the Chromium project Hans Wennborg hwennborg (at) google.com GOTO Aarhus 2013 Outline Chromium Background Numbers Clang Background Error Messages Warnings Tools Conclusion Introducing


slide-1
SLIDE 1

Using Clang for fun and profit

Examples from the Chromium project

Hans Wennborg

hwennborg (at) google.com GOTO Aarhus 2013

slide-2
SLIDE 2

Outline

Chromium Background Numbers Clang Background Error Messages Warnings Tools Conclusion

slide-3
SLIDE 3

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.

slide-4
SLIDE 4

Chromium vs. Chrome

+                    Branding Quality assurance Crash reports Auto updates PDF Flash Audio/Video codecs =

slide-5
SLIDE 5

The Code

Million lines

Code Comments Space

1 2 3 4 5 6 7 8 2009 2010 2011 2012 2013

slide-6
SLIDE 6

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 ◮ . . .

slide-7
SLIDE 7

The People

Committers per month 100 200 300 400 500 600 2009 2010 2011 2012 2013

slide-8
SLIDE 8

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.

slide-9
SLIDE 9

Clang in Chromium

◮ Used since 2010 ◮ Work on 20% time by three Googlers ◮ Used for Mac binaries, and more.

slide-10
SLIDE 10

What makes Clang interesting?

◮ Fast ◮ Good output ◮ Clear error messages ◮ High quality warnings ◮ Hackable and extendable.

slide-11
SLIDE 11

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

slide-12
SLIDE 12

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.

slide-13
SLIDE 13

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) ^

slide-14
SLIDE 14

Typo correction

a.cc:5:9: error: use of undeclared identifier ’dout’; did you mean ’cout’? dout << "Hello, world!" << endl; ^~~~ cout

slide-15
SLIDE 15

The Conditional Operator

int f(bool b, int x, int y) { return 7 + b ? x : y; }

slide-16
SLIDE 16

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.

slide-17
SLIDE 17

The Boolean Accident

void f(bool *delete_user_data) { if (!migrate_data_to_new_location()) { delete_user_data = false; return; } ... }

slide-18
SLIDE 18

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; ^~~~~

slide-19
SLIDE 19

Hackability

◮ Clang is extendable ◮ It is a set of libraries ◮ We can use it to build tools.

slide-20
SLIDE 20

Chromium Style Checker

◮ Mark functions virtual explicitly ◮ Use override for overriding functions ◮ . . .

slide-21
SLIDE 21

Chromium Style Checker

a.cc:8:5: warning: [chromium-style] Overriding method must have "virtual" keyword. void f(); ^ virtual 1 warnings generated.

slide-22
SLIDE 22

clang-format

◮ Formatting is important ◮ Formatting is boring ◮ Automatic formatting of C++ is hard.

slide-23
SLIDE 23

AddressSanitizer (ASan)

◮ Fast memory error detector for C, C++, etc. ◮ Compile-time instrumentation ◮ Typically ca 2x slow-down ◮ Catches many kinds of errors.

slide-24
SLIDE 24

ThreadSanitizer (TSan)

◮ Data race detector for C, C++, etc. ◮ Uses Clang/LLVM to insert compile-time

instrumentation

◮ Overhead is large but not crazy ◮ Points out racy situations.

slide-25
SLIDE 25

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.

slide-26
SLIDE 26

Fun and Profit.

slide-27
SLIDE 27

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/