diff options
author | Balazs Benics <benicsbalazs@gmail.com> | 2025-05-25 11:59:50 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-05-25 11:59:50 +0200 |
commit | 9a440f84773c56d3803f330774acb2b4f471d5b4 (patch) | |
tree | 8e6a642f2116c3f064b3da1b14aee6469c0ab8ab /clang/lib/StaticAnalyzer/Checkers | |
parent | bc0c4db5d95f229b420f3df0c01d0e41cff157c5 (diff) | |
download | llvm-9a440f84773c56d3803f330774acb2b4f471d5b4.zip llvm-9a440f84773c56d3803f330774acb2b4f471d5b4.tar.gz llvm-9a440f84773c56d3803f330774acb2b4f471d5b4.tar.bz2 |
[analyzer] Ignore [[clang::flag_enum]] enums in the EnumCastOutOfRange checker (#141232)
Resolves
https://github.com/llvm/llvm-project/issues/76208#issuecomment-2830854351
Quoting the docs of `[[clang::flag_enum]]`:
https://clang.llvm.org/docs/AttributeReference.html#flag-enum
> This attribute can be added to an enumerator to signal to the compiler that it
> is intended to be used as a flag type. This will cause the compiler to assume
> that the range of the type includes all of the values that you can get by
> manipulating bits of the enumerator when issuing warnings.
Ideally, we should still check the upper bounds but for simplicity let's not bother for now.
Diffstat (limited to 'clang/lib/StaticAnalyzer/Checkers')
-rw-r--r-- | clang/lib/StaticAnalyzer/Checkers/EnumCastOutOfRangeChecker.cpp | 5 |
1 files changed, 5 insertions, 0 deletions
diff --git a/clang/lib/StaticAnalyzer/Checkers/EnumCastOutOfRangeChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/EnumCastOutOfRangeChecker.cpp index 0fa2042..355e82e 100644 --- a/clang/lib/StaticAnalyzer/Checkers/EnumCastOutOfRangeChecker.cpp +++ b/clang/lib/StaticAnalyzer/Checkers/EnumCastOutOfRangeChecker.cpp @@ -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" @@ -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. |