Bug 1814798 - pt 1. Add bool to enable/disable PHC at runtime r=glandium
[gecko.git] / build / clang-plugin / MemMoveAnnotation.h
blob0a1a96e8ff9285c9fbdb5f5e67493d5c564fa8d5
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"
10 #include "Utils.h"
12 #include <unordered_set>
14 class MemMoveAnnotation final : public CustomTypeAnnotation {
15 public:
16 MemMoveAnnotation()
17 : CustomTypeAnnotation(moz_non_memmovable, "non-memmove()able") {}
19 virtual ~MemMoveAnnotation() {}
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 "";
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()) {
39 ToVisit = VISIT_NONE;
40 return "";
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 "";
51 return "it is an stl-provided type not guaranteed to be memmove-able";
54 private:
55 bool isNameExcepted(StringRef Name) const {
56 return Name == "pair" || Name == "atomic" || Name == "tuple";
60 extern MemMoveAnnotation NonMemMovable;
62 #endif