[cfe-commits] r127881 - in /cfe/trunk: include/clang/Basic/DiagnosticGroups.td include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaDeclCXX.cpp test/SemaCXX/warn-using-namespace-in-header.cpp test/SemaCXX/warn-using-namespace-in-header.h

Douglas Gregor dgregor at apple.com
Fri Mar 18 09:10:52 PDT 2011


Author: dgregor
Date: Fri Mar 18 11:10:52 2011
New Revision: 127881

URL: http://llvm.org/viewvc/llvm-project?rev=127881&view=rev
Log:
Add an opt-in -Wheader-hygiene, which current diagnoses the use of
global using directives in C++ headers, from Elliot Glaysher!

Added:
    cfe/trunk/test/SemaCXX/warn-using-namespace-in-header.cpp   (with props)
    cfe/trunk/test/SemaCXX/warn-using-namespace-in-header.h   (with props)
Modified:
    cfe/trunk/include/clang/Basic/DiagnosticGroups.td
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/SemaDeclCXX.cpp

Modified: cfe/trunk/include/clang/Basic/DiagnosticGroups.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticGroups.td?rev=127881&r1=127880&r2=127881&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticGroups.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticGroups.td Fri Mar 18 11:10:52 2011
@@ -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">;

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=127881&r1=127880&r2=127881&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Fri Mar 18 11:10:52 2011
@@ -2928,6 +2928,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 "

Modified: cfe/trunk/lib/Sema/SemaDeclCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclCXX.cpp?rev=127881&r1=127880&r2=127881&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclCXX.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclCXX.cpp Fri Mar 18 11:10:52 2011
@@ -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();

Added: cfe/trunk/test/SemaCXX/warn-using-namespace-in-header.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-using-namespace-in-header.cpp?rev=127881&view=auto
==============================================================================
--- cfe/trunk/test/SemaCXX/warn-using-namespace-in-header.cpp (added)
+++ cfe/trunk/test/SemaCXX/warn-using-namespace-in-header.cpp Fri Mar 18 11:10:52 2011
@@ -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}}

Propchange: cfe/trunk/test/SemaCXX/warn-using-namespace-in-header.cpp
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cfe/trunk/test/SemaCXX/warn-using-namespace-in-header.cpp
------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: cfe/trunk/test/SemaCXX/warn-using-namespace-in-header.cpp
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: cfe/trunk/test/SemaCXX/warn-using-namespace-in-header.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-using-namespace-in-header.h?rev=127881&view=auto
==============================================================================
--- cfe/trunk/test/SemaCXX/warn-using-namespace-in-header.h (added)
+++ cfe/trunk/test/SemaCXX/warn-using-namespace-in-header.h Fri Mar 18 11:10:52 2011
@@ -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;
+}

Propchange: cfe/trunk/test/SemaCXX/warn-using-namespace-in-header.h
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cfe/trunk/test/SemaCXX/warn-using-namespace-in-header.h
------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: cfe/trunk/test/SemaCXX/warn-using-namespace-in-header.h
------------------------------------------------------------------------------
    svn:mime-type = text/plain





More information about the cfe-commits mailing list