summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDouglas Gregor <dgregor@apple.com>2011-03-18 16:10:52 +0000
committerDouglas Gregor <dgregor@apple.com>2011-03-18 16:10:52 +0000
commit96a4bddefbfc52d9612cbdaea535e394ca6f765b (patch)
treecd01b0a32ba1da68d919cb0ae337f8862309b5b4
parent6b314b3d9456f5aa71f0a41d07f92f8c368cb182 (diff)
downloadbcm5719-llvm-96a4bddefbfc52d9612cbdaea535e394ca6f765b.tar.gz
bcm5719-llvm-96a4bddefbfc52d9612cbdaea535e394ca6f765b.zip
Add an opt-in -Wheader-hygiene, which current diagnoses the use of
global using directives in C++ headers, from Elliot Glaysher! llvm-svn: 127881
-rw-r--r--clang/include/clang/Basic/DiagnosticGroups.td1
-rw-r--r--clang/include/clang/Basic/DiagnosticSemaKinds.td3
-rw-r--r--clang/lib/Sema/SemaDeclCXX.cpp6
-rw-r--r--clang/test/SemaCXX/warn-using-namespace-in-header.cpp9
-rw-r--r--clang/test/SemaCXX/warn-using-namespace-in-header.h15
5 files changed, 34 insertions, 0 deletions
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td b/clang/include/clang/Basic/DiagnosticGroups.td
index 87c6c2d19e8..d3cc08319ae 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -109,6 +109,7 @@ def : DiagGroup<"stack-protector">;
def : DiagGroup<"switch-default">;
def : DiagGroup<"synth">;
def TautologicalCompare : DiagGroup<"tautological-compare">;
+def HeaderHygiene : DiagGroup<"header-hygiene">;
// Preprocessor warnings.
def : DiagGroup<"builtin-macro-redefined">;
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index c0d0642d635..a722f0514f2 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -2928,6 +2928,9 @@ def warn_overloaded_virtual : Warning<
InGroup<OverloadedVirtual>, DefaultIgnore;
def note_hidden_overloaded_virtual_declared_here : Note<
"hidden overloaded virtual function %q0 declared here">;
+def warn_using_directive_in_header : Warning<
+ "using namespace directive in global context in header">,
+ InGroup<HeaderHygiene>, DefaultIgnore;
def err_conditional_void_nonvoid : Error<
"%select{left|right}1 operand to ? is void, but %select{right|left}1 operand "
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 858a5f9251b..8b7bbb69540 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -3922,6 +3922,12 @@ Decl *Sema::ActOnUsingDirective(Scope *S,
UDir = UsingDirectiveDecl::Create(Context, CurContext, UsingLoc, NamespcLoc,
SS.getWithLocInContext(Context),
IdentLoc, Named, CommonAncestor);
+
+ if (CurContext->getDeclKind() == Decl::TranslationUnit &&
+ !SourceMgr.isFromMainFile(IdentLoc)) {
+ Diag(IdentLoc, diag::warn_using_directive_in_header);
+ }
+
PushUsingDirective(S, UDir);
} else {
Diag(IdentLoc, diag::err_expected_namespace_name) << SS.getRange();
diff --git a/clang/test/SemaCXX/warn-using-namespace-in-header.cpp b/clang/test/SemaCXX/warn-using-namespace-in-header.cpp
new file mode 100644
index 00000000000..393e097df59
--- /dev/null
+++ b/clang/test/SemaCXX/warn-using-namespace-in-header.cpp
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -fsyntax-only -Wheader-hygiene -verify %s
+
+#include "warn-using-namespace-in-header.h"
+
+namespace dont_warn {}
+using namespace dont_warn;
+
+// Warning is actually in the header but only the cpp file gets scanned.
+// expected-warning {{using namespace directive in global context in header}}
diff --git a/clang/test/SemaCXX/warn-using-namespace-in-header.h b/clang/test/SemaCXX/warn-using-namespace-in-header.h
new file mode 100644
index 00000000000..677c4ac51ae
--- /dev/null
+++ b/clang/test/SemaCXX/warn-using-namespace-in-header.h
@@ -0,0 +1,15 @@
+
+
+
+
+
+// Lots of vertical space to make the error line match up with the line of the
+// expected line in the source file.
+namespace warn_in_header_in_global_context {}
+using namespace warn_in_header_in_global_context;
+
+// While we want to error on the previous using directive, we don't when we are
+// inside a namespace
+namespace dont_warn_here {
+using namespace warn_in_header_in_global_context;
+}
OpenPOWER on IntegriCloud