Edit

Share via


Warning C28213

The _Use_decl_annotations_ annotation must be used to reference, without modification, a prior declaration.

Remarks

_Use_decl_annotations_ tells the compiler to use the annotations from an earlier declaration of the function. If it can't find an earlier declaration, or if the current declaration makes changes to the annotations, it emits this warning. _Use_decl_annotations_ also lets you remove all other annotations from the definition, and uses the declaration annotations for analysis of the function.

This diagnostic is frequently a side effect of refactoring or fixing other warnings by adjusting the annotations on a function. To fix the issue, use the same annotations at the other locations. To determine the correct set of annotations, look at the behavior in the function definition. In most cases, this behavior is intentional and the annotations to the function should reflect it. For more information on SAL annotations, see Using SAL Annotations to reduce code defects.

It's important for the annotations to match between the declarations and the definition of a function. When the analysis tools analyze the call site of the function, they use the declaration annotations. If the declaration and definition don't match, the static analysis tools may produce incorrect results. When you fix this warning, it's common for your changes to have cascading effects as the tool reanalyzes the source with updated information.

If this diagnostic occurs because the analyzer couldn't find a previous declaration in the translation unit, the most likely cause is a missing #include directive. To resolve this issue when you intentionally don't include the header file, verify that the annotations in the declaration and definition match, and remove the _Use_decl_annotations_ annotation. Be careful when you don't include a header file, as the two sets of annotations may get out of sync in the future.

Code analysis name: BAD_USEHEADER

Examples

The following code generates C28160. The buffer parameter annotation doesn't match between the two files.

From example.h:

void addNullTerminate(_Out_writes_(n) char* buffer, int n);

From example.cpp:

_Use_decl_annotations_
void addNullTerminate(_Out_writes_z_(n) char* buffer, int n)
{
    buffer[n] = '\0';
}

Examine the function definition to determine what the correct annotations should be. In this case, _Out_writes_z_(n) appears to be correct, so we move that annotation to the function declaration in the header file. This change resolves the issue because the annotations in the declaration and definition now match.

From example.h:

void addNullTerminate(_Out_writes_z_(n) char* buffer, int n);

Now we can remove the buffer annotation on the definition to simplify future maintenance (although this step is optional).

From example.cpp:

_Use_decl_annotations_
void addNullTerminate(char* buffer, int n)
{
    buffer[n] = '\0';
}

In real world code, it's not usually as clear which annotation is correct. For more information and guidance, see using SAL Annotations to reduce code defects.

See also

Rule sets for C++ code
Using SAL Annotations to reduce code defects
C28252
C28253
C28301