2016-12-18 00:35:53 +03:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef MemMoveAnnotation_h__
|
|
|
|
#define MemMoveAnnotation_h__
|
|
|
|
|
|
|
|
#include "CustomMatchers.h"
|
2017-10-20 20:11:50 +03:00
|
|
|
#include "CustomTypeAnnotation.h"
|
2016-12-18 00:35:53 +03:00
|
|
|
#include "Utils.h"
|
|
|
|
|
2019-09-09 16:30:16 +03:00
|
|
|
#include <unordered_set>
|
|
|
|
|
2016-12-18 00:35:53 +03:00
|
|
|
class MemMoveAnnotation final : public CustomTypeAnnotation {
|
|
|
|
public:
|
|
|
|
MemMoveAnnotation()
|
2018-09-18 16:03:33 +03:00
|
|
|
: CustomTypeAnnotation(moz_non_memmovable, "non-memmove()able") {}
|
2016-12-18 00:35:53 +03:00
|
|
|
|
|
|
|
virtual ~MemMoveAnnotation() {}
|
|
|
|
|
|
|
|
protected:
|
2021-12-14 19:49:17 +03:00
|
|
|
std::string getImplicitReason(const TagDecl *D,
|
|
|
|
VisitFlags &ToVisit) const override {
|
2016-12-18 00:35:53 +03:00
|
|
|
// Annotate everything in ::std, with a few exceptions; see bug
|
|
|
|
// 1201314 for discussion.
|
2021-12-14 19:49:17 +03:00
|
|
|
if (getDeclarationNamespace(D) != "std") {
|
|
|
|
return "";
|
2016-12-18 00:35:53 +03:00
|
|
|
}
|
2021-12-14 19:49:17 +03:00
|
|
|
|
|
|
|
StringRef Name = getNameChecked(D);
|
|
|
|
|
|
|
|
// If the type has a trivial move constructor and destructor, it is safe to
|
|
|
|
// memmove, and we don't need to visit any fields.
|
|
|
|
auto RD = dyn_cast<CXXRecordDecl>(D);
|
|
|
|
if (RD && RD->isCompleteDefinition() &&
|
|
|
|
(RD->hasTrivialMoveConstructor() ||
|
|
|
|
(!RD->hasMoveConstructor() && RD->hasTrivialCopyConstructor())) &&
|
|
|
|
RD->hasTrivialDestructor()) {
|
|
|
|
ToVisit = VISIT_NONE;
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
// This doesn't check that it's really ::std::pair and not
|
|
|
|
// ::std::something_else::pair, but should be good enough.
|
|
|
|
if (isNameExcepted(Name.data())) {
|
|
|
|
// If we're an excepted name, stop traversing within the type further,
|
|
|
|
// and only check template arguments for foreign types.
|
|
|
|
ToVisit = VISIT_TMPL_ARGS;
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
return "it is an stl-provided type not guaranteed to be memmove-able";
|
2016-12-18 00:35:53 +03:00
|
|
|
}
|
2019-09-09 16:30:16 +03:00
|
|
|
|
|
|
|
private:
|
2021-12-14 19:49:17 +03:00
|
|
|
bool isNameExcepted(StringRef Name) const {
|
2022-02-02 02:12:30 +03:00
|
|
|
return Name == "pair" || Name == "atomic" || Name == "tuple";
|
2019-09-09 16:30:16 +03:00
|
|
|
}
|
2016-12-18 00:35:53 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
extern MemMoveAnnotation NonMemMovable;
|
|
|
|
|
|
|
|
#endif
|