[PATCH] D52983: [analyzer] Support Reinitializes attribute in MisusedMovedObject check
Gábor Horváth via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Mon Oct 8 06:28:33 PDT 2018
xazax.hun created this revision.
xazax.hun added reviewers: NoQ, szepet, george.karpenkov.
xazax.hun added a project: clang.
Herald added subscribers: Szelethus, mikhail.ramalho, a.sidorin, dkrupp, rnkovacs, baloghadamsoftware, whisperity.
Support the same attribute that the tidy version of this check already supports.
Also do some minor cleanups.
Repository:
rC Clang
https://reviews.llvm.org/D52983
Files:
lib/StaticAnalyzer/Checkers/MisusedMovedObjectChecker.cpp
test/Analysis/MisusedMovedObject.cpp
Index: test/Analysis/MisusedMovedObject.cpp
===================================================================
--- test/Analysis/MisusedMovedObject.cpp
+++ test/Analysis/MisusedMovedObject.cpp
@@ -638,7 +638,10 @@
}
}
-class C : public A {};
+struct C : public A {
+ [[clang::reinitializes]] void reinit();
+};
+
void subRegionMoveTest() {
{
A a;
@@ -672,6 +675,13 @@
C c2 = c; // no-warning
}
+void resetSuperClass2() {
+ C c;
+ C c1 = std::move(c);
+ c.reinit();
+ C c2 = c; // no-warning
+}
+
void reportSuperClass() {
C c;
C c1 = std::move(c); // expected-note {{'c' became 'moved-from' here}}
Index: lib/StaticAnalyzer/Checkers/MisusedMovedObjectChecker.cpp
===================================================================
--- lib/StaticAnalyzer/Checkers/MisusedMovedObjectChecker.cpp
+++ lib/StaticAnalyzer/Checkers/MisusedMovedObjectChecker.cpp
@@ -314,17 +314,18 @@
return true;
}
// Function call `empty` can be skipped.
- if (MethodDec && MethodDec->getDeclName().isIdentifier() &&
+ return (MethodDec && MethodDec->getDeclName().isIdentifier() &&
(MethodDec->getName().lower() == "empty" ||
- MethodDec->getName().lower() == "isempty"))
- return true;
-
- return false;
+ MethodDec->getName().lower() == "isempty"));
}
bool MisusedMovedObjectChecker::isStateResetMethod(
const CXXMethodDecl *MethodDec) const {
- if (MethodDec && MethodDec->getDeclName().isIdentifier()) {
+ if (!MethodDec)
+ return false;
+ if (MethodDec->hasAttr<ReinitializesAttr>())
+ return true;
+ if (MethodDec->getDeclName().isIdentifier()) {
std::string MethodName = MethodDec->getName().lower();
if (MethodName == "reset" || MethodName == "clear" ||
MethodName == "destroy")
@@ -429,8 +430,7 @@
// We want to investigate the whole object, not only sub-object of a parent
// class in which the encountered method defined.
- while (const CXXBaseObjectRegion *BR =
- dyn_cast<CXXBaseObjectRegion>(ThisRegion))
+ while (const auto *BR = dyn_cast<CXXBaseObjectRegion>(ThisRegion))
ThisRegion = BR->getSuperRegion();
if (isMoveSafeMethod(MethodDecl))
@@ -487,13 +487,9 @@
ThisRegion = IC->getCXXThisVal().getAsRegion();
}
- for (ArrayRef<const MemRegion *>::iterator I = ExplicitRegions.begin(),
- E = ExplicitRegions.end();
- I != E; ++I) {
- const auto *Region = *I;
- if (ThisRegion != Region) {
+ for (const auto *Region : ExplicitRegions) {
+ if (ThisRegion != Region)
State = removeFromState(State, Region);
- }
}
return State;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D52983.168652.patch
Type: text/x-patch
Size: 2654 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20181008/ec375f71/attachment.bin>
More information about the cfe-commits
mailing list