summaryrefslogtreecommitdiffstats
path: root/build/clang-plugin/MemMoveAnnotation.h
blob: b4233837443de7093a7d560c78dcd1f28d76a131 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/* 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"
#include "CustomTypeAnnotation.h"
#include "Utils.h"

#include <unordered_set>

class MemMoveAnnotation final : public CustomTypeAnnotation {
public:
  MemMoveAnnotation()
      : CustomTypeAnnotation(moz_non_memmovable, "non-memmove()able") {}

  virtual ~MemMoveAnnotation() {}

protected:
  std::string getImplicitReason(const TagDecl *D) const override {
    // Annotate everything in ::std, with a few exceptions; see bug
    // 1201314 for discussion.
    if (getDeclarationNamespace(D) == "std") {
      // This doesn't check that it's really ::std::pair and not
      // ::std::something_else::pair, but should be good enough.
      StringRef Name = getNameChecked(D);
      if (isNameExcepted(Name.data())) {
        return "";
      }
      return "it is an stl-provided type not guaranteed to be memmove-able";
    }
    return "";
  }

private:
  bool isNameExcepted(const char *Name) const {
    static std::unordered_set<std::string> NamesSet = {
        {"pair"},
        {"atomic"},
        // libstdc++ specific names
        {"__atomic_base"},
        {"atomic_bool"},
        {"__cxx_atomic_impl"},
        {"__cxx_atomic_base_impl"},
        {"__pair_base"},
        // MSVCRT specific names
        {"_Atomic_impl"},
        {"_Atomic_base"},
        {"_Atomic_bool"},
        {"_Atomic_char"},
        {"_Atomic_schar"},
        {"_Atomic_uchar"},
        {"_Atomic_char16_t"},
        {"_Atomic_char32_t"},
        {"_Atomic_wchar_t"},
        {"_Atomic_short"},
        {"_Atomic_ushort"},
        {"_Atomic_int"},
        {"_Atomic_uint"},
        {"_Atomic_long"},
        {"_Atomic_ulong"},
        {"_Atomic_llong"},
        {"_Atomic_ullong"},
        {"_Atomic_address"},
        // MSVCRT 2019
        {"_Atomic_integral"},
        {"_Atomic_integral_facade"},
        {"_Atomic_padded"},
        {"_Atomic_pointer"},
        {"_Atomic_storage"}};

    return NamesSet.find(Name) != NamesSet.end();
  }
};

extern MemMoveAnnotation NonMemMovable;

#endif