Clang error: cannot catch incomplete type A (err_catch_incomplete)

From emmtrix Wiki
Jump to navigation Jump to search
Text error: cannot catch incomplete type A
Type Error
Category Semantic Issue
Internal Id err_catch_incomplete
Internal Message cannot catch incomplete type %0
Regular Expression (?:error|fatal error)\: cannot catch incomplete type (.*?)
First Commit 2009-03-14 5a8987ca5113 Update tablegen diagnostic files to be in sync with the def files.

Description

The error is issued by the Clang compiler when attempting to catch an exception of an incomplete type in a C++ program. In C++, an incomplete type is a type that has been declared but not defined. This means the compiler does not have enough information about the size or layout of the type, which is necessary for catching exceptions since the exception handling mechanism needs to copy or move the exception object into the catch clause.

Catching an exception by an incomplete type is problematic because, without a complete definition of the type, the compiler cannot guarantee that it can properly handle the exception. This includes issues related to object slicing, memory allocation, and invoking the correct destructor for the caught object.

This error typically occurs in situations where a forward declaration of a class or struct is used before its full definition. A common scenario is when trying to catch an exception of a class that is only forward-declared, but not defined within the scope of the catch clause.

To resolve this error, ensure that the type being caught is fully defined before the catch clause. This typically involves including the appropriate header file that contains the definition of the type or moving the definition of the type above the try-catch block in which it is used.

#include "complete_type_header.h" // Include the header file where 'A' is fully defined

void function() {
  try {
    // some code that may throw an exception of type A
  } catch (A& exception) {
    // handler code for exceptions of type A
  }
}

By ensuring that all types caught in exception handlers are complete, the Clang compiler can properly manage exceptions, leading to safer and more predictable exception handling in C++ programs.

 
AI Generated

Example

In the following example, a structure S is forward-declared, indicating that its complete definition is not yet provided to the compiler. This forward declaration is used within a try block attempting to catch an exception of type S. The catch clause, therefore, triggers the error message because S is an incomplete type at this point in the code. The output of the compilation demonstrates the compiler's inability to catch the incomplete type 'S', providing a direct link to the line of the forward declaration as a hint towards resolving the issue. This example illustrates the necessity of having complete type information for exception handling in C++.  
AI Generated


Flags -xc++

[Try out in Compiler Explorer]

Source
struct S; // Incomplete type declaration

void f() {
  try {
    // some code
  } catch (S) { // catching incomplete type
    // handler code
  }
}
Compiler Output
<source>:6:13: error: cannot catch incomplete type 'S'
<source>:1:8: note: forward declaration of 'S'


Clang Internals (17.0.6)

Git Commit Message

Update tablegen diagnostic files to be in sync with the def files.

llvm-svn: 67004

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/SemaDeclCXX.cpp (line 16633)

/// Perform semantic analysis for the variable declaration that
/// occurs within a C++ catch clause, returning the newly-created
/// variable.
VarDecl *Sema::BuildExceptionDeclaration(Scope *S, TypeSourceInfo *TInfo, SourceLocation StartLoc, SourceLocation Loc, IdentifierInfo *Name) {
  // ...
  unsigned DK = diag::err_catch_incomplete;

Triggered in Clang Tests

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

clang/test/SemaCXX/exceptions.cpp

  • clang/test/SemaCXX/exceptions.cpp:15:16: error: cannot catch incomplete type 'void'
  • clang/test/SemaCXX/exceptions.cpp:16:13: error: cannot catch incomplete type 'A'