16e566bc5SRichard.. title:: clang-tidy - misc-definitions-in-headers 26e566bc5SRichard 36e566bc5SRichardmisc-definitions-in-headers 46e566bc5SRichard=========================== 56e566bc5SRichard 66e566bc5SRichardFinds non-extern non-inline function and variable definitions in header files, 76e566bc5SRichardwhich can lead to potential ODR violations in case these headers are included 86e566bc5SRichardfrom multiple translation units. 96e566bc5SRichard 106e566bc5SRichard.. code-block:: c++ 116e566bc5SRichard 126e566bc5SRichard // Foo.h 136e566bc5SRichard int a = 1; // Warning: variable definition. 146e566bc5SRichard extern int d; // OK: extern variable. 156e566bc5SRichard 166e566bc5SRichard namespace N { 176e566bc5SRichard int e = 2; // Warning: variable definition. 186e566bc5SRichard } 196e566bc5SRichard 206e566bc5SRichard // Warning: variable definition. 216e566bc5SRichard const char* str = "foo"; 226e566bc5SRichard 236e566bc5SRichard // OK: internal linkage variable definitions are ignored for now. 246e566bc5SRichard // Although these might also cause ODR violations, we can be less certain and 256e566bc5SRichard // should try to keep the false-positive rate down. 266e566bc5SRichard static int b = 1; 276e566bc5SRichard const int c = 1; 286e566bc5SRichard const char* const str2 = "foo"; 296e566bc5SRichard constexpr int k = 1; 302469bd98SCarlos Galvez namespace { int x = 1; } 316e566bc5SRichard 326e566bc5SRichard // Warning: function definition. 336e566bc5SRichard int g() { 346e566bc5SRichard return 1; 356e566bc5SRichard } 366e566bc5SRichard 376e566bc5SRichard // OK: inline function definition is allowed to be defined multiple times. 386e566bc5SRichard inline int e() { 396e566bc5SRichard return 1; 406e566bc5SRichard } 416e566bc5SRichard 426e566bc5SRichard class A { 436e566bc5SRichard public: 446e566bc5SRichard int f1() { return 1; } // OK: implicitly inline member function definition is allowed. 456e566bc5SRichard int f2(); 466e566bc5SRichard 476e566bc5SRichard static int d; 486e566bc5SRichard }; 496e566bc5SRichard 506e566bc5SRichard // Warning: not an inline member function definition. 516e566bc5SRichard int A::f2() { return 1; } 526e566bc5SRichard 536e566bc5SRichard // OK: class static data member declaration is allowed. 546e566bc5SRichard int A::d = 1; 556e566bc5SRichard 566e566bc5SRichard // OK: function template is allowed. 576e566bc5SRichard template<typename T> 586e566bc5SRichard T f3() { 596e566bc5SRichard T a = 1; 606e566bc5SRichard return a; 616e566bc5SRichard } 626e566bc5SRichard 636e566bc5SRichard // Warning: full specialization of a function template is not allowed. 646e566bc5SRichard template <> 656e566bc5SRichard int f3() { 666e566bc5SRichard int a = 1; 676e566bc5SRichard return a; 686e566bc5SRichard } 696e566bc5SRichard 706e566bc5SRichard template <typename T> 716e566bc5SRichard struct B { 726e566bc5SRichard void f1(); 736e566bc5SRichard }; 746e566bc5SRichard 756e566bc5SRichard // OK: member function definition of a class template is allowed. 766e566bc5SRichard template <typename T> 776e566bc5SRichard void B<T>::f1() {} 786e566bc5SRichard 796e566bc5SRichard class CE { 806e566bc5SRichard constexpr static int i = 5; // OK: inline variable definition. 816e566bc5SRichard }; 826e566bc5SRichard 836e566bc5SRichard inline int i = 5; // OK: inline variable definition. 846e566bc5SRichard 856e566bc5SRichard constexpr int f10() { return 0; } // OK: constexpr function implies inline. 866e566bc5SRichard 876e566bc5SRichard // OK: C++14 variable templates are inline. 886e566bc5SRichard template <class T> 896e566bc5SRichard constexpr T pi = T(3.1415926L); 906e566bc5SRichard 91*c5b617c5SPiotr ZegarWhen :program:`clang-tidy` is invoked with the `--fix-notes` option, this check 92*c5b617c5SPiotr Zegarprovides fixes that automatically add the ``inline`` keyword to discovered 93*c5b617c5SPiotr Zegarfunctions. Please note that the addition of the ``inline`` keyword to variables 94*c5b617c5SPiotr Zegaris not currently supported by this check. 95