blob: b39ab35fe1ed5190cac1061187d383ab949cdf6a [file] [log] [blame] [view]
Karl Wibergbb821e22017-09-03 03:02:121# WebRTC coding style guide
2
3## C++
4
5WebRTC follows the [Chromium][chr-style] and [Google][goog-style] C++
Karl Wiberge1ed2412017-09-06 09:10:236style guides. In cases where they conflict, the Chromium style guide
7trumps the Google style guide, and the rules in this file trump them
8both.
Karl Wibergbb821e22017-09-03 03:02:129
10[chr-style]: https://chromium.googlesource.com/chromium/src/+/master/styleguide/c++/c++.md
11[goog-style]: https://google.github.io/styleguide/cppguide.html
12
Karl Wibergfb4e6772017-09-04 09:27:3313Some older parts of the code violate the style guide in various ways.
14
15* If making small changes to such code, follow the style guide when
16 its reasonable to do so, but in matters of formatting etc., it is
17 often better to be consistent with the surrounding code.
18* If making large changes to such code, consider first cleaning it up
19 in a separate CL.
20
Karl Wiberge1ed2412017-09-06 09:10:2321### ArrayView
Karl Wibergbb821e22017-09-03 03:02:1222
Karl Wiberge1ed2412017-09-06 09:10:2323When passing an array of values to a function, use `rtc::ArrayView`
24whenever possiblethat is, whenever youre not passing ownership of
25the array, and dont allow the callee to change the array size.
26
27For example,
28
29instead of | use
30------------------------------------|---------------------
31`const std::vector<T>&` | `ArrayView<const T>`
32`const T* ptr, size_t num_elements` | `ArrayView<const T>`
33`T* ptr, size_t num_elements` | `ArrayView<T>`
34
35See [the source](webrtc/api/array_view.h) for more detailed docs.
Karl Wibergfb4e6772017-09-04 09:27:3336
37## C
38
39Theres a substantial chunk of legacy C code in WebRTC, and a lot of
40it is old enough that it violates the parts of the C++ style guide
41that also applies to C (naming etc.) for the simple reason that it
42pre-dates the use of the current C++ style guide for this code base.
43
44* If making small changes to C code, mimic the style of the
45 surrounding code.
46* If making large changes to C code, consider converting the whole
47 thing to C++ first.
Karl Wibergee3d7602017-09-04 12:46:4748
49## Build files
50
51### Conditional compilation with the C preprocessor
52
53Avoid using the C preprocessor to conditionally enable or disable
54pieces of code. But if you cant avoid it, introduce a gn variable,
55and then set a preprocessor constant to either 0 or 1 in the build
56targets that need it:
57
58```
59if (apm_debug_dump) {
60 defines = [ "WEBRTC_APM_DEBUG_DUMP=1" ]
61} else {
62 defines = [ "WEBRTC_APM_DEBUG_DUMP=0" ]
63}
64```
65
66In the C, C++, or Objective-C files, use `#if` when testing the flag,
67not `#ifdef` or `#if defined()`:
68
69```
70#if WEBRTC_APM_DEBUG_DUMP
71// One way.
72#else
73// Or another.
74#endif
75```
76
77When combined with the `-Wundef` compiler option, this produces
78compile time warnings if preprocessor symbols are misspelled, or used
79without corresponding build rules to set them.