[PATCH] D156063: [Clang] Reject programs declaring namespace std to be inline

Mital Ashok via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Jul 24 01:50:12 PDT 2023


MitalAshok requested changes to this revision.
MitalAshok added a comment.
This revision now requires changes to proceed.

This does currently break `namespace foo { inline namespace std {} }`, `namespace foo::inline std {}`, etc.



================
Comment at: clang/lib/Sema/SemaDeclCXX.cpp:11369
+    // This has to be diagnosed before entering DiagnoseNamespaceInlineMismatch.
+    if (IsInline && II->isStr("std"))
+      Diag(InlineLoc, diag::err_inline_namespace_std)
----------------
You need to check if this is a std namespace declaration at file scope, `namespace foo::inline std {}` in a namespace scope should be fine.

The check for this is a few lines below: `CurContext->getRedeclContext()->isTranslationUnit()`.


================
Comment at: clang/lib/Sema/SemaDeclCXX.cpp:11393-11394
       if (IsInline != PrevNS->isInline())
         DiagnoseNamespaceInlineMismatch(*this, NamespaceLoc, Loc, II,
                                         &IsInline, PrevNS);
     } else if (PrevDecl) {
----------------
This ends up giving two errors on the same line if this wasn't the first declaration (`error: namespace 'std' cannot be declared inline` followed by `error: non-inline namespace cannot be reopened as inline; note: previous definition is here`). The wording for the second error is also a little confusing (it cannot be opened at all as inline, let alone reopened), so consider refactoring so that both diagnostics can't be issued at once


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D156063/new/

https://reviews.llvm.org/D156063



More information about the cfe-commits mailing list