[llvm] [LLVM][CodingStandard] Extend namespace qualifier rule to variables (PR #163588)
Rahul Joshi via llvm-commits
llvm-commits at lists.llvm.org
Mon Oct 20 09:27:15 PDT 2025
https://github.com/jurahul updated https://github.com/llvm/llvm-project/pull/163588
>From b75122e8a543031f09d693270af5a6032e69ffd0 Mon Sep 17 00:00:00 2001
From: Rahul Joshi <rjoshi at nvidia.com>
Date: Wed, 15 Oct 2025 09:36:26 -0700
Subject: [PATCH 1/3] [LLVM][CodingStandard] Extend namespace qualifier rule to
variables
Extend CS rule to use namespace qialifiers to define previously
declared functions to variables as well.
---
llvm/docs/CodingStandards.rst | 15 ++++++++++-----
1 file changed, 10 insertions(+), 5 deletions(-)
diff --git a/llvm/docs/CodingStandards.rst b/llvm/docs/CodingStandards.rst
index 65dd794103ac3..128892edf0772 100644
--- a/llvm/docs/CodingStandards.rst
+++ b/llvm/docs/CodingStandards.rst
@@ -860,23 +860,28 @@ your private interface remains private and undisturbed by outsiders.
It's okay to put extra implementation methods in a public class itself. Just
make them private (or protected) and all is well.
-Use Namespace Qualifiers to Implement Previously Declared Functions
-^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+Use Namespace Qualifiers to Define Previously Declared Variables and 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:
+When providing an out-of-line definition of a variable or 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 {
+ extern int FooVal;
int foo(const char *s);
}
// Foo.cpp
#include "Foo.h"
using namespace llvm;
+
+ int llvm::FooVal;
+
int llvm::foo(const char *s) {
// ...
}
>From d8c19ec4760344a356af67ba758178b6773298bc Mon Sep 17 00:00:00 2001
From: Rahul Joshi <rjoshi at nvidia.com>
Date: Wed, 15 Oct 2025 15:30:14 -0700
Subject: [PATCH 2/3] Add example of opaque class
---
llvm/docs/CodingStandards.rst | 22 +++++++++++++++-------
1 file changed, 15 insertions(+), 7 deletions(-)
diff --git a/llvm/docs/CodingStandards.rst b/llvm/docs/CodingStandards.rst
index 128892edf0772..fdb1932e09a48 100644
--- a/llvm/docs/CodingStandards.rst
+++ b/llvm/docs/CodingStandards.rst
@@ -860,13 +860,13 @@ your private interface remains private and undisturbed by outsiders.
It's okay to put extra implementation methods in a public class itself. Just
make them private (or protected) and all is well.
-Use Namespace Qualifiers to Define Previously Declared Variables and Functions
-^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+Use Namespace Qualifiers to Define Previously Declared Entities
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-When providing an out-of-line definition of a variable or 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:
+When providing an out-of-line definition for various entities (variables,
+functions, opaque classes) 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++
@@ -874,7 +874,11 @@ Do this:
namespace llvm {
extern int FooVal;
int foo(const char *s);
- }
+
+ namespace detail {
+ class FooImpl;
+ } // namespace detail
+ } // namespace llvm
// Foo.cpp
#include "Foo.h"
@@ -886,6 +890,10 @@ Do this:
// ...
}
+ class detail::FooImpl {
+ // ...
+ }
+
Doing this helps to avoid bugs where the definition does not match the
declaration from the header. For example, the following C++ code defines a new
overload of ``llvm::foo`` instead of providing a definition for the existing
>From 444896cbe5d8c79a41db81fd52bc51c4e2b8a521 Mon Sep 17 00:00:00 2001
From: Rahul Joshi <rjoshi at nvidia.com>
Date: Mon, 20 Oct 2025 09:26:49 -0700
Subject: [PATCH 3/3] Review feedback
---
llvm/docs/CodingStandards.rst | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/llvm/docs/CodingStandards.rst b/llvm/docs/CodingStandards.rst
index fdb1932e09a48..8677d894a94a6 100644
--- a/llvm/docs/CodingStandards.rst
+++ b/llvm/docs/CodingStandards.rst
@@ -860,10 +860,10 @@ your private interface remains private and undisturbed by outsiders.
It's okay to put extra implementation methods in a public class itself. Just
make them private (or protected) and all is well.
-Use Namespace Qualifiers to Define Previously Declared Entities
-^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+Use Namespace Qualifiers to Define Previously Declared Symbols
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-When providing an out-of-line definition for various entities (variables,
+When providing an out-of-line definition for various symbols (variables,
functions, opaque classes) 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:
More information about the llvm-commits
mailing list