1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #ifndef MemMoveAnnotation_h__
6 #define MemMoveAnnotation_h__
8 #include "CustomMatchers.h"
9 #include "CustomTypeAnnotation.h"
12 #include <unordered_set>
14 class MemMoveAnnotation final
: public CustomTypeAnnotation
{
17 : CustomTypeAnnotation(moz_non_memmovable
, "non-memmove()able") {}
19 virtual ~MemMoveAnnotation() {}
22 std::string
getImplicitReason(const TagDecl
*D
,
23 VisitFlags
&ToVisit
) const override
{
24 // Annotate everything in ::std, with a few exceptions; see bug
25 // 1201314 for discussion.
26 if (getDeclarationNamespace(D
) != "std") {
30 StringRef Name
= getNameChecked(D
);
32 // If the type has a trivial move constructor and destructor, it is safe to
33 // memmove, and we don't need to visit any fields.
34 auto RD
= dyn_cast
<CXXRecordDecl
>(D
);
35 if (RD
&& RD
->isCompleteDefinition() &&
36 (RD
->hasTrivialMoveConstructor() ||
37 (!RD
->hasMoveConstructor() && RD
->hasTrivialCopyConstructor())) &&
38 RD
->hasTrivialDestructor()) {
43 // This doesn't check that it's really ::std::pair and not
44 // ::std::something_else::pair, but should be good enough.
45 if (isNameExcepted(Name
.data())) {
46 // If we're an excepted name, stop traversing within the type further,
47 // and only check template arguments for foreign types.
48 ToVisit
= VISIT_TMPL_ARGS
;
51 return "it is an stl-provided type not guaranteed to be memmove-able";
55 bool isNameExcepted(StringRef Name
) const {
56 return Name
== "pair" || Name
== "atomic" || Name
== "tuple";
60 extern MemMoveAnnotation NonMemMovable
;