Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions clang/lib/StaticAnalyzer/Checkers/StdVariantChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ bool isStdVariant(const Type *Type) {
static std::optional<ArrayRef<TemplateArgument>>
getTemplateArgsFromVariant(const Type *VariantType) {
const auto *TempSpecType = VariantType->getAs<TemplateSpecializationType>();
while (TempSpecType && TempSpecType->isTypeAlias())
TempSpecType =
TempSpecType->getAliasedType()->getAs<TemplateSpecializationType>();
if (!TempSpecType)
return {};

Expand Down Expand Up @@ -219,10 +222,12 @@ class StdVariantChecker : public Checker<eval::Call, check::RegionChanges> {
bool handleStdGetCall(const CallEvent &Call, CheckerContext &C) const {
ProgramStateRef State = C.getState();

const auto &ArgType = Call.getArgSVal(0)
.getType(C.getASTContext())
->getPointeeType()
.getTypePtr();
SVal ArgSVal = Call.getArgSVal(0);
if (ArgSVal.isUnknown())
return false;

const auto &ArgType =
ArgSVal.getType(C.getASTContext())->getPointeeType().getTypePtr();
// We have to make sure that the argument is an std::variant.
// There is another std::get with std::pair argument
if (!isStdVariant(ArgType))
Expand Down
29 changes: 28 additions & 1 deletion clang/test/Analysis/std-variant-checker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,4 +355,31 @@ void nonInlineFunctionCallPtr() {
char c = std::get<char> (v); // no-warning
(void)a;
(void)c;
}
}

// ----------------------------------------------------------------------------//
// Misc
// ----------------------------------------------------------------------------//

void unknownVal() {
// force the argument to be UnknownVal
(void)std::get<int>(*(std::variant<int, float>*)(int)3.14f); // no crash
}

template <typename T>
using MyVariant = std::variant<int, float>;

void typeAlias() {
MyVariant<bool> v;

(void)std::get<int>(v); // no-warning
}

template <template<typename> typename Container>
using MySpecialVariant = std::variant<int, float>;

void complexTypeAlias() {
MySpecialVariant<std::vector> v;

(void)std::get<int>(v); // no crash
}
Loading