1==============
2MyFirstTypoFix
3==============
4
5.. contents::
6   :local:
7
8Introduction
9============
10
11This tutorial will guide you through the process of making a change to
12LLVM, and contributing it back to the LLVM project. We'll be making a
13change to Clang, but the steps for other parts of LLVM are the same.
14Even though the change we'll be making is simple, we're going to cover
15steps like building LLVM, running the tests, and code review. This is
16good practice, and you'll be prepared for making larger changes.
17
18We'll assume you:
19
20-  know how to use an editor,
21
22-  have basic C++ knowledge,
23
24-  know how to install software on your system,
25
26-  are comfortable with the command line,
27
28-  have basic knowledge of git.
29
30
31The change we're making
32-----------------------
33
34Clang has a warning for infinite recursion:
35
36.. code:: console
37
38   $ echo "void foo() { foo(); }" > ~/test.cc
39   $ clang -c -Wall ~/test.cc
40   input.cc:1:14: warning: all paths through this function will call
41   itself [-Winfinite-recursion]
42
43This is clear enough, but not exactly catchy. Let's improve the wording
44a little:
45
46.. code:: console
47
48   input.cc:1:14: warning: to understand recursion, you must first
49   understand recursion [-Winfinite-recursion]
50
51
52Dependencies
53------------
54
55We're going to need some tools:
56
57-  git: to check out the LLVM source code,
58
59-  a C++ compiler: to compile LLVM source code. You'll want `a recent
60   version <https://llvm.org/docs/GettingStarted.html#host-c-toolchain-both-compiler-and-standard-library>`__
61   of Clang, GCC, or Visual Studio.
62
63-  CMake: used to configure how LLVM should be built on your system,
64
65-  ninja: runs the C++ compiler to (re)build specific parts of LLVM,
66
67-  python: to run the LLVM tests,
68
69-  arcanist: for uploading changes for review,
70
71As an example, on Ubuntu:
72
73.. code:: console
74
75   $ sudo apt-get install git clang cmake ninja-build python arcanist subversion
76
77
78Building LLVM
79=============
80
81
82Checkout
83--------
84
85The source code is stored `on
86Github <https://github.com/llvm/llvm-project>`__ in one large repository
87("the monorepo").
88
89It may take a while to download!
90
91.. code:: console
92
93   $ git clone https://github.com/llvm/llvm-project.git
94
95This will create a directory "llvm-project" with all of the source
96code.(Checking out anonymously is OK - pushing commits uses a different
97mechanism, as we'll see later)
98
99Configure your workspace
100------------------------
101
102Before we can build the code, we must configure exactly how to build it
103by running CMake. CMake combines information from three sources:
104
105-  explicit choices you make (is this a debug build?)
106
107-  settings detected from your system (where are libraries installed?)
108
109-  project structure (which files are part of 'clang'?)
110
111First, create a directory to build in. Usually, this is
112llvm-project/build.
113
114.. code:: console
115
116   $ mkdir llvm-project/build
117   $ cd llvm-project/build
118
119Now, run CMake:
120
121.. code:: console
122
123   $ cmake -G Ninja ../llvm -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_PROJECTS=clang
124
125If all goes well, you'll see a lot of "performing test" lines, and
126finally:
127
128.. code:: console
129
130   Configuring done
131   Generating done
132   Build files have been written to: /path/llvm-project/build
133
134And you should see a build.ninja file.
135
136Let's break down that last command a little:
137
138-  **-G Ninja**: we're going to use ninja to build; please create
139   build.ninja
140
141-  **../llvm**: this is the path to the source of the "main" LLVM
142   project
143
144-  The two **-D** flags set CMake variables, which override
145   CMake/project defaults:
146
147-  **CMAKE\ BUILD\ TYPE=Release**: build in optimized mode, which is
148   (surprisingly) the fastest option.
149
150   If you want to run under a debugger, you should use the default Debug
151   (which is totally unoptimized, and will lead to >10x slower test
152   runs) or RelWithDebInfo which is a halfway point.
153   **CMAKE\ BUILD\ TYPE** affects code generation only, assertions are
154   on by default regardless! **LLVM\ ENABLE\ ASSERTIONS=Off** disables
155   them.
156
157-  **LLVM\ ENABLE\ PROJECTS=clang** : this lists the LLVM subprojects
158   you are interested in building, in addition to LLVM itself. Multiple
159   projects can be listed, separated by semicolons, such as "clang;
160   lldb".In this example, we'll be making a change to Clang, so we
161   should build it.
162
163Finally, create a symlink (or a copy) of
164llvm-project/build/compile-commands.json into llvm-project/:
165
166.. code:: console
167
168   $ ln -s build/compile_commands.json ../
169
170(This isn't strictly necessary for building and testing, but allows
171tools like clang-tidy, clang-query, and clangd to work in your source
172tree).
173
174
175Build and test
176--------------
177
178Finally, we can build the code! It's important to do this first, to
179ensure we're in a good state before making changes. But what to build?
180In ninja, you specify a **target**. If we just want to build the clang
181binary, our target name is "clang" and we run:
182
183.. code:: console
184
185   $ ninja clang
186
187The first time we build will be very slow - Clang + LLVM is a lot of
188code. But incremental builds are fast: ninja will only rebuild the parts
189that have changed. When it finally finishes you should have a working
190clang binary. Try running:
191
192.. code:: console
193
194   $ bin/clang --version
195
196There's also a target for building and running all the clang tests:
197
198.. code:: console
199
200   $ ninja check-clang
201
202This is a common pattern in LLVM: check-llvm is all the checks for core,
203other projects have targets like check-lldb.
204
205
206Making changes
207==============
208
209
210Edit
211----
212
213We need to find the file containing the error message.
214
215.. code:: console
216
217   $ git grep "all paths through this function" ..
218   ../clang/include/clang/Basic/DiagnosticSemaKinds.td:  "all paths through this function will call itself">,
219
220The string that appears in DiagnosticSemaKinds.td is the one that is
221printed by Clang. \*.td files define tables - in this case it's a list
222of warnings and errors clang can emit and their messages. Let's update
223the message in your favorite editor:
224
225.. code:: console
226
227   $ vi ../clang/include/clang/Basic/DiagnosticSemaKinds.td
228
229Find the message (it should be under
230warn\ *infinite*\ recursive_function)Change the message to "in order to
231understand recursion, you must first understand recursion".
232
233
234Test again
235----------
236
237To verify our change, we can build clang and manually check that it
238works.
239
240.. code:: console
241
242   $ ninja clang
243   $ bin/clang -Wall ~/test.cc
244
245   **/path/test.cc:1:124:** **warning****: in order to understand recursion, you must
246   first understand recursion [-Winfinite-recursion]**
247
248We should also run the tests to make sure we didn't break something.
249
250.. code:: console
251
252   $ ninja check-clang
253
254Notice that it is much faster to build this time, but the tests take
255just as long to run. Ninja doesn't know which tests might be affected,
256so it runs them all.
257
258.. code:: console
259
260   ********************
261   Testing Time: 408.84s
262   ********************
263   Failing Tests (1):
264       Clang :: SemaCXX/warn-infinite-recursion.cpp
265
266Well, that makes sense… and the test output suggests it's looking for
267the old string "call itself" and finding our new message instead.
268
269Let's fix it by updating the expectation in the test.
270
271.. code:: console
272
273   $ vi ../clang/test/SemaCXX/warn-infinite-recursion.cpp
274
275Everywhere we see // expected-warning{{call itself}}, let's replace it
276with // expected-warning{{to understand recursion}}.
277
278Now we could run **all** the tests again, but this is a slow way to
279iterate on a change! Instead, let's find a way to re-run just the
280specific test. There are two main types of tests in LLVM:
281
282-  **lit tests** (e.g. SemaCXX/warn-infinite-recursion.cpp).
283
284These are fancy shell scripts that run command-line tools and verify the
285output. They live in files like
286clang/**test**/FixIt/dereference-addressof.c. Re-run like this:
287
288.. code:: console
289
290   $ bin/llvm-lit -v ../clang/test/SemaCXX/warn-infinite-recursion.cpp
291
292-  **unit tests** (e.g. ToolingTests/ReplacementText.CanDeleteAllText)
293
294These are C++ programs that call LLVM functions and verify the results.
295They live in suites like ToolingTests. Re-run like this:
296
297.. code:: console
298
299   $ ninja ToolingTests && tools/clang/unittests/Tooling/ToolingTests
300   --gtest_filter=ReplacementText.CanDeleteAllText
301
302
303Commit locally
304--------------
305
306We'll save the change to a local git branch. This lets us work on other
307things while the change is being reviewed. Changes should have a
308description, to explain to reviewers and future readers of the code why
309the change was made.
310
311.. code:: console
312
313   $ git checkout -b myfirstpatch
314   $ git commit -am "[Diagnostic] Clarify -Winfinite-recursion message"
315
316Now we're ready to send this change out into the world! By the way,
317There is a unwritten convention of using tag for your commit. Tags
318usually represent modules that you intend to modify. If you don't know
319the tags for your modules, you can look at the commit history :
320https://github.com/llvm/llvm-project/commits/main.
321
322
323Code review
324===========
325
326
327Finding a reviewer
328------------------
329
330Changes can be reviewed by anyone in the LLVM community who has commit
331access.For larger and more complicated changes, it's important that the
332reviewer has experience with the area of LLVM and knows the design goals
333well. The author of a change will often assign a specific reviewer (git
334blame and git log can be useful to find one).
335
336As our change is fairly simple, we'll add the cfe-commits mailing list
337as a subscriber; anyone who works on clang can likely pick up the
338review. (For changes outside clang, llvm-commits is the usual list. See
339`http://lists.llvm.org/ <http://lists.llvm.org/mailman/listinfo>`__ for
340all the \*-commits mailing lists).
341
342
343Uploading a change for review
344-----------------------------
345
346LLVM code reviews happen at https://reviews.llvm.org. The web interface
347is called Phabricator, and the code review part is Differential. You
348should create a user account there for reviews (click "Log In" and then
349"Register new account").
350
351Now you can upload your change for review:
352
353.. code:: console
354
355   $ arc diff HEAD^
356
357This creates a review for your change, comparing your current commit
358with the previous commit. You will be prompted to fill in the review
359details. Your commit message is already there, so just add cfe-commits
360under the "subscribers" section. It should print a code review URL:
361https://reviews.llvm.org/D58291 You can always find your active reviews
362on Phabricator under "My activity".
363
364
365Review process
366--------------
367
368When you upload a change for review, an email is sent to you, the
369cfe-commits list, and anyone else subscribed to these kinds of changes.
370Within a few days, someone should start the review. They may add
371themselves as a reviewer, or simply start leaving comments. You'll get
372another email any time the review is updated. The details are in the
373`https://llvm.org/docs/CodeReview/ <https://llvm.org/docs/CodeReview.html>`__.
374
375
376Comments
377~~~~~~~~
378
379The reviewer can leave comments on the change, and you can reply. Some
380comments are attached to specific lines, and appear interleaved with the
381code. You can either reply to these, or address them and mark them as
382"done". Note that in-line replies are **not** sent straight away! They
383become "draft" comments and you must click "Submit" at the bottom of the
384page.
385
386
387Updating your change
388~~~~~~~~~~~~~~~~~~~~
389
390If you make changes in response to a reviewer's comments, simply run
391
392.. code:: console
393
394   $ arc diff
395
396again to update the change and notify the reviewer. Typically this is a
397good time to send any draft comments as well.
398
399
400Accepting a revision
401~~~~~~~~~~~~~~~~~~~~
402
403When the reviewer is happy with the change, they will **Accept** the
404revision. They may leave some more minor comments that you should
405address, but at this point the review is complete. It's time to get it
406committed!
407
408
409Commit by proxy
410---------------
411
412As this is your first change, you won't have access to commit it
413yourself yet. The reviewer **doesn't know this**, so you need to tell
414them! Leave a message on the review like:
415
416   Thanks @somellvmdev. I don't have commit access, can you land this
417   patch for me? Please use "My Name my@email" to commit the change.
418
419The review will be updated when the change is committed.
420
421
422Review expectations
423-------------------
424
425In order to make LLVM a long-term sustainable effort, code needs to be
426maintainable and well tested. Code reviews help to achieve that goal.
427Especially for new contributors, that often means many rounds of reviews
428and push-back on design decisions that do not fit well within the
429overall architecture of the project.
430
431For your first patches, this means:
432
433-  be kind, and expect reviewers to be kind in return - LLVM has a `Code
434   of Conduct <https://llvm.org/docs/CodeOfConduct.html>`__;
435
436-  be patient - understanding how a new feature fits into the
437   architecture of the project is often a time consuming effort, and
438   people have to juggle this with other responsibilities in their
439   lives; **ping the review once a week** when there is no response;
440
441-  if you can't agree, generally the best way is to do what the reviewer
442   asks; we optimize for readability of the code, which the reviewer is
443   in a better position to judge; if this feels like it's not the right
444   option, you can contact the cfe-dev mailing list to get more feedback
445   on the direction;
446
447
448Commit access
449=============
450
451Once you've contributed a handful of patches to LLVM, start to think
452about getting commit access yourself. It's probably a good idea if:
453
454-  you've landed 3-5 patches of larger scope than "fix a typo"
455
456-  you'd be willing to review changes that are closely related to yours
457
458-  you'd like to keep contributing to LLVM.
459
460
461Getting commit access
462---------------------
463
464LLVM uses Git for committing changes. The details are in the `developer
465policy
466document <https://llvm.org/docs/DeveloperPolicy.html#obtaining-commit-access>`__.
467
468
469With great power
470----------------
471
472Actually, this would be a great time to read the rest of the `developer
473policy <https://llvm.org/docs/DeveloperPolicy.html>`__, too. At minimum,
474you need to be subscribed to the relevant commits list before landing
475changes (e.g. [email protected]), as discussion often happens
476there if a new patch causes problems.
477
478
479Commit
480------
481
482Let's say you have a change on a local git branch, reviewed and ready to
483commit. Things to do first:
484
485-  if you used multiple fine-grained commits locally, squash them into a
486   single commit. LLVM prefers commits to match the code that was
487   reviewed. (If you created one commit and then used "arc diff", you're
488   fine)
489
490-  rebase your patch against the latest LLVM code. LLVM uses a linear
491   history, so everything should be based on an up-to-date origin/main.
492
493.. code:: console
494
495   $ git pull --rebase https://github.com/llvm/llvm-project.git main
496
497-  ensure the patch looks correct.
498
499.. code:: console
500
501   $ git show
502
503-  run the tests one last time, for good luck
504
505At this point git show should show a single commit on top of
506origin/main.
507
508Now you can push your commit with
509
510.. code:: console
511
512   $ git push https://github.com/llvm/llvm-project.git HEAD:main
513
514You should see your change `on
515GitHub <https://github.com/llvm/llvm-project/commits/main>`__ within
516minutes.
517
518
519Post-commit errors
520------------------
521
522Once your change is submitted it will be picked up by automated build
523bots that will build and test your patch in a variety of configurations.
524
525You can see all configurations and their current state in a waterfall
526view at http://lab.llvm.org:8011/waterfall. The waterfall view is good
527to get a general overview over the tested configurations and to see
528which configuration have been broken for a while.
529
530The console view at http://lab.llvm.org:8011/console helps to get a
531better understanding of the build results of a specific patch. If you
532want to follow along how your change is affecting the build bots, **this
533should be the first place to look at** - the colored bubbles correspond
534to projects in the waterfall.
535
536If you see a broken build, do not despair - some build bots are
537continuously broken; if your change broke the build, you will see a red
538bubble in the console view, while an already broken build will show an
539orange bubble. Of course, even when the build was already broken, a new
540change might introduce a hidden new failure.
541
542| When you want to see more details how a specific build is broken,
543  click the red bubble.
544| If post-commit error logs confuse you, do not worry too much -
545  everybody on the project is aware that this is a bit unwieldy, so
546  expect people to jump in and help you understand what's going on!
547
548buildbots, overview of bots, getting error logs.
549
550
551Reverts
552-------
553
554if in doubt, revert and re-land.
555
556
557Conclusion
558==========
559
560llvm is a land of contrasts.
561