brintos

brintos / llvm-project-archived public Read only

0
0
Text · 12.3 KiB · 71dce6e Raw
291 lines · cpp
1// RUN: %check_clang_tidy %s llvm-prefer-static-over-anonymous-namespace %t -- -- -fno-delayed-template-parsing2// RUN: %check_clang_tidy -check-suffixes=,VAR %s llvm-prefer-static-over-anonymous-namespace %t -- \3// RUN:   -config="{CheckOptions: { \4// RUN:     llvm-prefer-static-over-anonymous-namespace.AllowVariableDeclarations: false }, \5// RUN:   }" -- -fno-delayed-template-parsing6// RUN: %check_clang_tidy -check-suffixes=,MEM %s llvm-prefer-static-over-anonymous-namespace %t -- \7// RUN:   -config="{CheckOptions: { \8// RUN:     llvm-prefer-static-over-anonymous-namespace.AllowMemberFunctionsInClass: false }, \9// RUN:   }" -- -fno-delayed-template-parsing10 11namespace {12 13void regularFunction() {14// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: function 'regularFunction' is declared in an anonymous namespace; prefer using 'static' for restricting visibility [llvm-prefer-static-over-anonymous-namespace]15 16  int Variable = 42;17  auto Lambda = []() { return 42; };18  static int StaticVariable = 42;19}20 21void declaredFunction();22 23static void staticFunction() {}24// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: place static function 'staticFunction' outside of an anonymous namespace25 26int globalVariable = 42;27// CHECK-MESSAGES-VAR: :[[@LINE-1]]:5: warning: variable 'globalVariable' is declared in an anonymous namespace;28 29static int staticVariable = 42;30// CHECK-MESSAGES-VAR: :[[@LINE-1]]:12: warning: place static variable 'staticVariable' outside of an anonymous namespace31 32typedef int MyInt;33const MyInt myGlobalVariable = 42;34// CHECK-MESSAGES-VAR: :[[@LINE-1]]:13: warning: variable 'myGlobalVariable' is declared in an anonymous namespace;35 36template<typename T>37constexpr T Pi = T(3.1415926);38// CHECK-MESSAGES-VAR: :[[@LINE-1]]:13: warning: variable 'Pi' is declared in an anonymous namespace;39 40void (*funcPtr)() = nullptr;41// CHECK-MESSAGES-VAR: :[[@LINE-1]]:8: warning: variable 'funcPtr' is declared in an anonymous namespace;42 43auto lambda = []() { return 42; };44// CHECK-MESSAGES-VAR: :[[@LINE-1]]:6: warning: variable 'lambda' is declared in an anonymous namespace;45 46class InstanceClass {47  int member;48};49 50InstanceClass instance;51// CHECK-MESSAGES-VAR: :[[@LINE-1]]:15: warning: variable 'instance' is declared in an anonymous namespace;52 53InstanceClass* instancePtr = nullptr;54// CHECK-MESSAGES-VAR: :[[@LINE-1]]:16: warning: variable 'instancePtr' is declared in an anonymous namespace;55 56InstanceClass& instanceRef = instance;57// CHECK-MESSAGES-VAR: :[[@LINE-1]]:16: warning: variable 'instanceRef' is declared in an anonymous namespace;58 59class MyClass {60public:61  MyClass();62  MyClass(const MyClass&) {}63  // CHECK-MESSAGES-MEM: :[[@LINE-1]]:3: warning: place definition of method 'MyClass' outside of an anonymous namespace64  MyClass(MyClass&&) = default;65  // CHECK-MESSAGES-MEM: :[[@LINE-1]]:3: warning: place definition of method 'MyClass' outside of an anonymous namespace66  MyClass& operator=(const MyClass&);67  MyClass& operator=(MyClass&&);68  bool operator<(const MyClass&) const;69  void memberFunction();70  static void staticMemberFunction();71  void memberDefinedInClass() {}72  // CHECK-MESSAGES-MEM: :[[@LINE-1]]:8: warning: place definition of method 'memberDefinedInClass' outside of an anonymous namespace73  static void staticMemberDefinedInClass() {}74  // CHECK-MESSAGES-MEM: :[[@LINE-1]]:15: warning: place definition of method 'staticMemberDefinedInClass' outside of an anonymous namespace75  template <typename T>76  void templateFunction();77  template <typename T>78  void templateFunctionInClass() {}79  // CHECK-MESSAGES-MEM: :[[@LINE-1]]:8: warning: place definition of method 'templateFunctionInClass' outside of an anonymous namespace80};81 82MyClass::MyClass() {}83// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: place definition of method 'MyClass' outside of an anonymous namespace84 85MyClass& MyClass::operator=(const MyClass&) { return *this; }86// CHECK-MESSAGES: :[[@LINE-1]]:19: warning: place definition of method 'operator=' outside of an anonymous namespace87 88MyClass& MyClass::operator=(MyClass&&) = default;89// CHECK-MESSAGES: :[[@LINE-1]]:19: warning: place definition of method 'operator=' outside of an anonymous namespace90 91bool MyClass::operator<(const MyClass&) const { return true; }92// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: place definition of method 'operator<' outside of an anonymous namespace93 94void MyClass::memberFunction() {}95// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: place definition of method 'memberFunction' outside of an anonymous namespace96 97void MyClass::staticMemberFunction() {}98// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: place definition of method 'staticMemberFunction' outside of an anonymous namespace99 100template <typename T>101void MyClass::templateFunction() {}102// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: place definition of method 'templateFunction' outside of an anonymous namespace103 104template<typename T>105void templateFunction(T Value) {}106// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: function 'templateFunction' is declared in an anonymous namespace; prefer using 'static' for restricting visibility107 108template<>109void templateFunction<int>(int Value) {}110// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: function 'templateFunction<int>' is declared in an anonymous namespace; prefer using 'static' for restricting visibility111 112template<typename T>113class TemplateClass {114public:115  TemplateClass();116  TemplateClass(const TemplateClass&) {}117  // CHECK-MESSAGES-MEM: :[[@LINE-1]]:3: warning: place definition of method 'TemplateClass<T>' outside of an anonymous namespace118  TemplateClass(TemplateClass&&) = default;119  // CHECK-MESSAGES-MEM: :[[@LINE-1]]:3: warning: place definition of method 'TemplateClass<T>' outside of an anonymous namespace120  TemplateClass& operator=(const TemplateClass&);121  TemplateClass& operator=(TemplateClass&&);122  bool operator<(const TemplateClass&) const;123  void memberFunc();124  T getValue() const;125  void memberDefinedInClass() {}126  // CHECK-MESSAGES-MEM: :[[@LINE-1]]:8: warning: place definition of method 'memberDefinedInClass' outside of an anonymous namespace127  static void staticMemberDefinedInClass() {}128  // CHECK-MESSAGES-MEM: :[[@LINE-1]]:15: warning: place definition of method 'staticMemberDefinedInClass' outside of an anonymous namespace129  template <typename U>130  void templateMethodInTemplateClass() {}131  // CHECK-MESSAGES-MEM: :[[@LINE-1]]:8: warning: place definition of method 'templateMethodInTemplateClass' outside of an anonymous namespace132private:133  T Value;134};135 136template<typename T>137TemplateClass<T>::TemplateClass() {}138// CHECK-MESSAGES: :[[@LINE-1]]:19: warning: place definition of method 'TemplateClass<T>' outside of an anonymous namespace139 140template<typename T>141TemplateClass<T>& TemplateClass<T>::operator=(const TemplateClass&) { return *this; }142// CHECK-MESSAGES: :[[@LINE-1]]:37: warning: place definition of method 'operator=' outside of an anonymous namespace143 144template<typename T>145TemplateClass<T>& TemplateClass<T>::operator=(TemplateClass&&) = default;146// CHECK-MESSAGES: :[[@LINE-1]]:37: warning: place definition of method 'operator=' outside of an anonymous namespace147 148template<typename T>149bool TemplateClass<T>::operator<(const TemplateClass&) const { return true; }150// CHECK-MESSAGES: :[[@LINE-1]]:24: warning: place definition of method 'operator<' outside of an anonymous namespace151 152template<typename T>153void TemplateClass<T>::memberFunc() {}154// CHECK-MESSAGES: :[[@LINE-1]]:24: warning: place definition of method 'memberFunc' outside of an anonymous namespace155 156template<typename T>157T TemplateClass<T>::getValue() const { return Value; }158// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: place definition of method 'getValue' outside of an anonymous namespace159 160inline void inlineFunction() {}161// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: function 'inlineFunction' is declared in an anonymous namespace; prefer using 'static' for restricting visibility162 163auto autoReturnFunction() -> int { return 42; }164// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: function 'autoReturnFunction' is declared in an anonymous namespace; prefer using 'static' for restricting visibility165 166class OuterClass {167public:168  class NestedClass {169  public:170    void nestedMemberFunc();171    void nestedMemberDefinedInClass() {}172    // CHECK-MESSAGES-MEM: :[[@LINE-1]]:10: warning: place definition of method 'nestedMemberDefinedInClass' outside of an anonymous namespace173  };174};175 176void OuterClass::NestedClass::nestedMemberFunc() {}177// CHECK-MESSAGES: :[[@LINE-1]]:31: warning: place definition of method 'nestedMemberFunc' outside of an anonymous namespace178 179} // namespace180 181namespace {182 183class MyClassOutOfAnon {184public:185  MyClassOutOfAnon();186  MyClassOutOfAnon(const MyClassOutOfAnon&) {}187  // CHECK-MESSAGES-MEM: :[[@LINE-1]]:3: warning: place definition of method 'MyClassOutOfAnon' outside of an anonymous namespace188  MyClassOutOfAnon(MyClassOutOfAnon&&) = default;189  // CHECK-MESSAGES-MEM: :[[@LINE-1]]:3: warning: place definition of method 'MyClassOutOfAnon' outside of an anonymous namespace190  MyClassOutOfAnon& operator=(const MyClassOutOfAnon&);191  MyClassOutOfAnon& operator=(MyClassOutOfAnon&&);192  bool operator<(const MyClassOutOfAnon&) const;193  void memberFunction();194  static void staticMemberFunction();195  void memberDefinedInClass() {}196  // CHECK-MESSAGES-MEM: :[[@LINE-1]]:8: warning: place definition of method 'memberDefinedInClass' outside of an anonymous namespace197  static void staticMemberDefinedInClass() {}198  // CHECK-MESSAGES-MEM: :[[@LINE-1]]:15: warning: place definition of method 'staticMemberDefinedInClass' outside of an anonymous namespace199  template <typename T>200  void templateFunction();201  template <typename T>202  void templateFunctionInClass() {}203  // CHECK-MESSAGES-MEM: :[[@LINE-1]]:8: warning: place definition of method 'templateFunctionInClass' outside of an anonymous namespace204};205 206} // namespace207 208MyClassOutOfAnon::MyClassOutOfAnon() {}209 210MyClassOutOfAnon& MyClassOutOfAnon::operator=(const MyClassOutOfAnon&) { return *this; }211 212MyClassOutOfAnon& MyClassOutOfAnon::operator=(MyClassOutOfAnon&&) = default;213 214bool MyClassOutOfAnon::operator<(const MyClassOutOfAnon&) const { return true; }215 216void MyClassOutOfAnon::memberFunction() {}217 218void MyClassOutOfAnon::staticMemberFunction() {}219 220template <typename T>221void MyClassOutOfAnon::templateFunction() {}222 223namespace {224 225template<typename T>226class TemplateClassOutOfAnon {227  public:228  TemplateClassOutOfAnon();229  TemplateClassOutOfAnon(const TemplateClassOutOfAnon&) {}230  // CHECK-MESSAGES-MEM: :[[@LINE-1]]:3: warning: place definition of method 'TemplateClassOutOfAnon<T>' outside of an anonymous namespace231  TemplateClassOutOfAnon(TemplateClassOutOfAnon&&) = default;232  // CHECK-MESSAGES-MEM: :[[@LINE-1]]:3: warning: place definition of method 'TemplateClassOutOfAnon<T>' outside of an anonymous namespace233  TemplateClassOutOfAnon& operator=(const TemplateClassOutOfAnon&);234  TemplateClassOutOfAnon& operator=(TemplateClassOutOfAnon&&);235  bool operator<(const TemplateClassOutOfAnon&) const;236  void memberFunc();237  T getValue() const;238  void memberDefinedInClass() {}239  // CHECK-MESSAGES-MEM: :[[@LINE-1]]:8: warning: place definition of method 'memberDefinedInClass' outside of an anonymous namespace240  static void staticMemberDefinedInClass() {}241  // CHECK-MESSAGES-MEM: :[[@LINE-1]]:15: warning: place definition of method 'staticMemberDefinedInClass' outside of an anonymous namespace242  template <typename U>243  void templateMethodInTemplateClass() {}244  // CHECK-MESSAGES-MEM: :[[@LINE-1]]:8: warning: place definition of method 'templateMethodInTemplateClass' outside of an anonymous namespace245  private:246  T Value;247};248 249} // namespace250 251template<typename T>252TemplateClassOutOfAnon<T>::TemplateClassOutOfAnon() {}253 254template<typename T>255TemplateClassOutOfAnon<T>& TemplateClassOutOfAnon<T>::operator=(const TemplateClassOutOfAnon&) { return *this; }256 257template<typename T>258TemplateClassOutOfAnon<T>& TemplateClassOutOfAnon<T>::operator=(TemplateClassOutOfAnon&&) = default;259 260template<typename T>261bool TemplateClassOutOfAnon<T>::operator<(const TemplateClassOutOfAnon&) const { return true; }262 263template<typename T>264void TemplateClassOutOfAnon<T>::memberFunc() {}265 266template<typename T>267T TemplateClassOutOfAnon<T>::getValue() const { return Value; }268 269 270#define DEFINE_FUNCTION(name) \271  namespace { \272    void name() {} \273  }274 275DEFINE_FUNCTION(macroDefinedFunction)276 277#define DECLARE_VAR(type, name, value) \278  namespace { \279    type name = value; \280  }281 282DECLARE_VAR(int, macroVariable, 42)283 284namespace {285 286#define INTERNAL_FUNC void internalMacroFunc() {}287 288INTERNAL_FUNC289 290} // namespace291