Skip to content

[analyzer] Ignore [[clang::flag_enum]] enums in the EnumCastOutOfRange checker #141232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion clang/docs/analyzer/checkers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,11 @@ enumerators at all.
**Limitations**

This checker does not accept the coding pattern where an enum type is used to
store combinations of flag values:
store combinations of flag values.
Such enums should be annotated with the `__attribute__((flag_enum))` or by the
`[[clang::flag_enum]]` attribute to signal this intent. Refer to the
`documentation <https://clang.llvm.org/docs/AttributeReference.html#flag-enum>`_
of this Clang attribute.

.. code-block:: cpp

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
// enumeration value
//===----------------------------------------------------------------------===//

#include "clang/AST/Attr.h"
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Expand Down Expand Up @@ -149,6 +150,10 @@ void EnumCastOutOfRangeChecker::checkPreStmt(const CastExpr *CE,
// function to handle this.
const EnumDecl *ED = T->castAs<EnumType>()->getDecl();

// [[clang::flag_enum]] annotated enums are by definition should be ignored.
if (ED->hasAttr<FlagEnumAttr>())
return;

EnumValueVector DeclValues = getDeclValuesForEnum(ED);

// If the declarator list is empty, bail out.
Expand Down
11 changes: 11 additions & 0 deletions clang/test/Analysis/enum-cast-out-of-range.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,14 @@ void testTrackExpression(int i) {
(void)(enum En_t)(i); // expected-warning {{not in the valid range of values for 'En_t'}}
// expected-note@-1 {{not in the valid range of values for 'En_t'}}
}

enum __attribute__((flag_enum)) FlagEnum {
FE_BIT_1 = 1 << 0,
FE_BIT_2 = 1 << 1,
FE_BIT_3 = 1 << 2,
};

void testFlagEnum_gh_76208(void) {
enum FlagEnum First2BitsSet = (enum FlagEnum)(FE_BIT_1 | FE_BIT_2); // no-warning: Enums with the attribute 'flag_enum' are not checked
(void)First2BitsSet;
}
11 changes: 11 additions & 0 deletions clang/test/Analysis/enum-cast-out-of-range.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,14 @@ void foo() {

ignore_unused(c, x, d);
}

enum [[clang::flag_enum]] FlagEnum {
FE_BIT_1 = 1 << 0,
FE_BIT_2 = 1 << 1,
FE_BIT_3 = 1 << 2,
};

void testFlagEnum_gh_76208(void) {
FlagEnum First2BitsSet = (FlagEnum)(FE_BIT_1 | FE_BIT_2); // no-warning: Enums with the attribute 'flag_enum' are not checked
(void)First2BitsSet;
}