File: definitions-in-headers.rst

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,998,520 kB
  • sloc: cpp: 6,951,680; ansic: 1,486,157; asm: 913,598; python: 232,024; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,009; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,167; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (94 lines) | stat: -rw-r--r-- 2,443 bytes parent folder | download | duplicates (16)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
.. title:: clang-tidy - misc-definitions-in-headers

misc-definitions-in-headers
===========================

Finds non-extern non-inline function and variable definitions in header files,
which can lead to potential ODR violations in case these headers are included
from multiple translation units.

.. code-block:: c++

   // Foo.h
   int a = 1; // Warning: variable definition.
   extern int d; // OK: extern variable.

   namespace N {
     int e = 2; // Warning: variable definition.
   }

   // Warning: variable definition.
   const char* str = "foo";

   // OK: internal linkage variable definitions are ignored for now.
   // Although these might also cause ODR violations, we can be less certain and
   // should try to keep the false-positive rate down.
   static int b = 1;
   const int c = 1;
   const char* const str2 = "foo";
   constexpr int k = 1;
   namespace { int x = 1; }

   // Warning: function definition.
   int g() {
     return 1;
   }

   // OK: inline function definition is allowed to be defined multiple times.
   inline int e() {
     return 1;
   }

   class A {
   public:
     int f1() { return 1; } // OK: implicitly inline member function definition is allowed.
     int f2();

     static int d;
   };

   // Warning: not an inline member function definition.
   int A::f2() { return 1; }

   // OK: class static data member declaration is allowed.
   int A::d = 1;

   // OK: function template is allowed.
   template<typename T>
   T f3() {
     T a = 1;
     return a;
   }

   // Warning: full specialization of a function template is not allowed.
   template <>
   int f3() {
     int a = 1;
     return a;
   }

   template <typename T>
   struct B {
     void f1();
   };

   // OK: member function definition of a class template is allowed.
   template <typename T>
   void B<T>::f1() {}

   class CE {
     constexpr static int i = 5; // OK: inline variable definition.
   };

   inline int i = 5; // OK: inline variable definition.

   constexpr int f10() { return 0; } // OK: constexpr function implies inline.

   // OK: C++14 variable templates are inline.
   template <class T>
   constexpr T pi = T(3.1415926L);

When :program:`clang-tidy` is invoked with the `--fix-notes` option, this check
provides fixes that automatically add the ``inline`` keyword to discovered
functions. Please note that the addition of the ``inline`` keyword to variables
is not currently supported by this check.