Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / tools / clang / blink_gc_plugin / Config.h
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // This file defines the names used by GC infrastructure.
6
7 #ifndef TOOLS_BLINK_GC_PLUGIN_CONFIG_H_
8 #define TOOLS_BLINK_GC_PLUGIN_CONFIG_H_
9
10 #include "clang/AST/AST.h"
11
12 const char kNewOperatorName[] = "operator new";
13 const char kTraceName[] = "trace";
14 const char kTraceAfterDispatchName[] = "traceAfterDispatch";
15 const char kRegisterWeakMembersName[] = "registerWeakMembers";
16 const char kHeapAllocatorName[] = "HeapAllocator";
17
18 // TODO: Expand template aliases instead of using StartsWith/EndsWith patterns.
19
20 class Config {
21  public:
22
23   static bool IsMember(const std::string& name) {
24     return name == "Member"
25         || (oilpan_enabled() && EndsWith(name, "WillBeMember"));
26   }
27
28   static bool IsWeakMember(const std::string& name) {
29     return name == "WeakMember"
30         || (oilpan_enabled() && EndsWith(name, "WillBeWeakMember"));
31   }
32
33   static bool IsMemberHandle(const std::string& name) {
34     return IsMember(name)
35         || IsWeakMember(name);
36   }
37
38   static bool IsPersistentHandle(const std::string& name) {
39     return name == "Persistent"
40         || (oilpan_enabled() && EndsWith(name, "WillBePersistent"));
41   }
42
43   static bool IsRefPtr(const std::string& name) {
44     return name == "RefPtr"
45         || (!oilpan_enabled() && StartsWith(name, "RefPtrWillBe"));
46   }
47
48   static bool IsOwnPtr(const std::string& name) {
49     return name == "OwnPtr"
50         || (!oilpan_enabled() && StartsWith(name, "OwnPtrWillBe"));
51   }
52
53   static bool IsWTFCollection(const std::string& name) {
54     return name == "Vector"
55         || name == "HashSet"
56         || name == "HashMap"
57         || name == "HashCountedSet"
58         || name == "ListHashSet"
59         || name == "Deque"
60         || (!oilpan_enabled() && StartsWith(name, "WillBeHeap"));
61   }
62
63   static bool IsGCCollection(const std::string& name) {
64     return name == "HeapVector"
65         || name == "HeapHashMap"
66         || name == "HeapHashSet"
67         || (oilpan_enabled() && StartsWith(name, "WillBeHeap"));
68   }
69
70   static bool IsGCFinalizedBase(const std::string& name) {
71     return name == "GarbageCollectedFinalized"
72         || name == "RefCountedGarbageCollected"
73         || (oilpan_enabled()
74             && (EndsWith(name, "WillBeGarbageCollectedFinalized") ||
75                 EndsWith(name, "WillBeRefCountedGarbageCollected")));
76   }
77
78   static bool IsGCBase(const std::string& name) {
79     return name == "GarbageCollected"
80         || IsGCFinalizedBase(name)
81         || (oilpan_enabled() && EndsWith(name, "WillBeGarbageCollected"));
82   }
83
84   static bool IsTraceMethod(clang::CXXMethodDecl* method,
85                             bool* isTraceAfterDispatch = 0) {
86     const std::string& name = method->getNameAsString();
87     if (name == kTraceName || name == kTraceAfterDispatchName) {
88       if (isTraceAfterDispatch)
89         *isTraceAfterDispatch = (name == kTraceAfterDispatchName);
90       return true;
91     }
92     return false;
93   }
94
95   static bool StartsWith(const std::string& str, const std::string& prefix) {
96     if (prefix.size() > str.size())
97       return false;
98     return str.compare(0, prefix.size(), prefix) == 0;
99   }
100
101   static bool EndsWith(const std::string& str, const std::string& suffix) {
102     if (suffix.size() > str.size())
103       return false;
104     return str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
105   }
106
107   static void set_oilpan_enabled(bool enabled) { oilpan_enabled_ = enabled; }
108   static bool oilpan_enabled() { return oilpan_enabled_; }
109
110  private:
111   static bool oilpan_enabled_;
112 };
113
114 #endif // TOOLS_BLINK_GC_PLUGIN_CONFIG_H_