1 // RUN: %check_clang_tidy %s llvmlibc-callee-namespace %t
2 
3 namespace __llvm_libc {
4 namespace nested {
nested_func()5 void nested_func() {}
6 } // namespace nested
libc_api_func()7 void libc_api_func() {}
8 } // namespace __llvm_libc
9 
10 // Emulate a function from the public headers like string.h
libc_api_func()11 void libc_api_func() {}
12 
13 namespace __llvm_libc {
Test()14 void Test() {
15   // Allow calls with the fully qualified name.
16   __llvm_libc::libc_api_func();
17   __llvm_libc::nested::nested_func();
18   void (*qualifiedPtr)(void) = __llvm_libc::libc_api_func;
19   qualifiedPtr();
20 
21   // Should not trigger on compiler provided function calls.
22   (void)__builtin_abs(-1);
23 
24   // Bare calls are allowed as long as they resolve to the correct namespace.
25   libc_api_func();
26   nested::nested_func();
27   void (*barePtr)(void) = __llvm_libc::libc_api_func;
28   barePtr();
29 
30   // Disallow calling into global namespace for implemented entrypoints.
31   ::libc_api_func();
32   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: 'libc_api_func' must resolve to a function declared within the '__llvm_libc' namespace
33   // CHECK-MESSAGES: :11:6: note: resolves to this declaration
34 
35   // Disallow indirect references to functions in global namespace.
36   void (*badPtr)(void) = ::libc_api_func;
37   badPtr();
38   // CHECK-MESSAGES: :[[@LINE-2]]:26: warning: 'libc_api_func' must resolve to a function declared within the '__llvm_libc' namespace
39   // CHECK-MESSAGES: :11:6: note: resolves to this declaration
40 }
41 
42 } // namespace __llvm_libc
43