- Notifications
You must be signed in to change notification settings - Fork 15.3k
[clang-tidy] Filter out googletest TUs in bugprone-unchecked-optional-access #115051
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
Open
jvoung wants to merge 8 commits into llvm:main Choose a base branch from jvoung:unchecked_optional_skip_gtests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits Select commit Hold shift + click to select a range
8d83ec2 [clang-tidy] Filter out googletest TUs in bugprone-unchecked-optional…
jvoung 3427fa7 Add one more missing macro
jvoung 2611af1 Simplify the mock a bit more
jvoung 2e96cc1 Merge branch 'main' into unchecked_optional_skip_gtests
jvoung ee10510 Make an option, default off
jvoung d8b311d fix clang-format
jvoung 9942eee Merge branch 'main' into unchecked_optional_skip_gtests
jvoung ed8e56a Address review comments
jvoung File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions 32 ...lang-tidy/checkers/bugprone/Inputs/unchecked-optional-access/catch2/catch_test_macros.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| #ifndef LLVM_CLANG_TOOLS_EXTRA_TEST_CLANG_TIDY_CHECKERS_INPUTS_CATCH2_CATCH_TEST_MACROS_HPP_ | ||
| #define LLVM_CLANG_TOOLS_EXTRA_TEST_CLANG_TIDY_CHECKERS_INPUTS_CATCH2_CATCH_TEST_MACROS_HPP_ | ||
| | ||
| // Mock version of catch2 test framework macros. | ||
| | ||
| // The macros are normally much more complex. For | ||
| // - REQUIRE and REQUIRE_FALSE: define them to go through some wrapper | ||
| // (that is not at all what catch2 actually does) | ||
| // - TEST_CASE and METHOD_AS_TEST_CASE: define a function | ||
| void Wrapper(bool cond) { | ||
| if (!cond) | ||
| __builtin_trap(); | ||
| } | ||
| | ||
| #define CONCAT_HELP(X,Y) X##Y // helper macro | ||
| #define CONCAT(X,Y) CONCAT_HELP(X,Y) | ||
| | ||
| // Catch2 can be configured to have a prefix "CATCH" for macro names, or not, | ||
| // so we model this. | ||
| #ifdef CATCH_CONFIG_PREFIX_ALL | ||
| #define CATCH_REQUIRE( ... ) Wrapper(static_cast<bool>(__VA_ARGS__)) | ||
| #define CATCH_REQUIRE_FALSE( ... ) Wrapper(static_cast<bool>(__VA_ARGS__)) | ||
| #define CATCH_TEST_CASE( ... ) void CONCAT(TEST, __LINE__)() | ||
| #define CATCH_METHOD_AS_TEST_CASE( ... ) void CONCAT(TEST, __LINE__)() | ||
| #else | ||
| #define REQUIRE( ... ) Wrapper(static_cast<bool>(__VA_ARGS__)) | ||
| #define REQUIRE_FALSE( ... ) Wrapper(static_cast<bool>(__VA_ARGS__)) | ||
| #define TEST_CASE( ... ) void CONCAT(TEST, __LINE__)() | ||
| #define METHOD_AS_TEST_CASE( ... ) void CONCAT(TEST, __LINE__)() | ||
| #endif | ||
| | ||
| #endif // LLVM_CLANG_TOOLS_EXTRA_TEST_CLANG_TIDY_CHECKERS_INPUTS_CATCH2_CATCH_TEST_MACROS_HPP_ |
33 changes: 33 additions & 0 deletions 33 ...ls-extra/test/clang-tidy/checkers/bugprone/Inputs/unchecked-optional-access/gtest/gtest.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| #ifndef LLVM_CLANG_TOOLS_EXTRA_TEST_CLANG_TIDY_CHECKERS_INPUTS_GTEST_GTEST_H_ | ||
| #define LLVM_CLANG_TOOLS_EXTRA_TEST_CLANG_TIDY_CHECKERS_INPUTS_GTEST_GTEST_H_ | ||
| | ||
| // Mock version of googletest macros. | ||
| | ||
| // Normally this declares a class, but it isn't relevant for testing. | ||
| #define GTEST_TEST(test_suite_name, test_name) | ||
| | ||
| // Normally googletest creates a class wrapping the condition, which the | ||
| // dataflow analysis framework has difficulty "seeing through" | ||
| // (needs some inter-procedural analysis, or special-case handling). | ||
| // Here is a simplified version. | ||
| class BoolWrapper { | ||
| public: | ||
| template <typename T> | ||
| BoolWrapper(T &&val) : success_(static_cast<bool>(val)) {} | ||
| operator bool() const { return success_; } | ||
| bool success_; | ||
| }; | ||
| | ||
| #define ASSERT_TRUE(condition) \ | ||
| if (BoolWrapper(condition)) \ | ||
| ; \ | ||
| else \ | ||
| return; | ||
| | ||
| #define ASSERT_FALSE(condition) \ | ||
| if (BoolWrapper(!condition)) \ | ||
| ; \ | ||
| else \ | ||
| return; | ||
| | ||
| #endif // LLVM_CLANG_TOOLS_EXTRA_TEST_CLANG_TIDY_CHECKERS_INPUTS_GTEST_GTEST_H_ |
85 changes: 85 additions & 0 deletions 85 ...ls-extra/test/clang-tidy/checkers/bugprone/unchecked-optional-access-ignore-catchtest.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| // RUN: %check_clang_tidy -check-suffix=IGNORE-TESTS %s \ | ||
| // RUN: bugprone-unchecked-optional-access %t -- \ | ||
| // RUN: -config="{CheckOptions: \ | ||
| // RUN: {bugprone-unchecked-optional-access.IgnoreTestTUs: true}}" \ | ||
| // RUN: -- -I %S/Inputs/unchecked-optional-access | ||
| | ||
| // RUN: %check_clang_tidy -check-suffix=IGNORE-TESTS-CATCH-PREFIX %s \ | ||
| // RUN: --extra-arg=-DCATCH_CONFIG_PREFIX_ALL \ | ||
| // RUN: bugprone-unchecked-optional-access %t -- \ | ||
| // RUN: -config="{CheckOptions: \ | ||
| // RUN: {bugprone-unchecked-optional-access.IgnoreTestTUs: true}}" \ | ||
| // RUN: -- -I %S/Inputs/unchecked-optional-access | ||
| | ||
| // RUN: %check_clang_tidy -check-suffix=DEFAULT %s \ | ||
| // RUN: bugprone-unchecked-optional-access %t -- \ | ||
| // RUN: -- -I %S/Inputs/unchecked-optional-access | ||
| | ||
| // RUN: %check_clang_tidy -check-suffix=DEFAULT-CATCH-PREFIX %s \ | ||
| // RUN: --extra-arg=-DCATCH_CONFIG_PREFIX_ALL \ | ||
| // RUN: bugprone-unchecked-optional-access %t -- \ | ||
| // RUN: -- -I %S/Inputs/unchecked-optional-access | ||
| | ||
| #include "absl/types/optional.h" | ||
| #include "catch2/catch_test_macros.hpp" | ||
| | ||
| // When IgnoreTestTUs is set, all of the warnings in a test TU are suppressed, | ||
| // even the "Unguarded access" and "Guarded incorrectly" cases. | ||
| | ||
| // CHECK-MESSAGES-IGNORE-TESTS: 2 warnings generated | ||
| // CHECK-MESSAGES-IGNORE-TESTS-CATCH-PREFIX: 1 warning generated | ||
| // CHECK-MESSAGES-DEFAULT: 6 warnings generated | ||
| // CHECK-MESSAGES-DEFAULT-CATCH-PREFIX: 3 warnings generated | ||
| | ||
| absl::optional<int> FunctionUnderTest(); | ||
| | ||
| #ifndef CATCH_CONFIG_PREFIX_ALL | ||
| | ||
| TEST_CASE("FN -- Unguarded access", "[optional]") { | ||
| auto opt = FunctionUnderTest(); | ||
| opt.value(); | ||
| // CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:3: warning: unchecked access to optional value | ||
| } | ||
| | ||
| TEST_CASE("FN -- Guarded incorrectly assert false has_value()", "[optional]") { | ||
| auto opt = FunctionUnderTest(); | ||
| REQUIRE_FALSE(!opt.has_value()); | ||
| opt.value(); | ||
| // CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:3: warning: unchecked access to optional value | ||
| } | ||
| | ||
| TEST_CASE("FP -- Guarded assert true has_value()", "[optional]") { | ||
| auto opt = FunctionUnderTest(); | ||
| REQUIRE(opt.has_value()); | ||
| *opt; | ||
| // CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:4: warning: unchecked access to optional value | ||
| } | ||
| | ||
| TEST_CASE("FP -- Guarded assert false not operator bool()", "[optional]") { | ||
| auto opt = FunctionUnderTest(); | ||
| REQUIRE_FALSE(!opt); | ||
| opt.value(); | ||
| // CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:3: warning: unchecked access to optional value | ||
| } | ||
| | ||
| #else | ||
| | ||
| CATCH_TEST_CASE("FN -- Unguarded access", "[optional]") { | ||
| auto opt = FunctionUnderTest(); | ||
| opt.value(); | ||
| // CHECK-MESSAGES-DEFAULT-CATCH-PREFIX: :[[@LINE-1]]:3: warning: unchecked access to optional value | ||
| } | ||
| | ||
| CATCH_TEST_CASE("FP -- Guarded assert true has_value()", "[optional]") { | ||
| auto opt = FunctionUnderTest(); | ||
| CATCH_REQUIRE(opt.has_value()); | ||
| *opt; | ||
| // CHECK-MESSAGES-DEFAULT-CATCH-PREFIX: :[[@LINE-1]]:4: warning: unchecked access to optional value | ||
| } | ||
| | ||
| #endif | ||
| | ||
| // CHECK-MESSAGES-IGNORE-TESTS: Suppressed 2 warnings | ||
| // CHECK-MESSAGES-IGNORE-TESTS-CATCH-PREFIX: Suppressed 1 warning | ||
| // CHECK-MESSAGES-DEFAULT: Suppressed 2 warnings | ||
| // CHECK-MESSAGES-DEFAULT-CATCH-PREFIX: Suppressed 1 warning |
48 changes: 48 additions & 0 deletions 48 ...s-extra/test/clang-tidy/checkers/bugprone/unchecked-optional-access-ignore-googletest.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| // RUN: %check_clang_tidy -check-suffix=IGNORE-TESTS %s \ | ||
| // RUN: bugprone-unchecked-optional-access %t -- \ | ||
| // RUN: -config="{CheckOptions: \ | ||
| // RUN: {bugprone-unchecked-optional-access.IgnoreTestTUs: true}}" \ | ||
| // RUN: -- -I %S/Inputs/unchecked-optional-access | ||
| | ||
| // RUN: %check_clang_tidy -check-suffix=DEFAULT %s \ | ||
| // RUN: bugprone-unchecked-optional-access %t -- \ | ||
| // RUN: -- -I %S/Inputs/unchecked-optional-access | ||
| | ||
| #include "absl/types/optional.h" | ||
| #include "gtest/gtest.h" | ||
| | ||
| // When IgnoreTestTUs is set, all of the warnings in a test TU are suppressed, | ||
| // even the `unchecked_value_access` and `assert_true_incorrect` cases. | ||
| | ||
| // CHECK-MESSAGES-IGNORE-TESTS: 2 warnings generated | ||
| // CHECK-MESSAGES-DEFAULT: 6 warnings generated | ||
| | ||
| // False negative from suppressing in test TU. | ||
| void unchecked_value_access(const absl::optional<int> opt) { | ||
| opt.value(); | ||
| // CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:3: warning: unchecked access to optional value | ||
| } | ||
| | ||
| // False negative from suppressing in test TU. | ||
| void assert_true_incorrect(const absl::optional<int> opt) { | ||
| ASSERT_TRUE(!opt.has_value()); | ||
| opt.value(); | ||
| // CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:3: warning: unchecked access to optional value | ||
| } | ||
| | ||
| // False positive, unless we suppress in test TU. | ||
| void assert_true_check_has_value(const absl::optional<int> opt) { | ||
| ASSERT_TRUE(opt.has_value()); | ||
| *opt; | ||
| // CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:4: warning: unchecked access to optional value | ||
| } | ||
| | ||
| // False positive, unless we suppress (one of many other ways to check) | ||
| void assert_true_check_operator_bool_not_false(const absl::optional<int> opt) { | ||
| ASSERT_FALSE(!opt); | ||
| opt.value(); | ||
| // CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:3: warning: unchecked access to optional value | ||
| } | ||
| | ||
| // CHECK-MESSAGES-IGNORE-TESTS: Suppressed 2 warnings | ||
| // CHECK-MESSAGES-DEFAULT: Suppressed 2 warnings |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.