| 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/. */ |
| 4 |
|
| 5 |
#ifndef MemMoveAnnotation_h__ |
| 6 |
#define MemMoveAnnotation_h__ |
| 7 |
|
| 8 |
#include "CustomMatchers.h" |
| 9 |
#include "CustomTypeAnnotation.h" |
| 10 |
#include "Utils.h" |
| 11 |
|
| 12 |
#include <unordered_set> |
| 13 |
|
| 14 |
class MemMoveAnnotation final : public CustomTypeAnnotation { |
| 15 |
public: |
| 16 |
MemMoveAnnotation() |
| 17 |
: CustomTypeAnnotation(moz_non_memmovable, "non-memmove()able") {} |
| 18 |
|
| 19 |
virtual ~MemMoveAnnotation() {} |
| 20 |
|
| 21 |
protected: |
| 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") { |
| 27 |
return ""; |
| 28 |
} |
| 29 |
|
| 30 |
StringRef Name = getNameChecked(D); |
| 31 |
|
| 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()) { |
| 39 |
ToVisit = VISIT_NONE; |
| 40 |
return ""; |
| 41 |
} |
| 42 |
|
| 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; |
| 49 |
return ""; |
| 50 |
} |
| 51 |
return "it is an stl-provided type not guaranteed to be memmove-able"; |
| 52 |
} |
| 53 |
|
| 54 |
private: |
| 55 |
bool isNameExcepted(StringRef Name) const { |
| 56 |
return Name == "pair" || Name == "atomic" || Name == "tuple"; |
| 57 |
} |
| 58 |
}; |
| 59 |
|
| 60 |
extern MemMoveAnnotation NonMemMovable; |
| 61 |
|
| 62 |
#endif |