code-review-guidelines.rst 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. Code Review Guidelines
  2. ======================
  3. Why do we do code reviews?
  4. --------------------------
  5. The main goal of code reviews is to improve the code quality. By reviewing each
  6. other's code, we can help catch issues that were missed by the author
  7. before they are integrated in the source tree. Different people bring different
  8. perspectives, depending on their past work, experiences and their current use
  9. cases of TF-A in their products.
  10. Code reviews also play a key role in sharing knowledge within the
  11. community. People with more expertise in one area of the code base can
  12. help those that are less familiar with it.
  13. Code reviews are meant to benefit everyone through team work. It is not about
  14. unfairly criticizing or belittling the work of any contributor.
  15. Overview of the code review process
  16. -----------------------------------
  17. All contributions to Trusted Firmware-A project are reviewed by the community to
  18. ensure they meet the project's expectations before they get merged, according to
  19. the `Project Maintenance Process`_ defined for all `Trusted Firmware` projects.
  20. Technical ownership of most parts of the codebase falls on the :ref:`code
  21. owners`. All patches are ultimately merged by the :ref:`maintainers`.
  22. Approval of a patch is tracked using Gerrit `labels`. For a patch to be merged,
  23. it must get all of the following votes:
  24. - At least one ``Code-Owner-Review+1`` up-vote, and no ``Code-Owner-Review-1``
  25. down-vote.
  26. - At least one ``Maintainer-Review+1`` up-vote, and no ``Maintainer-Review-1``
  27. down-vote.
  28. - ``Verified+1`` vote applied by the automated Continuous Integration (CI)
  29. system.
  30. Note that, in some instances, the maintainers might give a waiver for some of
  31. the CI failures and manually override the ``Verified+1`` score.
  32. Good practices for all reviewers
  33. --------------------------------
  34. To ensure the code review gives the greatest possible benefit, participants in
  35. the project should:
  36. - Be considerate of other people and their needs. Participants may be working
  37. to different timescales, and have different priorities. Keep this in
  38. mind - be gracious while waiting for action from others, and timely in your
  39. actions when others are waiting for you.
  40. - Review other people's patches where possible. The more active reviewers there
  41. are, the more quickly new patches can be reviewed and merged. Contributing to
  42. code review helps everyone in the long run, as it creates a culture of
  43. participation which serves everyone's interests.
  44. Guidelines for patch contributors
  45. ---------------------------------
  46. In addition to the rules outlined in the :ref:`Contributor's Guide`, as a patch
  47. contributor you are expected to:
  48. - Answer all comments from people who took the time to review your
  49. patches.
  50. - Be patient and resilient. It is quite common for patches to go through
  51. several rounds of reviews and rework before they get approved, especially
  52. for larger features.
  53. In the event that a code review takes longer than you would hope for, you
  54. may try the following actions to speed it up:
  55. - Ping the reviewers on Gerrit or on the mailing list. If it is urgent,
  56. explain why. Please remain courteous and do not abuse this.
  57. - If one code owner has become unresponsive, ask the other code owners for
  58. help progressing the patch.
  59. - If there is only one code owner and they have become unresponsive, ask one
  60. of the project maintainers for help.
  61. - Do the right thing for the project, not the fastest thing to get code merged.
  62. For example, if some existing piece of code - say a driver - does not quite
  63. meet your exact needs, go the extra mile and extend the code with the missing
  64. functionality you require - as opposed to copying the code into some other
  65. directory to have the freedom to change it in any way. This way, your changes
  66. benefit everyone and will be maintained over time.
  67. - It is the patch-author's responsibility to respond to review comments within
  68. 21 days. In the event that the patch-author does not respond within this
  69. timeframe, the maintainer is entitled to abandon the patch(es).
  70. Patch author(s) may be busy with other priorities, causing a delay in
  71. responding to active review comments after posting patch(es). In such a
  72. situation, if the author's patch(es) is/are abandoned, they can restore
  73. their work for review by resolving comments, merge-conflicts, and revising
  74. their original submissions.
  75. Guidelines for all reviewers
  76. ----------------------------
  77. There are no good or bad review comments. If you have any doubt about a patch or
  78. need some clarifications, it's better to ask rather than letting a potential
  79. issue slip. Examples of review comments could be:
  80. - Questions ("Why do you need to do this?", "What if X happens?")
  81. - Bugs ("I think you need a logical \|\| rather than a bitwise \|.")
  82. - Design issues ("This won't scale well when we introduce feature X.")
  83. - Improvements ("Would it be better if we did Y instead?")
  84. Guidelines for code owners
  85. --------------------------
  86. Code owners are listed on the :ref:`Project Maintenance<code owners>` page,
  87. along with the module(s) they look after.
  88. When reviewing a patch, code owners are expected to check the following:
  89. - The patch looks good from a technical point of view. For example:
  90. - The structure of the code is clear.
  91. - It complies with the relevant standards or technical documentation (where
  92. applicable).
  93. - It leverages existing interfaces rather than introducing new ones
  94. unnecessarily.
  95. - It fits well in the design of the module.
  96. - It adheres to the security model of the project. In particular, it does not
  97. increase the attack surface (e.g. new SMCs) without justification.
  98. - The patch adheres to the TF-A :ref:`Coding Style`. The CI system should help
  99. catch coding style violations.
  100. - (Only applicable to generic code) The code is MISRA-compliant (see
  101. :ref:`misra-compliance`). The CI system should help catch violations.
  102. - Documentation is provided/updated (where applicable).
  103. - The patch has had an appropriate level of testing. Testing details are
  104. expected to be provided by the patch author. If they are not, do not hesitate
  105. to request this information.
  106. - All CI automated tests pass.
  107. If a code owner is happy with a patch, they should give their approval
  108. through the ``Code-Owner-Review+1`` label in Gerrit. If instead, they have
  109. concerns, questions, or any other type of blocking comment, they should set
  110. ``Code-Owner-Review-1``.
  111. Code owners are expected to behave professionally and responsibly. Here are some
  112. guidelines for them:
  113. - Once you are engaged in a review, make sure you stay involved until the patch
  114. is merged. Rejecting a patch and going away is not very helpful. You are
  115. expected to monitor the patch author's answers to your review comments,
  116. answer back if needed and review new revisions of their patch.
  117. - Provide constructive feedback. Just saying, "This is wrong, you should do X
  118. instead." is usually not very helpful. The patch author is unlikely to
  119. understand why you are requesting this change and might feel personally
  120. attacked.
  121. - Be mindful when reviewing a patch. As a code owner, you are viewed as
  122. the expert for the relevant module. By approving a patch, you are partially
  123. responsible for its quality and the effects it has for all TF-A users. Make
  124. sure you fully understand what the implications of a patch might be.
  125. Guidelines for maintainers
  126. --------------------------
  127. Maintainers are listed on the :ref:`Project Maintenance<maintainers>` page.
  128. When reviewing a patch, maintainers are expected to check the following:
  129. - The general structure of the patch looks good. This covers things like:
  130. - Code organization.
  131. - Files and directories, names and locations.
  132. For example, platform code should be added under the ``plat/`` directory.
  133. - Naming conventions.
  134. For example, platform identifiers should be properly namespaced to avoid
  135. name clashes with generic code.
  136. - API design.
  137. - Interaction of the patch with other modules in the code base.
  138. - The patch aims at complying with any standard or technical documentation
  139. that applies.
  140. - New files must have the correct license and copyright headers. See :ref:`this
  141. paragraph<copyright-license-guidance>` for more information. The CI system
  142. should help catch files with incorrect or no copyright/license headers.
  143. - There is no third party code or binary blobs with potential IP concerns.
  144. Maintainers should look for copyright or license notices in code, and use
  145. their best judgement. If they are unsure about a patch, they should ask
  146. other maintainers for help.
  147. - Generally speaking, new driver code should be placed in the generic
  148. layer. There are cases where a driver has to stay into the platform layer but
  149. this should be the exception, rather than the rule.
  150. - Existing common drivers (in particular for Arm IPs like the GIC driver) should
  151. not be copied into the platform layer to cater for platform quirks. This
  152. type of code duplication hurts the maintainability of the project. The
  153. duplicate driver is less likely to benefit from bug fixes and future
  154. enhancements. In most cases, it is possible to rework a generic driver to
  155. make it more flexible and fit slightly different use cases. That way, these
  156. enhancements benefit everyone.
  157. - When a platform specific driver really is required, the burden lies with the
  158. patch author to prove the need for it. A detailed justification should be
  159. posted via the commit message or on the mailing list.
  160. - Before merging a patch, verify that all review comments have been addressed.
  161. If this is not the case, encourage the patch author and the relevant
  162. reviewers to resolve these together.
  163. If a maintainer is happy with a patch, they should give their approval
  164. through the ``Maintainer-Review+1`` label in Gerrit. If instead, they have
  165. concerns, questions, or any other type of blocking comment, they should set
  166. ``Maintainer-Review-1``.
  167. --------------
  168. *Copyright (c) 2020-2023, Arm Limited. All rights reserved.*
  169. .. _Project Maintenance Process: https://trusted-firmware-docs.readthedocs.io/en/latest/generic_processes/project_maintenance_process.html