1 //===-- main.cpp ------------------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include <cstdarg>
10 #include <cstdlib>
11 #include "ns.h"
12 
13 namespace {
14     typedef unsigned int my_uint_t;
15     int i; // Find the line number for anonymous namespace variable i.
16 
17     int myanonfunc (int a)
18     {
19         return a + a;
20     }
21 
22     int
23     variadic_sum (int arg_count...)
24     {
25         int sum = 0;
26         std::va_list args;
27         va_start(args, arg_count);
28 
29         for (int i = 0; i < arg_count; i++)
30             sum += va_arg(args, int);
31 
32         va_end(args);
33         return sum;
34     }
35 }
36 
37 namespace A {
38     typedef unsigned int uint_t;
39     namespace B {
40         typedef unsigned int uint_t;
41         int j; // Find the line number for named namespace variable j.
42         int myfunc (int a);
43         int myfunc2(int a)
44         {
45              return a + 2;
46         }
47         float myfunc (float f)
48         {
49             return f - 2.0;
50         }
51     }
52 }
53 
54 namespace Y
55 {
56     typedef unsigned int uint_t;
57     using A::B::j;
58     int foo;
59 }
60 
61 using A::B::j;          // using declaration
62 
63 namespace Foo = A::B;   // namespace alias
64 
65 using Foo::myfunc;      // using declaration
66 
67 using namespace Foo;    // using directive
68 
69 namespace A {
70     namespace B {
71         using namespace Y;
72         int k;
73     }
74 }
75 
76 namespace ns1 {
77     int value = 100;
78 }
79 
80 namespace ns2 {
81     int value = 200;
82 }
83 
84 void test_namespace_scopes() {
85     do {
86         using namespace ns1;
87         printf("ns1::value = %d\n", value); // Evaluate ns1::value
88     } while(0);
89 
90     do {
91         using namespace ns2;
92         printf("ns2::value = %d\n", value); // Evaluate ns2::value
93     } while(0);
94 }
95 
96 int Foo::myfunc(int a)
97 {
98     test_namespace_scopes();
99 
100     ::my_uint_t anon_uint = 0;
101     A::uint_t a_uint = 1;
102     B::uint_t b_uint = 2;
103     Y::uint_t y_uint = 3;
104     i = 3;
105     j = 4;
106     printf("::i=%d\n", ::i);
107     printf("A::B::j=%d\n", A::B::j);
108     printf("variadic_sum=%d\n", variadic_sum(3, 1, 2, 3));
109     myanonfunc(3);
110     return myfunc2(3) + j + i + a + 2 + anon_uint + a_uint + b_uint + y_uint; // Set break point at this line.
111 }
112 
113 int
114 main (int argc, char const *argv[])
115 {
116     test_lookup_at_global_scope();
117     test_lookup_at_file_scope();
118     A::test_lookup_at_ns_scope();
119     A::B::test_lookup_at_nested_ns_scope();
120     A::B::test_lookup_at_nested_ns_scope_after_using();
121     test_lookup_before_using_directive();
122     test_lookup_after_using_directive();
123     return Foo::myfunc(12);
124 }
125