Add .clang-format
dblume

dblume commited on 2023-08-12 23:24:26
Showing 13 changed files, with 103 additions and 45 deletions.

... ...
@@ -0,0 +1,37 @@
1
+---
2
+
3
+Language:        Cpp
4
+BasedOnStyle:    Google
5
+AlignOperands:   false
6
+AllowAllArgumentsOnNextLine: true
7
+AllowAllParametersOfDeclarationOnNextLine: true
8
+AllowShortCaseLabelsOnASingleLine: false
9
+AllowShortFunctionsOnASingleLine: Empty
10
+AllowShortIfStatementsOnASingleLine: WithoutElse
11
+AllowShortLoopsOnASingleLine: false
12
+BinPackArguments: true
13
+BinPackParameters: true
14
+
15
+# PackConstructorInitializers: NextLine  # <-- Preferred, but in v14
16
+ConstructorInitializerAllOnOneLineOrOnePerLine: true
17
+AllowAllConstructorInitializersOnNextLine: true
18
+
19
+# Support vim navigation with [[ and ]]
20
+BreakBeforeBraces: Custom
21
+BraceWrapping:
22
+  AfterClass: true
23
+  AfterFunction: true
24
+  AfterStruct: true
25
+
26
+BreakBeforeBinaryOperators: NonAssignment
27
+BreakConstructorInitializers: AfterColon
28
+DerivePointerAlignment: false
29
+SortIncludes:    false
30
+SpacesBeforeTrailingComments: 1
31
+AccessModifierOffset: -4
32
+IndentWidth: 4
33
+ContinuationIndentWidth: 8
34
+ConstructorInitializerIndentWidth: 8
35
+IndentCaseLabels: false
36
+ColumnLimit: 86
37
+...
... ...
@@ -57,6 +57,23 @@ Or, if it's not in your path, something like...
57 57
 
58 58
     /mnt/c/Users/$USER/AppData/Local/Programs/Microsoft\ VS\ Code/bin/code .
59 59
 
60
+
61
+### Formatting
62
+
63
+Use clang-format. Install with:
64
+
65
+    sudo apt install clang-format
66
+
67
+Then, after making your edits, use the [vim integration](https://clang.llvm.org/docs/ClangFormat.html#vim-integration) or manually format with:
68
+
69
+    git status --porcelain | \
70
+    awk 'match($1, "M|A") && match($2, "(c|h)(pp)?$"){print $2}' | \
71
+    xargs clang-format -style=file -i
72
+
73
+Or, shorter but less comprehensive:
74
+
75
+    git ls-files -m | egrep ".(c|h)(pp)?$" | xargs clang-format -style=file -i
76
+
60 77
 ### Other IDEs
61 78
 
62 79
 Look for the presence of their project files. They should work normally.
... ...
@@ -17,6 +18,6 @@ public:
17 18
 int my_lib_fn();
18 19
 int my_lib_fn(int x);
19 20
 
20
-}
21
+} // namespace MY_LIB
21 22
 
22 23
 #endif
... ...
@@ -3,22 +3,20 @@
3 3
 
4 4
 using namespace std;
5 5
 
