Clang error: exception specifications are not allowed beyond a single level of indirection (err_distant_exception_spec)

From emmtrix Wiki
Jump to navigation Jump to search
Text error: exception specifications are not allowed beyond a single level of indirection
Type Error
Category Semantic Issue
Internal Id err_distant_exception_spec
Internal Message exception specifications are not allowed beyond a single level of indirection
Regular Expression (?:error|fatal error)\: exception specifications are not allowed beyond a single level of indirection
First Commit 2009-05-29 10b9de45c376 Disallow exception specifications on multi-level indirections.

Description

The error is issued by the Clang compiler when an attempt is made to define or declare a function pointer or member function with an exception specification at more than one level of indirection. In C++, exception specifications are part of the function type and indicate which exceptions a function may throw. However, the language standard restricts the use of exception specifications to just a single level of indirection. This means that while a function pointer itself can have an exception specification, a pointer to a pointer to a function (or any higher-level indirection) with an exception specification is not permitted. Similarly, this restriction applies to other compounded types such as references to pointers to functions, arrays of pointers to functions, and member pointers to functions within classes if these are specified with exception specifications beyond the allowed single level of indirection.  
AI Generated

Example

In the following example, a function f is declared, which takes as its parameter a pointer to a pointer to a function. This function, pointed to by the double pointer, is intended to have an exception specification, indicating it may throw an exception of type int. However, due to C++ language restrictions, exception specifications cannot be applied beyond a single level of indirection. Thus, the attempt to specify an exception for a function through more than one level of pointers leads to the Clang compiler issuing an error message, highlighting that the code contravenes the C++ standard's rules regarding exception specification depth.  
AI Generated


Flags -xc++ -std=c++11

[Try out in Compiler Explorer]

Source
// Exception specification with two indirection levels
void f(void (**p)(void) throw(int));
Compiler Output
<source>:2:16: error: exception specifications are not allowed beyond a single level of indirection


Clang Internals (17.0.6)

Git Commit Message

Disallow exception specifications on multi-level indirections.

llvm-svn: 72571

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/SemaType.cpp (line 3076)

/// Build a member pointer type \c T Class::*.
///
/// \param T the type to which the member pointer refers.
/// \param Class the class type into which the member pointer points.
/// \param Loc the location where this type begins
/// \param Entity the name of the entity that will have this member pointer type
///
/// \returns a member pointer type, if successful, or a NULL type if there was
/// an error.
QualType Sema::BuildMemberPointerType(QualType T, QualType Class, SourceLocation Loc, DeclarationName Entity) {
  // Verify that we're not building a pointer to pointer to function with
  // exception specification.
  if (CheckDistantExceptionSpec(T)) {
    Diag(Loc, diag::err_distant_exception_spec);

clang/lib/Sema/SemaType.cpp (line 5053)

static TypeSourceInfo *GetFullTypeForDeclarator(TypeProcessingState &state, QualType declSpecType, TypeSourceInfo *TInfo) {
  // ...
  for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
    // ...
    case DeclaratorChunk::Pointer:
      // Verify that we're not building a pointer to pointer to function with
      // exception specification.
      if (LangOpts.CPlusPlus && S.CheckDistantExceptionSpec(T)) {
        S.Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);

clang/lib/Sema/SemaType.cpp (line 5089)

static TypeSourceInfo *GetFullTypeForDeclarator(TypeProcessingState &state, QualType declSpecType, TypeSourceInfo *TInfo) {
  // ...
  for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
    // ...
    case DeclaratorChunk::Reference: {
      // Verify that we're not building a reference to pointer to function with
      // exception specification.
      if (LangOpts.CPlusPlus && S.CheckDistantExceptionSpec(T)) {
        S.Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);

clang/lib/Sema/SemaType.cpp (line 5103)

static TypeSourceInfo *GetFullTypeForDeclarator(TypeProcessingState &state, QualType declSpecType, TypeSourceInfo *TInfo) {
  // ...
  for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) {
    // ...
    case DeclaratorChunk::Array: {
      // Verify that we're not building an array of pointers to function with
      // exception specification.
      if (LangOpts.CPlusPlus && S.CheckDistantExceptionSpec(T)) {
        S.Diag(D.getIdentifierLoc(), diag::err_distant_exception_spec);

Triggered in Clang Tests

This section lists all internal Clang test cases that trigger the diagnostic.

clang/test/CXX/except/except.spec/p2-places.cpp

  • clang/test/CXX/except/except.spec/p2-places.cpp:35:11: error: exception specifications are not allowed beyond a single level of indirection
  • clang/test/CXX/except/except.spec/p2-places.cpp:38:13: error: exception specifications are not allowed beyond a single level of indirection
  • clang/test/CXX/except/except.spec/p2-places.cpp:62:11: error: exception specifications are not allowed beyond a single level of indirection
  • clang/test/CXX/except/except.spec/p2-places.cpp:64:13: error: exception specifications are not allowed beyond a single level of indirection