[cfe-dev] Interest in a -Wusing-namespace-in-headers?

Douglas Gregor dgregor at apple.com
Wed Mar 16 18:15:05 PDT 2011


On Mar 16, 2011, at 6:12 PM, Elliot Glaysher (Chromium) wrote:

> Attached is a new version, using Douglas's suggestion for a
> -Wheader-hygiene flag, as I intend to add the anonymous namespace
> warning next.
> 
> On Wed, Mar 16, 2011 at 4:51 PM, Daniel James <dnljms at gmail.com> wrote:
>> But it doesn't really matter where it is or isn't used. What matters
>> is that a properly scoped import is usually safe, and warning about
>> one would be counter productive.
> 
> This version only warns in the global context and not warning when a
> using declarative is done inside a namespace is covered by the tests.

Cool. One other comment:

Index: lib/Sema/SemaDeclCXX.cpp
===================================================================
--- lib/Sema/SemaDeclCXX.cpp	(revision 127569)
+++ lib/Sema/SemaDeclCXX.cpp	(working copy)
@@ -3922,6 +3922,15 @@
     UDir = UsingDirectiveDecl::Create(Context, CurContext, UsingLoc, NamespcLoc,
                                       SS.getWithLocInContext(Context),
                                       IdentLoc, Named, CommonAncestor);
+
+    if (Diags.getDiagnosticLevel(diag::warn_using_directive_in_header,
+                                 IdentLoc) != Diagnostic::Ignored) {
+      if (!SourceMgr.isFromMainFile(IdentLoc) &&
+          CurContext->getDeclKind() == Decl::TranslationUnit) {
+        Diag(IdentLoc, diag::warn_using_directive_in_header);
+      }
+    }
+
     PushUsingDirective(S, UDir);
   } else {
     Diag(IdentLoc, diag::err_expected_namespace_name) << SS.getRange();

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.

	- Doug



More information about the cfe-dev mailing list