6
-namespace
7
-{
6
+namespace {
8 7
 
9
-  int my_lib_helper_fn(int x) {
10
-    cout << "The function " << __func__ << " with parameter " << x << " was called." << endl;
8
+int my_lib_helper_fn(int x)
9
+{
10
+    cout << "The function " << __func__ << " with parameter " << x << " was called."
11
+         << endl;
11 12
     return x + 1;
12 13
 }
13 14
 
14
-}
15
+} // namespace
15 16
 
16 17
 namespace MY_LIB {
17 18
 
18
-my_lib_rect::my_lib_rect(int width, int height):
19
-  width_(width),
20
-  height_(height)
21
-{}
19
+my_lib_rect::my_lib_rect(int width, int height) : width_(width), height_(height) {}
22 20
 
23 21
 int my_lib_rect::area() const
24 22
 {
... ...
@@ -34,8 +32,9 @@ int my_lib_fn()
34 32
 int my_lib_fn(int x)
35 33
 {
36 34
     int a = my_lib_helper_fn(x);
37
-    cout << "The function " << __PRETTY_FUNCTION__ << " with parameter " << x << " was called." << endl;
35
+    cout << "The function " << __PRETTY_FUNCTION__ << " with parameter " << x
36
+         << " was called." << endl;
38 37
     return x + a;
39 38
 }
40 39
 
41
-}
40
+} // namespace MY_LIB
... ...
@@ -32,7 +32,8 @@ void do_work_on_left(bool have_right, int32_t tick)
32 32
     std::lock_guard<std::mutex> lm(left_chopstick);
33 33
     std::this_thread::sleep_for(std::chrono::milliseconds(get_rnd()));
34 34
     if (have_right) {
35
-        std::cout << "deadlock::thread " << std::hex << this_thread::get_id() << " " << tick << endl;
35
+        std::cout << "deadlock::thread " << std::hex << this_thread::get_id() << " "
36
+                  << tick << endl;
36 37
     } else {
37 38
         do_work_on_right(true, tick);
38 39
     }
... ...
@@ -43,7 +44,8 @@ void do_work_on_right(bool have_left, int32_t tick)
43 44
     std::lock_guard<std::mutex> rm(right_chopstick);
44 45
     std::this_thread::sleep_for(std::chrono::milliseconds(get_rnd()));
45 46
     if (have_left) {
46
-        std::cout << "deadlock::thread " << std::hex << this_thread::get_id() << " " << tick << endl;
47
+        std::cout << "deadlock::thread " << std::hex << this_thread::get_id() << " "
48
+                  << tick << endl;
47 49
     } else {
48 50
         do_work_on_left(true, tick);
49 51
     }
... ...
@@ -56,7 +58,7 @@ void work(int32_t ticks)
56 58
     }
57 59
 }
58 60
 
59
-};
61
+}; // namespace
60 62
 
61 63
 int deadlock()
62 64
 {
... ...
@@ -113,7 +113,8 @@ int main()
113 113
     length_of_array = 6;
114 114
     int b[6] = {10, 20, 20, 30, 40, 40};
115 115
     length_of_array = remove_duplicates(b, length_of_array);
116
-    cout << "Removing duplicates yielded " << length_of_array << " items " << b << endl;
116
+    cout << "Removing duplicates yielded " << length_of_array << " items " << b
117
+         << endl;
117 118
 
118 119
     main_helper_fn();
119 120
     UI::widget_cb();
... ...
@@ -130,8 +131,10 @@ int main()
130 131
         scoped_set_adder another_set_adder("and another set_adder");
131 132
         cout << "unique_ptr " << (up ? "passes" : "fails") << " if." << endl;
132 133
         delete up.release();
133
-        cout << "unique_ptr after release " << (up ? "passes" : "fails") << " if." << endl;
134
-        cout << "unique_ptr.get() after release " << (up.get() ? "passes" : "fails") << " if." << endl;
134
+        cout << "unique_ptr after release " << (up ? "passes" : "fails") << " if."
135
+             << endl;
136
+        cout << "unique_ptr.get() after release " << (up.get() ? "passes" : "fails")
137
+             << " if." << endl;
135 138
         scoped_set_adder::dump();
136 139
     }
137 140
 
... ...
@@ -144,9 +147,12 @@ int main()
144 147
 
145 148
     // cout << "deadlock() returned " << deadlock() << endl;
146 149
 
147
-    for(unsigned i : {0, 1, 2, 3, 4, 5, 6, 7, 31, 32, 33, 4003, 65535, 65536, 65537}) {
148
-        cout << "next_power_of_two(" << i << ") = " << round_up_to_next_power_of_two(i) << endl;
149
-        assert(round_up_to_next_power_of_two(i) == another_round_up_to_next_power_of_two(i));
150
+    for (unsigned i :
151
+         {0, 1, 2, 3, 4, 5, 6, 7, 31, 32, 33, 4003, 65535, 65536, 65537}) {
152
+        cout << "next_power_of_two(" << i
153
+             << ") = " << round_up_to_next_power_of_two(i) << endl;
154
+        assert(round_up_to_next_power_of_two(i)
155
+               == another_round_up_to_next_power_of_two(i));
150 156
     }
151 157
     cout << "Done." << endl;
152 158
     return 0;
... ...
@@ -26,15 +25,13 @@ int main_helper_fn()
26 25
 class MyClass::Impl
27 26
 {
28 27
 public:
29
-    void f() {
28
+    void f()
29
+    {
30 30
         cout << "The function " << __PRETTY_FUNCTION__ << " was called." << endl;
31 31
     };
32 32
 };
33 33
 
34
-MyClass::MyClass()
35
-  : pimpl{std::make_unique<Impl>()}
36
-{
37
-}
34
+MyClass::MyClass() : pimpl{std::make_unique<Impl>()} {}
38 35
 
39 36
 // See http://www.cppsamples.com/common-tasks/pimpl.html
40 37
 MyClass::~MyClass() = default;
... ...
@@ -7,7 +7,7 @@
7 7
 namespace {
8 8
 std::mutex g_mutex;
9 9
 std::set<scoped_set_adder*> g_items;
10
-}
10
+} // namespace
11 11
 
12 12
 scoped_set_adder::scoped_set_adder(std::string const& name) : name_(name)
13 13
 {
... ...
@@ -25,6 +25,7 @@ bool scoped_set_adder::dump()
25 25
 {
26 26
     std::lock_guard<std::mutex> lock(g_mutex);
27 27
     bool ret = (g_items.size() > 0);
28
-    std::cout << "Scoped sets exist: " << ret << ", count: " << g_items.size() << std::endl;
28
+    std::cout << "Scoped sets exist: " << ret << ", count: " << g_items.size()
29
+              << std::endl;
29 30
     return ret;
30 31
 }
... ...
@@ -3,15 +3,14 @@
3 3
 #include "thread_manager.hpp"
4 4
 
5 5
 // using namespace std;
6
-using std::thread;
7
-using std::mutex;
8 6
 using std::condition_variable;
9
-using std::chrono::seconds;
10
-using std::unique_lock;
11
-using std::lock_guard;
12 7
 using std::cout;
13 8
 using std::endl;
14
-
9
+using std::lock_guard;
10
+using std::mutex;
11
+using std::thread;
12
+using std::unique_lock;
13
+using std::chrono::seconds;
15 14
 
16 15
 /*
17 16
 class Reporter {
... ...
@@ -26,10 +25,7 @@ class Worker {
26 25
 };
27 26
 */
28 27
 
29
-
30
-Manager::Manager():
31
-    abort_reporting_(false),
32
-    data_to_protect_(0)
28
+Manager::Manager() : abort_reporting_(false), data_to_protect_(0)
33 29
 {
34 30
     report_thread_ = thread([this] { WriteReport(); });
35 31
 }
... ...
@@ -3,14 +3,12 @@
3 3
 #include <iostream>
4 4
 #include "thread_with_lambda.hpp"
5 5
 
6
-
7
-namespace
8
-{
6
+namespace {
9 7
 
10 8
 std::mutex my_mutex;
11 9
 std::thread worker;
12 10
 
13
-}
11
+} // namespace
14 12
 
15 13
 void thread_with_lambda()
16 14
 {
... ...
@@ -13,5 +13,4 @@ int widget_cb()
13 13
     return 0;
14 14
 }
15 15
 
16
-}
17
-
16
+} // namespace UI
... ...
@@ -7,6 +7,6 @@ namespace UI {
7 7
 
8 8
 int widget_cb();
9 9
 
10
-}
10
+} // namespace UI
11 11
 
12 12
 #endif
... ...
@@ -0,0 +1,5 @@
1
+#ifndef xxx
2
+
3
+#define undefined
4
+
5
+#endif
0 6