Clang error: cannot find protocol declaration for A (err_undeclared_protocol)

From emmtrix Wiki
Jump to navigation Jump to search
Text error: cannot find protocol declaration for A
Type Error
Category Semantic Issue
Internal Id err_undeclared_protocol
Internal Message cannot find protocol declaration for %0
Regular Expression (?:error|fatal error)\: cannot find protocol declaration for (.*?)
First Commit 2009-03-04 b1c4d5507fad The basic representation of diagnostics information in tablegen format, plus (uncommented and incomp...

Description

The error is issued by the Clang compiler when it cannot locate a protocol declaration referenced in the source code. Protocols define a blueprint of methods, properties, and other requirements that cater to a particular task or piece of functionality. They are a cornerstone of Objective-C programming, allowing for a form of interface definition. This error typically occurs in situations where a protocol is referenced before it has been declared or if it has not been defined at all within the scope of the project or its included headers. The absence of a protocol declaration prompts the compiler to flag an error, as it is unable to enforce the protocol's requirements or verify its existence. This is crucial for Objective-C's runtime type checks and compile-time verification processes. The issue can manifest in various contexts, including class, category, and protocol definitions that adopt or extend the missing protocol. Identifying and resolving missing protocol declarations are essential for the successful compilation and intended runtime behavior of Objective-C programs.  
AI Generated

Example

In the following example, an Objective-C category is being defined for the class C that is supposed to conform to the protocol P. However, the error messages indicate that both the protocol P and the interface declaration for class C cannot be found. The absence of a declaration for P triggers the specific error message discussed in this article, confirming that the compiler cannot recognize P as a known protocol due to its undeclared state in the scope of the compilation process. Additionally, the error message concerning the interface declaration for C suggests that the class C itself is also undeclared or not visible within the scope of the source file, highlighting the importance of ensuring that all referenced interfaces and protocols are properly declared and imported as necessary. This scenario effectively demonstrates the consequences of referencing undeclared protocols and interfaces in Objective-C code.  
AI Generated


Flags -xobjective-c

[Try out in Compiler Explorer]

Source
@interface C (Category) <P> @end
Compiler Output
<source>:1:26: error: cannot find protocol declaration for 'P'
<source>:1:12: error: cannot find interface declaration for 'C'


Clang Internals (17.0.6)

Git Commit Message

The basic representation of diagnostics information in tablegen format, plus (uncommented and incomplete) test conversions of the existing def files to this format.

llvm-svn: 66064

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/SemaDeclObjC.cpp (line 1330)

/// FindProtocolDeclaration - This routine looks up protocols and
/// issues an error if they are not declared. It returns list of
/// protocol declarations in its 'Protocols' argument.
void Sema::FindProtocolDeclaration(bool WarnOnDeclarations, bool ForObjCContainer, ArrayRef<IdentifierLocPair> ProtocolId, SmallVectorImpl<Decl *> &Protocols) {
  for (const IdentifierLocPair &Pair : ProtocolId) {
    // ...
    if (!PDecl) {
      Diag(Pair.second, diag::err_undeclared_protocol) << Pair.first;

clang/lib/Sema/SemaDeclObjC.cpp (line 1744)

void Sema::actOnObjCTypeArgsOrProtocolQualifiers(Scope *S, ParsedType baseType, SourceLocation lAngleLoc, ArrayRef<IdentifierInfo *> identifiers, ArrayRef<SourceLocation> identifierLocs, SourceLocation rAngleLoc, SourceLocation &typeArgsLAngleLoc, SmallVectorImpl<ParsedType> &typeArgs, SourceLocation &typeArgsRAngleLoc, SourceLocation &protocolLAngleLoc, SmallVectorImpl<Decl *> &protocols, SourceLocation &protocolRAngleLoc, bool warnOnIncompleteProtocols) {
  // ...
  for (unsigned i = 0, n = identifiers.size(); i != n; ++i) {
    // ...
    Diag(identifierLocs[i], (lookupKind == LookupAnyName ? diag::err_objc_type_arg_missing : lookupKind == LookupObjCProtocolName ? diag::err_undeclared_protocol : diag::err_unknown_typename)) << identifiers[i];

clang/lib/Sema/SemaExprObjC.cpp (line 1392)

ExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId, SourceLocation AtLoc, SourceLocation ProtoLoc, SourceLocation LParenLoc, SourceLocation ProtoIdLoc, SourceLocation RParenLoc) {
  // ...
  if (!PDecl) {
    Diag(ProtoLoc, diag::err_undeclared_protocol) << ProtocolId;

Triggered in Clang Tests

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

clang/test/SemaObjC/undefined-protocol-type-1.m

  • clang/test/SemaObjC/undefined-protocol-type-1.m:7:10: error: cannot find protocol declaration for 'p3'
  • clang/test/SemaObjC/undefined-protocol-type-1.m:8:10: error: cannot find protocol declaration for 'p3'