summaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
authorDuncan P. N. Exon Smith <dexonsmith@apple.com>2014-03-03 16:48:44 +0000
committerDuncan P. N. Exon Smith <dexonsmith@apple.com>2014-03-03 16:48:44 +0000
commit653638bea4c05fa3d68378d38a5feba60096287e (patch)
tree4500ea9dcc272a4bf9bb4f6a77512f8140f8c81d /docs
parentdb3064e438bb44227e8d16c0f82cef74db5d6e69 (diff)
downloadllvm-653638bea4c05fa3d68378d38a5feba60096287e.tar.gz
llvm-653638bea4c05fa3d68378d38a5feba60096287e.tar.bz2
llvm-653638bea4c05fa3d68378d38a5feba60096287e.tar.xz
Clarify struct usage guidelines
The current coding standards restrict the use of struct to PODs, but no one has been following them. This patch updates the standards to clarify when structs are dangerous and describe common practice in LLVM. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@202728 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'docs')
-rw-r--r--docs/CodingStandards.rst36
1 files changed, 31 insertions, 5 deletions
diff --git a/docs/CodingStandards.rst b/docs/CodingStandards.rst
index de2ac3fbe5..edf001aeda 100644
--- a/docs/CodingStandards.rst
+++ b/docs/CodingStandards.rst
@@ -650,12 +650,38 @@ members public by default.
Unfortunately, not all compilers follow the rules and some will generate
different symbols based on whether ``class`` or ``struct`` was used to declare
-the symbol. This can lead to problems at link time.
+the symbol (e.g., MSVC). This can lead to problems at link time.
-So, the rule for LLVM is to always use the ``class`` keyword, unless **all**
-members are public and the type is a C++ `POD
-<http://en.wikipedia.org/wiki/Plain_old_data_structure>`_ type, in which case
-``struct`` is allowed.
+* All declarations and definitions of a given ``class`` or ``struct`` must use
+ the same keyword. For example:
+
+.. code-block:: c++
+
+ class Foo;
+
+ // Breaks mangling in MSVC.
+ struct Foo { int Data; };
+
+* As a rule of thumb, ``struct`` should be kept to structures where *all*
+ members are declared public.
+
+.. code-block:: c++
+
+ // Foo feels like a class... this is strange.
+ struct Foo {
+ private:
+ int Data;
+ public:
+ Foo() : Data(0) { }
+ int getData() const { return Data; }
+ void setData(int D) { Data = D; }
+ };
+
+ // Bar isn't POD, but it does look like a struct.
+ struct Bar {
+ int Data;
+ Foo() : Data(0) { }
+ };
Do not use Braced Initializer Lists to Call a Constructor
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^