xref: /minix3/external/bsd/llvm/dist/clang/test/SemaCXX/warn-using-namespace-in-header.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -Wheader-hygiene -verify %s
2*f4a2713aSLionel Sambuc 
3*f4a2713aSLionel Sambuc #ifdef BE_THE_HEADER
4*f4a2713aSLionel Sambuc namespace warn_in_header_in_global_context {}
5*f4a2713aSLionel Sambuc using namespace warn_in_header_in_global_context; // expected-warning {{using namespace directive in global context in header}}
6*f4a2713aSLionel Sambuc 
7*f4a2713aSLionel Sambuc // While we want to error on the previous using directive, we don't when we are
8*f4a2713aSLionel Sambuc // inside a namespace
9*f4a2713aSLionel Sambuc namespace dont_warn_here {
10*f4a2713aSLionel Sambuc using namespace warn_in_header_in_global_context;
11*f4a2713aSLionel Sambuc }
12*f4a2713aSLionel Sambuc 
13*f4a2713aSLionel Sambuc // We should warn in toplevel extern contexts.
14*f4a2713aSLionel Sambuc namespace warn_inside_linkage {}
15*f4a2713aSLionel Sambuc extern "C++" {
16*f4a2713aSLionel Sambuc using namespace warn_inside_linkage; // expected-warning {{using namespace directive in global context in header}}
17*f4a2713aSLionel Sambuc }
18*f4a2713aSLionel Sambuc 
19*f4a2713aSLionel Sambuc // This is really silly, but we should warn on it:
20*f4a2713aSLionel Sambuc extern "C++" {
21*f4a2713aSLionel Sambuc extern "C" {
22*f4a2713aSLionel Sambuc extern "C++" {
23*f4a2713aSLionel Sambuc using namespace warn_inside_linkage; // expected-warning {{using namespace directive in global context in header}}
24*f4a2713aSLionel Sambuc }
25*f4a2713aSLionel Sambuc }
26*f4a2713aSLionel Sambuc }
27*f4a2713aSLionel Sambuc 
28*f4a2713aSLionel Sambuc // But we shouldn't warn in extern contexts inside namespaces.
29*f4a2713aSLionel Sambuc namespace dont_warn_here {
30*f4a2713aSLionel Sambuc extern "C++" {
31*f4a2713aSLionel Sambuc using namespace warn_in_header_in_global_context;
32*f4a2713aSLionel Sambuc }
33*f4a2713aSLionel Sambuc }
34*f4a2713aSLionel Sambuc 
35*f4a2713aSLionel Sambuc // We also shouldn't warn in case of functions.
foo()36*f4a2713aSLionel Sambuc inline void foo() {
37*f4a2713aSLionel Sambuc   using namespace warn_in_header_in_global_context;
38*f4a2713aSLionel Sambuc }
39*f4a2713aSLionel Sambuc 
40*f4a2713aSLionel Sambuc 
41*f4a2713aSLionel Sambuc namespace macronamespace {}
42*f4a2713aSLionel Sambuc #define USING_MACRO using namespace macronamespace;
43*f4a2713aSLionel Sambuc 
44*f4a2713aSLionel Sambuc // |using namespace| through a macro should warn if the instantiation is in a
45*f4a2713aSLionel Sambuc // header.
46*f4a2713aSLionel Sambuc USING_MACRO // expected-warning {{using namespace directive in global context in header}}
47*f4a2713aSLionel Sambuc 
48*f4a2713aSLionel Sambuc #else
49*f4a2713aSLionel Sambuc 
50*f4a2713aSLionel Sambuc #define BE_THE_HEADER
51*f4a2713aSLionel Sambuc #include __FILE__
52*f4a2713aSLionel Sambuc 
53*f4a2713aSLionel Sambuc namespace dont_warn {}
54*f4a2713aSLionel Sambuc using namespace dont_warn;
55*f4a2713aSLionel Sambuc 
56*f4a2713aSLionel Sambuc // |using namespace| through a macro shouldn't warn if the instantiation is in a
57*f4a2713aSLionel Sambuc // cc file.
58*f4a2713aSLionel Sambuc USING_MACRO
59*f4a2713aSLionel Sambuc 
60*f4a2713aSLionel Sambuc // Check behavior of line markers.
61*f4a2713aSLionel Sambuc namespace warn_header_with_line_marker {}
62*f4a2713aSLionel Sambuc # 1 "XXX.h" 1
63*f4a2713aSLionel Sambuc using namespace warn_header_with_line_marker; // expected-warning {{using namespace directive in global context in header}}
64*f4a2713aSLionel Sambuc # 70 "warn-using-namespace-in-header.cpp" 2
65*f4a2713aSLionel Sambuc 
66*f4a2713aSLionel Sambuc namespace nowarn_after_line_marker {}
67*f4a2713aSLionel Sambuc using namespace nowarn_after_line_marker;
68*f4a2713aSLionel Sambuc 
69*f4a2713aSLionel Sambuc #endif
70