[PATCH] D126972: [clang][dataflow] Modify optional-checker to handle type aliases.
Yitzhak Mandelbaum via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Fri Jun 3 09:23:46 PDT 2022
ymandel created this revision.
ymandel added reviewers: xazax.hun, sgatev.
Herald added subscribers: tschuett, steakhal, jeroen.dobbelaere, rnkovacs.
Herald added a project: All.
ymandel requested review of this revision.
Herald added a project: clang.
Previously, type aliases were not handled (and resulted in an assertion
firing). This patch generalizes the code to consider aliases everywhere (a
previous patch already considered aliases for optional-returning functions).
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D126972
Files:
clang/lib/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.cpp
clang/unittests/Analysis/FlowSensitive/UncheckedOptionalAccessModelTest.cpp
Index: clang/unittests/Analysis/FlowSensitive/UncheckedOptionalAccessModelTest.cpp
===================================================================
--- clang/unittests/Analysis/FlowSensitive/UncheckedOptionalAccessModelTest.cpp
+++ clang/unittests/Analysis/FlowSensitive/UncheckedOptionalAccessModelTest.cpp
@@ -2214,6 +2214,23 @@
UnorderedElementsAre(Pair("check-4", "unsafe: input.cc:10:7")));
}
+// Verifies that the check sees through aliases.
+TEST_P(UncheckedOptionalAccessTest, WithAlias) {
+ ExpectLatticeChecksFor(
+ R"(
+ #include "unchecked_optional_access_test.h"
+
+ template <typename T>
+ using MyOptional = $ns::$optional<T>;
+
+ void target(MyOptional<int> opt) {
+ opt.value();
+ /*[[check]]*/
+ }
+ )",
+ UnorderedElementsAre(Pair("check", "unsafe: input.cc:8:7")));
+}
+
// FIXME: Add support for:
// - constructors (copy, move)
// - assignment operators (default, copy, move)
Index: clang/lib/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.cpp
===================================================================
--- clang/lib/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.cpp
+++ clang/lib/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.cpp
@@ -43,13 +43,14 @@
hasTemplateArgument(0, refersToType(type().bind("T"))));
}
-auto hasOptionalType() { return hasType(optionalClass()); }
-
-auto hasOptionalOrAliasType() {
+auto optionalOrAliasType() {
return hasUnqualifiedDesugaredType(
recordType(hasDeclaration(optionalClass())));
}
+/// Matches any of the spellings of the optional types and sugar, aliases, etc.
+auto hasOptionalType() { return hasType(optionalOrAliasType()); }
+
auto isOptionalMemberCallWithName(
llvm::StringRef MemberName,
llvm::Optional<StatementMatcher> Ignorable = llvm::None) {
@@ -164,9 +165,8 @@
}
auto isCallReturningOptional() {
- return callExpr(callee(functionDecl(
- returns(anyOf(hasOptionalOrAliasType(),
- referenceType(pointee(hasOptionalOrAliasType())))))));
+ return callExpr(callee(functionDecl(returns(anyOf(
+ optionalOrAliasType(), referenceType(pointee(optionalOrAliasType())))))));
}
/// Creates a symbolic value for an `optional` value using `HasValueVal` as the
@@ -485,8 +485,9 @@
return MatchSwitchBuilder<LatticeTransferState>()
// Attach a symbolic "has_value" state to optional values that we see for
// the first time.
- .CaseOf<Expr>(expr(anyOf(declRefExpr(), memberExpr()), hasOptionalType()),
- initializeOptionalReference)
+ .CaseOf<Expr>(
+ expr(anyOf(declRefExpr(), memberExpr()), hasOptionalType()),
+ initializeOptionalReference)
// make_optional
.CaseOf<CallExpr>(isMakeOptionalCall(), transferMakeOptionalCall)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D126972.434047.patch
Type: text/x-patch
Size: 2836 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220603/190912b2/attachment-0001.bin>
More information about the cfe-commits
mailing list