diff --git a/lib/Sema/SemaDeclCXX.cpp b/lib/Sema/SemaDeclCXX.cpp index b193c6021848ff64b98dc118f09cda6c88387379..07d60caf435a4213222fd63a9b97ab0a11b1d9dc 100644 --- a/lib/Sema/SemaDeclCXX.cpp +++ b/lib/Sema/SemaDeclCXX.cpp @@ -3817,6 +3817,19 @@ NamespaceDecl *Sema::getOrCreateStdNamespace() { 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 @@ Decl *Sema::ActOnUsingDirective(Scope *S, 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); } diff --git a/test/SemaCXX/warn-using-namespace-in-header.cpp b/test/SemaCXX/warn-using-namespace-in-header.cpp index 393e097df5975ab9d404c48214427c4a04df962c..e040ca74cbfc224f7996910c78777e6e024ff56e 100644 --- a/test/SemaCXX/warn-using-namespace-in-header.cpp +++ b/test/SemaCXX/warn-using-namespace-in-header.cpp @@ -7,3 +7,21 @@ 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}} + + + + + + + + + +// 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}} diff --git a/test/SemaCXX/warn-using-namespace-in-header.h b/test/SemaCXX/warn-using-namespace-in-header.h index 677c4ac51aefae538bdcf88bcaeea05d69d9caab..57bb5d91be45abac17275f13a44365bbac34237a 100644 --- a/test/SemaCXX/warn-using-namespace-in-header.h +++ b/test/SemaCXX/warn-using-namespace-in-header.h @@ -13,3 +13,30 @@ using namespace warn_in_header_in_global_context; 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; +}