Adds a -Wheader-hygiene warning for warnings that should only trigger
in #included files.
The first -Wheader-hygiene check is to make sure a using directive
isn't placed in the global context in a header.
On Wed, Mar 16, 2011 at 6:15 PM, Douglas Gregor <[email protected]> wrote:
> You don't need to perform the getDiagnosticLevel() check yourself, because
> the diagnostic system will handle warning suppression itself.
>
> However, I do suggest performing the CurContext->getDeclKind() ==
> Decl::TranslationUnit check before the isFromMainFile() check, since the
> former is cheaper.
Done.
-- Elliot Glaysher
Index: test/SemaCXX/warn-using-namespace-in-header.h
===================================================================
--- test/SemaCXX/warn-using-namespace-in-header.h (revision 0)
+++ test/SemaCXX/warn-using-namespace-in-header.h (revision 0)
@@ -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;
+}
Property changes on: test/SemaCXX/warn-using-namespace-in-header.h
___________________________________________________________________
Added: svn:eol-style
+ LF
Index: test/SemaCXX/warn-using-namespace-in-header.cpp
===================================================================
--- test/SemaCXX/warn-using-namespace-in-header.cpp (revision 0)
+++ test/SemaCXX/warn-using-namespace-in-header.cpp (revision 0)
@@ -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}}
Property changes on: test/SemaCXX/warn-using-namespace-in-header.cpp
___________________________________________________________________
Added: svn:eol-style
+ LF
Index: include/clang/Basic/DiagnosticGroups.td
===================================================================
--- include/clang/Basic/DiagnosticGroups.td (revision 127569)
+++ include/clang/Basic/DiagnosticGroups.td (working copy)
@@ -109,6 +109,7 @@
def : DiagGroup<"switch-default">;
def : DiagGroup<"synth">;
def TautologicalCompare : DiagGroup<"tautological-compare">;
+def HeaderHygiene : DiagGroup<"header-hygiene">;
// Preprocessor warnings.
def : DiagGroup<"builtin-macro-redefined">;
Index: include/clang/Basic/DiagnosticSemaKinds.td
===================================================================
--- include/clang/Basic/DiagnosticSemaKinds.td (revision 127569)
+++ include/clang/Basic/DiagnosticSemaKinds.td (working copy)
@@ -2920,6 +2920,9 @@
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 "
Index: lib/Sema/SemaDeclCXX.cpp
===================================================================
--- lib/Sema/SemaDeclCXX.cpp (revision 127569)
+++ lib/Sema/SemaDeclCXX.cpp (working copy)
@@ -3922,6 +3922,12 @@
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();
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits