Clang warning: A is a reserved name for a module [-Wreserved-module-identifier] (warn_reserved_module_name)

From emmtrix Wiki
Jump to navigation Jump to search
Text error: A is a reserved name for a module
Type Warning
Category Semantic Issue
Internal Id warn_reserved_module_name
Active by Default Yes
Flags -Wno-reserved-identifier (4 elements)
-Wno-reserved-module-identifier (1 element)
Internal Message %0 is a reserved name for a module
Regular Expression (?:warning|error|fatal error)\: (.*?) is a reserved name for a module \[(?:\-Werror,)?\-Wreserved\-module\-identifier[^\]]*\]
First Commit 2021-07-10 95f50964fbf5 Implement P2361 Unevaluated string literals

Description

The warning is issued by the Clang compiler when an attempt is made to define a module with a name that is reserved by the language or the compiler. This situation typically arises when a programmer inadvertently uses a name for a module that is part of the C++ standard library or reserved for compiler-internal use. Module names such as std or any name that begins with std followed exclusively by digits are considered reserved. The rationale behind this restriction is to avoid conflicts and undefined behavior that could arise from name clashes with standard library components or compiler-implemented modules. When this warning is triggered, it suggests that the chosen module name infringes on the reserved namespace and should be renamed to avoid potential issues.  
AI Generated

Example

In the following example, the code attempts to define a module with the name std. As std is a reserved name used by the C++ standard library, the Clang compiler issues a warning. This serves as a clear illustration of the compiler's protective measure to prevent naming conflicts that could arise from using reserved names for modules. The output generated by the compiler reaffirms that std is indeed a reserved name, and using it for a module triggers the -Wreserved-module-identifier warning.  
AI Generated


Flags -xc++ --std=c++20 -fmodules

[Try out in Compiler Explorer]

Source
// Reserved module name
export module std;
Compiler Output
<source>:2:15: warning: 'std' is a reserved name for a module [-Wreserved-module-identifier]


Clang Internals (17.0.6)

Git Commit Message

Implement P2361 Unevaluated string literals

This patch proposes to handle in an uniform fashion
the parsing of strings that are never evaluated,
in asm statement, static assert, attrributes, extern,
etc.

Unevaluated strings are UTF-8 internally and so currently
behave as narrow strings, but these things will diverge with
D93031.

The big question both for this patch and the P2361 paper
is whether we risk breaking code by disallowing
encoding prefixes in this context.
I hope this patch may allow to gather some data on that.

Future work:
Improve the rendering of unicode characters, line break
and so forth in static-assert messages

Reviewed By: aaron.ballman, shafik

Differential Revision: https://reviews.llvm.org/D105759

Used in Clang Sources

This section lists all occurrences of the diagnostic within the Clang's codebase. For each occurrence, an auto-extracted snipped from the source code is listed including key elements like control structures, functions, or classes. It should illustrate the conditions under which the diagnostic is activated.

clang/lib/Sema/SemaModule.cpp (line 166)

/// Tests whether the given identifier is reserved as a module name and
/// diagnoses if it is. Returns true if a diagnostic is emitted and false
/// otherwise.
static bool DiagReservedModuleName(Sema &S, const IdentifierInfo *II, SourceLocation Loc) {
  // ...
  case Reserved:
    S.Diag(Loc, diag::warn_reserved_module_name) << II;

clang/lib/Sema/SemaModule.cpp (line 273)

Sema::DeclGroupPtrTy Sema::ActOnModuleDecl(SourceLocation StartLoc, SourceLocation ModuleLoc, ModuleDeclKind MDK, ModuleIdPath Path, ModuleIdPath Partition, ModuleImportState &ImportState) {
  // ...
  if (!getSourceManager().isInSystemHeader(Path[0].second) && (FirstComponentName == "std" || (FirstComponentName.startswith("std") && llvm::all_of(FirstComponentName.drop_front(3), &llvm::isDigit))))
    Diag(Path[0].second, diag::warn_reserved_module_name) << Path[0].first;