brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.8 KiB · 79179ce Raw
74 lines · plain
1.. title:: clang-tidy - llvm-prefer-static-over-anonymous-namespace2 3llvm-prefer-static-over-anonymous-namespace4===========================================5 6Finds function and variable declarations inside anonymous namespace and7suggests replacing them with ``static`` declarations.8 9The `LLVM Coding Standards <https://llvm.org/docs/CodingStandards.html#restrict-visibility>`_10recommend keeping anonymous namespaces as small as possible and only use them11for class declarations. For functions and variables the ``static`` specifier12should be preferred for restricting visibility.13 14For example non-compliant code:15 16.. code-block:: c++17 18  namespace {19 20  class StringSort {21  public:22    StringSort(...)23    bool operator<(const char *RHS) const;24  };25 26  // warning: place method definition outside of an anonymous namespace27  bool StringSort::operator<(const char *RHS) const {}28 29  // warning: prefer using 'static' for restricting visibility30  void runHelper() {}31 32  // warning: prefer using 'static' for restricting visibility33  int myVariable = 42;34 35  }36 37Should become:38 39.. code-block:: c++40 41  // Small anonymous namespace for class declaration42  namespace {43 44  class StringSort {45  public:46    StringSort(...)47    bool operator<(const char *RHS) const;48  };49 50  }51 52  // placed method definition outside of the anonymous namespace53  bool StringSort::operator<(const char *RHS) const {}54 55  // used 'static' instead of an anonymous namespace56  static void runHelper() {}57 58  // used 'static' instead of an anonymous namespace59  static int myVariable = 42;60 61 62Options63-------64 65.. option:: AllowVariableDeclarations66 67  When `true`, allow variable declarations to be in anonymous namespace.68  Default value is `true`.69 70.. option:: AllowMemberFunctionsInClass71 72  When `true`, only methods defined in anonymous namespace outside of the73  corresponding class will be warned. Default value is `true`.74