-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[clang-tidy] add abseil-unchecked-statusor-access #171188
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
fmayer
wants to merge
11
commits into
users/fmayer/spr/main.clang-tidy-add-abseil-unchecked-statusor-access
Choose a base branch
from
users/fmayer/spr/clang-tidy-add-abseil-unchecked-statusor-access
base: users/fmayer/spr/main.clang-tidy-add-abseil-unchecked-statusor-access
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
11 commits
Select commit
Hold shift + click to select a range
24cb2c1
[𝘀𝗽𝗿] initial version
fmayer 9959d66
rebase\
fmayer 1ec8e00
fix
fmayer 3a77056
change
fmayer d020c4b
test
fmayer 6580c09
doc
fmayer 77c75b4
release notes
fmayer 6125e65
addressed
fmayer 0c934ba
address comments
fmayer a19badb
doc
fmayer 1f94537
doc
fmayer 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
68 changes: 68 additions & 0 deletions
68
clang-tools-extra/clang-tidy/abseil/UncheckedStatusOrAccessCheck.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,68 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "UncheckedStatusOrAccessCheck.h" | ||
| #include "clang/AST/ASTContext.h" | ||
| #include "clang/ASTMatchers/ASTMatchFinder.h" | ||
| #include "clang/ASTMatchers/ASTMatchers.h" | ||
| #include "clang/Analysis/FlowSensitive/DataflowAnalysis.h" | ||
| #include "clang/Analysis/FlowSensitive/Models/UncheckedStatusOrAccessModel.h" | ||
| #include "clang/Basic/SourceLocation.h" | ||
| #include "llvm/ADT/SmallVector.h" | ||
| #include "llvm/Support/Error.h" | ||
|
|
||
| namespace clang::tidy::abseil { | ||
| using ast_matchers::MatchFinder; | ||
| using dataflow::statusor_model::UncheckedStatusOrAccessDiagnoser; | ||
| using dataflow::statusor_model::UncheckedStatusOrAccessModel; | ||
|
|
||
| static constexpr llvm::StringLiteral FuncID("fun"); | ||
|
|
||
| void UncheckedStatusOrAccessCheck::registerMatchers(MatchFinder *Finder) { | ||
| using namespace ast_matchers; | ||
|
|
||
| auto HasStatusOrCallDescendant = | ||
| hasDescendant(callExpr(callee(cxxMethodDecl(ofClass(hasAnyName( | ||
| "absl::StatusOr", "absl::internal_statusor::OperatorBase")))))); | ||
| Finder->addMatcher(functionDecl(unless(isExpansionInSystemHeader()), | ||
| hasBody(HasStatusOrCallDescendant)) | ||
| .bind(FuncID), | ||
| this); | ||
| Finder->addMatcher( | ||
| cxxConstructorDecl(hasAnyConstructorInitializer( | ||
| withInitializer(HasStatusOrCallDescendant))) | ||
| .bind(FuncID), | ||
| this); | ||
| } | ||
|
|
||
| void UncheckedStatusOrAccessCheck::check( | ||
| const MatchFinder::MatchResult &Result) { | ||
| if (Result.SourceManager->getDiagnostics().hasUncompilableErrorOccurred()) | ||
| return; | ||
vbvictor marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const auto *FuncDecl = Result.Nodes.getNodeAs<FunctionDecl>(FuncID); | ||
| if (FuncDecl->isTemplated()) | ||
| return; | ||
|
|
||
| UncheckedStatusOrAccessDiagnoser Diagnoser; | ||
| if (llvm::Expected<llvm::SmallVector<SourceLocation>> Locs = | ||
| dataflow::diagnoseFunction<UncheckedStatusOrAccessModel, | ||
| SourceLocation>(*FuncDecl, *Result.Context, | ||
| Diagnoser)) | ||
| for (const SourceLocation &Loc : *Locs) | ||
| diag(Loc, "unchecked access to 'absl::StatusOr' value"); | ||
| else | ||
| llvm::consumeError(Locs.takeError()); | ||
| } | ||
|
|
||
| bool UncheckedStatusOrAccessCheck::isLanguageVersionSupported( | ||
| const LangOptions &LangOpts) const { | ||
| return LangOpts.CPlusPlus; | ||
| } | ||
|
|
||
| } // namespace clang::tidy::abseil | ||
24 changes: 24 additions & 0 deletions
24
clang-tools-extra/clang-tidy/abseil/UncheckedStatusOrAccessCheck.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,24 @@ | ||
| #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ABSEIL_UNCHECKEDSTATUSORACCESSCHECK_H | ||
| #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ABSEIL_UNCHECKEDSTATUSORACCESSCHECK_H | ||
|
|
||
| #include "../ClangTidyCheck.h" | ||
| #include "clang/ASTMatchers/ASTMatchFinder.h" | ||
|
|
||
| namespace clang::tidy::abseil { | ||
|
|
||
| // Warns when the code is unwrapping an absl::StatusOr<T> object without | ||
| // assuring that it contains a value. | ||
| // | ||
| // For details on the dataflow analysis implemented in this check see: | ||
| // clang/lib/Analysis/FlowSensitive/Models/UncheckedStatusOrAccessModel.cpp | ||
| class UncheckedStatusOrAccessCheck : public ClangTidyCheck { | ||
| public: | ||
| using ClangTidyCheck::ClangTidyCheck; | ||
| void registerMatchers(ast_matchers::MatchFinder *Finder) override; | ||
| void check(const ast_matchers::MatchFinder::MatchResult &Result) override; | ||
| bool isLanguageVersionSupported(const LangOptions &LangOpts) const override; | ||
| }; | ||
|
|
||
| } // namespace clang::tidy::abseil | ||
|
|
||
| #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_ABSEIL_UNCHECKEDSTATUSORACCESSCHECK_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
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.