xref: /llvm-project/clang-tools-extra/test/clang-tidy/checkers/llvmlibc/implementation-in-namespace.cpp (revision a074f8869563cb5b296732e0e8af46dcdc286ae5)
189a1d03eSRichard // RUN: %check_clang_tidy %s llvmlibc-implementation-in-namespace %t
289a1d03eSRichard 
389a1d03eSRichard #define MACRO_A "defining macros outside namespace is valid"
489a1d03eSRichard 
589a1d03eSRichard class ClassB;
6*a074f886SPaul Kirth // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: declaration must be enclosed within the 'LIBC_NAMESPACE_DECL' namespace
789a1d03eSRichard struct StructC {};
8*a074f886SPaul Kirth // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: declaration must be enclosed within the 'LIBC_NAMESPACE_DECL' namespace
9*a074f886SPaul Kirth const char *VarD = MACRO_A;
10*a074f886SPaul Kirth // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: declaration must be enclosed within the 'LIBC_NAMESPACE_DECL' namespace
1189a1d03eSRichard typedef int typeE;
12*a074f886SPaul Kirth // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: declaration must be enclosed within the 'LIBC_NAMESPACE_DECL' namespace
1389a1d03eSRichard void funcF() {}
14*a074f886SPaul Kirth // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: declaration must be enclosed within the 'LIBC_NAMESPACE_DECL' namespace
15774116b8SGuillaume Chatelet 
16774116b8SGuillaume Chatelet namespace outer_most {
17*a074f886SPaul Kirth // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: the outermost namespace should be the 'LIBC_NAMESPACE_DECL' macro
18774116b8SGuillaume Chatelet  class A {};
19774116b8SGuillaume Chatelet }
20774116b8SGuillaume Chatelet 
21774116b8SGuillaume Chatelet // Wrapped in anonymous namespace.
22774116b8SGuillaume Chatelet namespace {
23*a074f886SPaul Kirth // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: declaration must be enclosed within the 'LIBC_NAMESPACE_DECL' namespace
24774116b8SGuillaume Chatelet  class A {};
25774116b8SGuillaume Chatelet }
2689a1d03eSRichard 
2789a1d03eSRichard namespace namespaceG {
28*a074f886SPaul Kirth // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: the outermost namespace should be the 'LIBC_NAMESPACE_DECL' macro
2989a1d03eSRichard namespace __llvm_libc {
3089a1d03eSRichard namespace namespaceH {
3189a1d03eSRichard class ClassB;
3289a1d03eSRichard } // namespace namespaceH
3389a1d03eSRichard struct StructC {};
3489a1d03eSRichard } // namespace __llvm_libc
35*a074f886SPaul Kirth const char *VarD = MACRO_A;
3689a1d03eSRichard typedef int typeE;
3789a1d03eSRichard void funcF() {}
3889a1d03eSRichard } // namespace namespaceG
3989a1d03eSRichard 
40774116b8SGuillaume Chatelet // Wrapped in macro namespace but with an incorrect name
41*a074f886SPaul Kirth #define LIBC_NAMESPACE_DECL [[gnu::visibility("hidden")]] custom_namespace
42*a074f886SPaul Kirth namespace LIBC_NAMESPACE_DECL {
43*a074f886SPaul Kirth // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: the 'LIBC_NAMESPACE_DECL' macro expansion should start with '__llvm_libc'
44*a074f886SPaul Kirth 
45774116b8SGuillaume Chatelet namespace namespaceH {
46774116b8SGuillaume Chatelet class ClassB;
47774116b8SGuillaume Chatelet } // namespace namespaceH
48*a074f886SPaul Kirth } // namespace LIBC_NAMESPACE_DECL
49774116b8SGuillaume Chatelet 
50774116b8SGuillaume Chatelet 
51*a074f886SPaul Kirth // Wrapped in macro namespace with a valid name, LIBC_NAMESPACE_DECL starts with '__llvm_libc'
52*a074f886SPaul Kirth #undef LIBC_NAMESPACE_DECL
53*a074f886SPaul Kirth #define LIBC_NAMESPACE_DECL [[gnu::visibility("hidden")]] __llvm_libc_xyz
54*a074f886SPaul Kirth namespace LIBC_NAMESPACE_DECL {
5589a1d03eSRichard namespace namespaceI {
5689a1d03eSRichard class ClassB;
5789a1d03eSRichard } // namespace namespaceI
5889a1d03eSRichard struct StructC {};
59*a074f886SPaul Kirth const char *VarD = MACRO_A;
6089a1d03eSRichard typedef int typeE;
6189a1d03eSRichard void funcF() {}
6289a1d03eSRichard extern "C" void extern_funcJ() {}
63*a074f886SPaul Kirth } // namespace LIBC_NAMESPACE_DECL
64