[cfe-commits] r128352 - in /cfe/trunk: 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
Sat Mar 26 15:25:30 PDT 2011


Author: dgregor
Date: Sat Mar 26 17:25:30 2011
New Revision: 128352

URL: http://llvm.org/viewvc/llvm-project?rev=128352&view=rev
Log:
Improve -Wheader-hygiene to warn about using directives inside linkage
specifications within the global scope, from Elliot Glaysher.

Modified:
    cfe/trunk/lib/Sema/SemaDeclCXX.cpp
    cfe/trunk/test/SemaCXX/warn-using-namespace-in-header.cpp
    cfe/trunk/test/SemaCXX/warn-using-namespace-in-header.h

Modified: cfe/trunk/lib/Sema/SemaDeclCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclCXX.cpp?rev=128352&r1=128351&r2=128352&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclCXX.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclCXX.cpp Sat Mar 26 17:25:30 2011
@@ -3817,6 +3817,19 @@
   return getStdNamespace();
 }
 
+/// \brief Determine whether a using statement is in a context where it will be
+/// apply in all contexts.
+static bool IsUsingDirectiveInToplevelContext(DeclContext *CurContext) {
+  switch (CurContext->getDeclKind()) {
+    case Decl::TranslationUnit:
+      return true;
+    case Decl::LinkageSpec:
+      return IsUsingDirectiveInToplevelContext(CurContext->getParent());
+    default:
+      return false;
+  }
+}
+
 Decl *Sema::ActOnUsingDirective(Scope *S,
                                           SourceLocation UsingLoc,
                                           SourceLocation NamespcLoc,
@@ -3902,7 +3915,7 @@
                                       SS.getWithLocInContext(Context),
                                       IdentLoc, Named, CommonAncestor);
 
-    if (CurContext->getDeclKind() == Decl::TranslationUnit &&
+    if (IsUsingDirectiveInToplevelContext(CurContext) &&
         !SourceMgr.isFromMainFile(IdentLoc)) {
       Diag(IdentLoc, diag::warn_using_directive_in_header);
     }

Modified: 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=128352&r1=128351&r2=128352&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/warn-using-namespace-in-header.cpp (original)
+++ cfe/trunk/test/SemaCXX/warn-using-namespace-in-header.cpp Sat Mar 26 17:25:30 2011
@@ -7,3 +7,21 @@
 
 // Warning is actually in the header but only the cpp file gets scanned.
 // expected-warning {{using namespace directive in global context in header}}
+
+
+
+
+
+
+
+
+
+// Warn inside linkage specs too.
+// expected-warning {{using namespace directive in global context in header}}
+
+
+
+
+
+
+// expected-warning {{using namespace directive in global context in header}}

Modified: 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=128352&r1=128351&r2=128352&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/warn-using-namespace-in-header.h (original)
+++ cfe/trunk/test/SemaCXX/warn-using-namespace-in-header.h Sat Mar 26 17:25:30 2011
@@ -13,3 +13,30 @@
 namespace dont_warn_here {
 using namespace warn_in_header_in_global_context;
 }
+
+// We should warn in toplevel extern contexts.
+namespace warn_inside_linkage {}
+extern "C++" {
+using namespace warn_inside_linkage;
+}
+
+// This is really silly, but we should warn on it:
+extern "C++" {
+extern "C" {
+extern "C++" {
+using namespace warn_inside_linkage;
+}
+}
+}
+
+// But we shouldn't warn in extern contexts inside namespaces.
+namespace dont_warn_here {
+extern "C++" {
+using namespace warn_in_header_in_global_context;
+}
+}
+
+// We also shouldn't warn in case of functions.
+inline void foo() {
+  using namespace warn_in_header_in_global_context;
+}





More information about the cfe-commits mailing list