[PATCH] D74515: Add coding standard recommending use of qualifiers in cpp files

Reid Kleckner via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 12 16:40:40 PST 2020


rnk updated this revision to Diff 244303.
rnk added a comment.

- shorten example


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D74515/new/

https://reviews.llvm.org/D74515

Files:
  llvm/docs/CodingStandards.rst


Index: llvm/docs/CodingStandards.rst
===================================================================
--- llvm/docs/CodingStandards.rst
+++ llvm/docs/CodingStandards.rst
@@ -762,6 +762,47 @@
     It's okay to put extra implementation methods in a public class itself. Just
     make them private (or protected) and all is well.
 
+Use Name Qualifiers to Implement Previously Declared Functions
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+When providing an out of line implementation of a function in a source file, do
+not open namespace blocks in the source file. Instead, use namespace qualifiers
+to help ensure that your definition matches an existing declaration. Do this:
+
+.. code-block:: c++
+
+  // Foo.h
+  namespace llvm {
+  int foo(const char *s);
+  }
+
+  // Foo.cpp
+  #include "Foo.h"
+  using namespace llvm;
+  int llvm::foo(const char *s) {
+    // ...
+  }
+
+If every definition in the implementation file uses a previously declared
+qualifier, it helps to catch bugs where the definition does not match the
+declaration. In C++, this would declare a new overload of ``llvm::foo``, for
+example:
+
+.. code-block:: c++
+
+  // Foo.cpp
+  #include "Foo.h"
+  namespace llvm {
+  int foo(char *s) { // Mismatch between "const char *" and "char *"
+  }
+  } // end namespace llvm
+
+It is more likely that the programmer intended to implement
+``llvm::foo(const char *)`` in this case.
+
+Class method implementations must already name the class and new overloads
+cannot be introduced out of line, so this recommendation does not apply to them.
+
 .. _early exits:
 
 Use Early Exits and ``continue`` to Simplify Code


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D74515.244303.patch
Type: text/x-patch
Size: 1665 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200213/ca361df2/attachment.bin>


More information about the llvm-commits mailing list