[PATCH] D153954: [clang][analyzer] Fix empty enum handling in EnumCastOutOfRange checker
This revision was landed with ongoing or failed builds. This revision was automatically updated to reflect the committed changes. Closed by commit rG90c1f51c4b3e: [clang][analyzer] Fix empty enum handling in EnumCastOutOfRange checker (authored by gamesh411). Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D153954/new/ https://reviews.llvm.org/D153954 Files: clang/lib/StaticAnalyzer/Checkers/EnumCastOutOfRangeChecker.cpp clang/test/Analysis/enum-cast-out-of-range.cpp Index: clang/test/Analysis/enum-cast-out-of-range.cpp === --- clang/test/Analysis/enum-cast-out-of-range.cpp +++ clang/test/Analysis/enum-cast-out-of-range.cpp @@ -198,3 +198,20 @@ s.E = static_cast(4); // OK. s.E = static_cast(5); // expected-warning {{The value provided to the cast expression is not in the valid range of values for the enum}} } + + +enum class empty_unspecified {}; + +enum class empty_specified: char {}; + +enum class empty_specified_unsigned: unsigned char {}; + +void ignore_unused(...); + +void empty_enums_init_with_zero_should_not_warn() { + auto eu = static_cast(0); //should always be OK to zero initialize any enum + auto ef = static_cast(0); + auto efu = static_cast(0); + + ignore_unused(eu, ef, efu); +} Index: clang/lib/StaticAnalyzer/Checkers/EnumCastOutOfRangeChecker.cpp === --- clang/lib/StaticAnalyzer/Checkers/EnumCastOutOfRangeChecker.cpp +++ clang/lib/StaticAnalyzer/Checkers/EnumCastOutOfRangeChecker.cpp @@ -129,6 +129,14 @@ const EnumDecl *ED = T->castAs()->getDecl(); EnumValueVector DeclValues = getDeclValuesForEnum(ED); + + // If the declarator list is empty, bail out. + // Every initialization an enum with a fixed underlying type but without any + // enumerators would produce a warning if we were to continue at this point. + // The most notable example is std::byte in the C++17 standard library. + if (DeclValues.size() == 0) +return; + // Check if any of the enum values possibly match. bool PossibleValueMatch = llvm::any_of( DeclValues, ConstraintBasedEQEvaluator(C, *ValueToCast)); Index: clang/test/Analysis/enum-cast-out-of-range.cpp === --- clang/test/Analysis/enum-cast-out-of-range.cpp +++ clang/test/Analysis/enum-cast-out-of-range.cpp @@ -198,3 +198,20 @@ s.E = static_cast(4); // OK. s.E = static_cast(5); // expected-warning {{The value provided to the cast expression is not in the valid range of values for the enum}} } + + +enum class empty_unspecified {}; + +enum class empty_specified: char {}; + +enum class empty_specified_unsigned: unsigned char {}; + +void ignore_unused(...); + +void empty_enums_init_with_zero_should_not_warn() { + auto eu = static_cast(0); //should always be OK to zero initialize any enum + auto ef = static_cast(0); + auto efu = static_cast(0); + + ignore_unused(eu, ef, efu); +} Index: clang/lib/StaticAnalyzer/Checkers/EnumCastOutOfRangeChecker.cpp === --- clang/lib/StaticAnalyzer/Checkers/EnumCastOutOfRangeChecker.cpp +++ clang/lib/StaticAnalyzer/Checkers/EnumCastOutOfRangeChecker.cpp @@ -129,6 +129,14 @@ const EnumDecl *ED = T->castAs()->getDecl(); EnumValueVector DeclValues = getDeclValuesForEnum(ED); + + // If the declarator list is empty, bail out. + // Every initialization an enum with a fixed underlying type but without any + // enumerators would produce a warning if we were to continue at this point. + // The most notable example is std::byte in the C++17 standard library. + if (DeclValues.size() == 0) +return; + // Check if any of the enum values possibly match. bool PossibleValueMatch = llvm::any_of( DeclValues, ConstraintBasedEQEvaluator(C, *ValueToCast)); ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D153954: [clang][analyzer] Fix empty enum handling in EnumCastOutOfRange checker
gamesh411 updated this revision to Diff 548593. gamesh411 added a comment. minor review fixups Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D153954/new/ https://reviews.llvm.org/D153954 Files: clang/lib/StaticAnalyzer/Checkers/EnumCastOutOfRangeChecker.cpp clang/test/Analysis/enum-cast-out-of-range.cpp Index: clang/test/Analysis/enum-cast-out-of-range.cpp === --- clang/test/Analysis/enum-cast-out-of-range.cpp +++ clang/test/Analysis/enum-cast-out-of-range.cpp @@ -198,3 +198,20 @@ s.E = static_cast(4); // OK. s.E = static_cast(5); // expected-warning {{The value provided to the cast expression is not in the valid range of values for the enum}} } + + +enum class empty_unspecified {}; + +enum class empty_specified: char {}; + +enum class empty_specified_unsigned: unsigned char {}; + +void ignore_unused(...); + +void empty_enums_init_with_zero_should_not_warn() { + auto eu = static_cast(0); //should always be OK to zero initialize any enum + auto ef = static_cast(0); + auto efu = static_cast(0); + + ignore_unused(eu, ef, efu); +} Index: clang/lib/StaticAnalyzer/Checkers/EnumCastOutOfRangeChecker.cpp === --- clang/lib/StaticAnalyzer/Checkers/EnumCastOutOfRangeChecker.cpp +++ clang/lib/StaticAnalyzer/Checkers/EnumCastOutOfRangeChecker.cpp @@ -129,6 +129,14 @@ const EnumDecl *ED = T->castAs()->getDecl(); EnumValueVector DeclValues = getDeclValuesForEnum(ED); + + // If the declarator list is empty, bail out. + // Every initialization an enum with a fixed underlying type but without any + // enumerators would produce a warning if we were to continue at this point. + // The most notable example is std::byte in the C++17 standard library. + if (DeclValues.size() == 0) +return; + // Check if any of the enum values possibly match. bool PossibleValueMatch = llvm::any_of( DeclValues, ConstraintBasedEQEvaluator(C, *ValueToCast)); Index: clang/test/Analysis/enum-cast-out-of-range.cpp === --- clang/test/Analysis/enum-cast-out-of-range.cpp +++ clang/test/Analysis/enum-cast-out-of-range.cpp @@ -198,3 +198,20 @@ s.E = static_cast(4); // OK. s.E = static_cast(5); // expected-warning {{The value provided to the cast expression is not in the valid range of values for the enum}} } + + +enum class empty_unspecified {}; + +enum class empty_specified: char {}; + +enum class empty_specified_unsigned: unsigned char {}; + +void ignore_unused(...); + +void empty_enums_init_with_zero_should_not_warn() { + auto eu = static_cast(0); //should always be OK to zero initialize any enum + auto ef = static_cast(0); + auto efu = static_cast(0); + + ignore_unused(eu, ef, efu); +} Index: clang/lib/StaticAnalyzer/Checkers/EnumCastOutOfRangeChecker.cpp === --- clang/lib/StaticAnalyzer/Checkers/EnumCastOutOfRangeChecker.cpp +++ clang/lib/StaticAnalyzer/Checkers/EnumCastOutOfRangeChecker.cpp @@ -129,6 +129,14 @@ const EnumDecl *ED = T->castAs()->getDecl(); EnumValueVector DeclValues = getDeclValuesForEnum(ED); + + // If the declarator list is empty, bail out. + // Every initialization an enum with a fixed underlying type but without any + // enumerators would produce a warning if we were to continue at this point. + // The most notable example is std::byte in the C++17 standard library. + if (DeclValues.size() == 0) +return; + // Check if any of the enum values possibly match. bool PossibleValueMatch = llvm::any_of( DeclValues, ConstraintBasedEQEvaluator(C, *ValueToCast)); ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D153954: [clang][analyzer] Fix empty enum handling in EnumCastOutOfRange checker
donat.nagy added inline comments. Comment at: clang/test/Analysis/enum-cast-out-of-range.cpp:203 + +enum class empty_unfixed {}; + steakhal wrote: > donat.nagy wrote: > > Consider using "specified" and "unspecified" instead of "fixed" and > > "unfixed", because the rest of the test file uses them and in my opinion > > "unfixed" looks strange in this context. (I know that e.g. > > https://en.cppreference.com/w/cpp/language/enum speaks about "fixed" > > underlying context, but it doesn't use "unfixed".) > How about calling it "plain" or "common"? "plain" is also a good alternative for "unfixed" (although it would still be inconsistent with the earlier cases); but "common" is confusing, because in addition to the "plain, regular, normal" meaning it can also mean "mutual; shared by more than one ". Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D153954/new/ https://reviews.llvm.org/D153954 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D153954: [clang][analyzer] Fix empty enum handling in EnumCastOutOfRange checker
steakhal accepted this revision. steakhal added a comment. LGTM Comment at: clang/test/Analysis/enum-cast-out-of-range.cpp:203 + +enum class empty_unfixed {}; + donat.nagy wrote: > Consider using "specified" and "unspecified" instead of "fixed" and > "unfixed", because the rest of the test file uses them and in my opinion > "unfixed" looks strange in this context. (I know that e.g. > https://en.cppreference.com/w/cpp/language/enum speaks about "fixed" > underlying context, but it doesn't use "unfixed".) How about calling it "plain" or "common"? Comment at: clang/test/Analysis/enum-cast-out-of-range.cpp:212-214 + empty_unfixed eu = static_cast(0); //should always be OK to zero initialize any enum + empty_fixed ef = static_cast(0); + empty_fixed_unsigned efu = static_cast(0); Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D153954/new/ https://reviews.llvm.org/D153954 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D153954: [clang][analyzer] Fix empty enum handling in EnumCastOutOfRange checker
donat.nagy accepted this revision. donat.nagy added a comment. This revision is now accepted and ready to land. LGTM, with a little bikeshedding ;-) Comment at: clang/test/Analysis/enum-cast-out-of-range.cpp:203 + +enum class empty_unfixed {}; + Consider using "specified" and "unspecified" instead of "fixed" and "unfixed", because the rest of the test file uses them and in my opinion "unfixed" looks strange in this context. (I know that e.g. https://en.cppreference.com/w/cpp/language/enum speaks about "fixed" underlying context, but it doesn't use "unfixed".) Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D153954/new/ https://reviews.llvm.org/D153954 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits