diff --git a/llvm/docs/CodingStandards.rst b/llvm/docs/CodingStandards.rst --- a/llvm/docs/CodingStandards.rst +++ b/llvm/docs/CodingStandards.rst @@ -762,6 +762,60 @@ It's okay to put extra implementation methods in a public class itself. Just make them private (or protected) and all is well. +Open Namespaces in Header Files, Not Source Files +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +To declare things in a namespace in a header file, open a namespace in the usual +way: + +.. code-block:: c++ + + // Foo.h + namespace llvm { + class Foo { + public: + int foo(); + // ... + }; + int bar(Foo *p); + } + +When providing implementations in a source file for functions or methods +declared in a header 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.cpp + #include "Foo.h" + + using namespace llvm; + + int Foo::foo() { /* ... */ } + + int llvm::bar(Foo *p) { + // ... + } + +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::bar``, for +example: + +.. code-block:: c++ + + // Foo.cpp + #include "Foo.h" + + namespace llvm { + int bar(Bar *p) { // Note "Bar" does not match "Foo" above. + } + } // end namespace llvm + +It is more likely that the programmer intended to implement ``llvm::bar(Foo*)`` +in this case, but has not yet updated the prototype. + .. _early exits: Use Early Exits and ``continue`` to Simplify Code