Skip to content

[clang] [Modules] Add warning for including header in module purview #68615

@davidstone

Description

@davidstone

Given the following code

export module a;

#include <new>

When compiling with -std=c++26 -stdlib=libc++ -Weverything

all that we get as output is

In file included from <source>:3:
/opt/compiler-explorer/clang-assertions-trunk-20231009/bin/../include/c++/v1/new:217:66: error: declaration of 'operator new' in module a follows declaration in the global module
  217 | _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz) _THROW_BAD_ALLOC;
      |                                                                  ^
note: previous declaration is here
/opt/compiler-explorer/clang-assertions-trunk-20231009/bin/../include/c++/v1/new:219:36: error: declaration of 'operator delete' in module a follows declaration in the global module
  219 | _LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete(void* __p) _NOEXCEPT;
      |                                    ^
note: previous declaration is here
/opt/compiler-explorer/clang-assertions-trunk-20231009/bin/../include/c++/v1/new:225:66: error: declaration of 'operator new[]' in module a follows declaration in the global module
  225 | _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz) _THROW_BAD_ALLOC;
      |                                                                  ^
note: previous declaration is here
/opt/compiler-explorer/clang-assertions-trunk-20231009/bin/../include/c++/v1/new:227:36: error: declaration of 'operator delete[]' in module a follows declaration in the global module
  227 | _LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete[](void* __p) _NOEXCEPT;
      |                                    ^
note: previous declaration is here
/opt/compiler-explorer/clang-assertions-trunk-20231009/bin/../include/c++/v1/new:234:66: error: declaration of 'operator new' in module a follows declaration in the global module
  234 | _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new(std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC;
      |                                                                  ^
note: previous declaration is here
/opt/compiler-explorer/clang-assertions-trunk-20231009/bin/../include/c++/v1/new:236:36: error: declaration of 'operator delete' in module a follows declaration in the global module
  236 | _LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete(void* __p, std::align_val_t) _NOEXCEPT;
      |                                    ^
note: previous declaration is here
/opt/compiler-explorer/clang-assertions-trunk-20231009/bin/../include/c++/v1/new:242:66: error: declaration of 'operator new[]' in module a follows declaration in the global module
  242 | _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_OVERRIDABLE_FUNC_VIS void* operator new[](std::size_t __sz, std::align_val_t) _THROW_BAD_ALLOC;
      |                                                                  ^
note: previous declaration is here
/opt/compiler-explorer/clang-assertions-trunk-20231009/bin/../include/c++/v1/new:244:36: error: declaration of 'operator delete[]' in module a follows declaration in the global module
  244 | _LIBCPP_OVERRIDABLE_FUNC_VIS void  operator delete[](void* __p, std::align_val_t) _NOEXCEPT;
      |                                    ^
note: previous declaration is here
8 errors generated.
Compiler returned: 1

This is not obvious to users what they did wrong. The issue here is that the #include is in the module purview. It would be nice to have a general warning saying "Don't include there, type module; #include <whatever> instead.

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzerclang:modulesC++20 modules and Clang Header Modules

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions