Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(120)

Side by Side Diff: Source/build/scripts/templates/MakeQualifiedNames.cpp.tmpl

Issue 604743003: MakeNames: Generate global constants in a loop rather than in sequence. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: MakeNames: In a loop instead of in a sequence. Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 {% from "macros.tmpl" import license %} 1 {% from "macros.tmpl" import license %}
2 {{ license() }} 2 {{ license() }}
3 3
4 #include "config.h" 4 #include "config.h"
5 5
6 #include "{{namespace}}Names.h" 6 #include "{{namespace}}Names.h"
7 7
8 #include "wtf/StaticConstructors.h" 8 #include "wtf/StaticConstructors.h"
9 #include "wtf/StdLibExtras.h"
9 10
10 namespace blink { 11 namespace blink {
11 namespace {{namespace}}Names { 12 namespace {{namespace}}Names {
12 13
13 using namespace blink; 14 using namespace blink;
14 15
15 DEFINE_GLOBAL(AtomicString, {{namespace_prefix}}NamespaceURI) 16 DEFINE_GLOBAL(AtomicString, {{namespace_prefix}}NamespaceURI)
16 17
17 {% if tags %} 18 {% if tags %}
18 // Tags 19 // Tags
19 {% for tag in tags|sort %} 20
20 DEFINE_GLOBAL({{namespace}}QualifiedName, {{tag|symbol}}Tag) 21 void* {{suffix}}TagStorage[{{namespace}}TagsCount * ((sizeof({{namespace}}Qualif iedName) + sizeof(void *) - 1) / sizeof(void *))];
22 {% for tag in tags|sort(attribute='name', case_sensitive=True) %}
23 const {{namespace}}QualifiedName& {{tag|symbol}}Tag = reinterpret_cast<{{namespa ce}}QualifiedName*>(&{{suffix}}TagStorage)[{{loop.index0}}];
Jens Widell 2014/09/25 17:16:16 Could you declare an const {{namespace}}Qualifi
Daniel Bratell 2014/09/25 17:48:42 I've tried it but it generates warnings like this:
21 {% endfor %} 24 {% endfor %}
22 25
23 26
24 PassOwnPtr<const {{namespace}}QualifiedName*[]> get{{namespace}}Tags() 27 PassOwnPtr<const {{namespace}}QualifiedName*[]> get{{namespace}}Tags()
25 { 28 {
26 OwnPtr<const {{namespace}}QualifiedName*[]> tags = adoptArrayPtr(new const { {namespace}}QualifiedName*[{{namespace}}TagsCount]); 29 OwnPtr<const {{namespace}}QualifiedName*[]> tags = adoptArrayPtr(new const { {namespace}}QualifiedName*[{{namespace}}TagsCount]);
27 {% for tag in tags|sort %} 30 for (size_t i = 0; i < {{namespace}}TagsCount; i++)
28 tags[{{loop.index0}}] = reinterpret_cast<const {{namespace}}QualifiedName*>( &{{tag|symbol}}Tag); 31 tags[i] = reinterpret_cast<{{namespace}}QualifiedName*>(&{{suffix}}TagSt orage) + i;
29 {% endfor %}
30 return tags.release(); 32 return tags.release();
31 } 33 }
32 34
33 {% endif %} 35 {% endif %}
34 // Attributes 36 // Attributes
35 {% for attr in attrs|sort %} 37
36 DEFINE_GLOBAL(QualifiedName, {{attr|symbol}}Attr) 38 void* {{suffix}}AttrStorage[{{namespace}}AttrsCount * ((sizeof(QualifiedName) + sizeof(void *) - 1) / sizeof(void *))];
39
40 {% for attr in attrs|sort(attribute='name', case_sensitive=True) %}
41 const QualifiedName& {{attr|symbol}}Attr = reinterpret_cast<QualifiedName*>(&{{s uffix}}AttrStorage)[{{loop.index0}}];
37 {% endfor %} 42 {% endfor %}
38 43
39 PassOwnPtr<const QualifiedName*[]> get{{namespace}}Attrs() 44 PassOwnPtr<const QualifiedName*[]> get{{namespace}}Attrs()
40 { 45 {
41 OwnPtr<const QualifiedName*[]> attrs = adoptArrayPtr(new const QualifiedName *[{{namespace}}AttrsCount]); 46 OwnPtr<const QualifiedName*[]> attrs = adoptArrayPtr(new const QualifiedName *[{{namespace}}AttrsCount]);
42 {% for attr in attrs|sort %} 47 for (size_t i = 0; i < {{namespace}}AttrsCount; i++)
43 attrs[{{loop.index0}}] = reinterpret_cast<const blink::QualifiedName*>(&{{at tr|symbol}}Attr); 48 attrs[i] = reinterpret_cast<QualifiedName*>(&{{suffix}}AttrStorage) + i;
44 {% endfor %}
45 return attrs.release(); 49 return attrs.release();
46 } 50 }
47 51
48 void init() 52 void init()
49 { 53 {
54 struct NameEntry {
55 const char* name;
56 unsigned hash;
57 unsigned char length;
58 unsigned char isTag;
59 unsigned char isAttr;
60 };
61
50 // Use placement new to initialize the globals. 62 // Use placement new to initialize the globals.
51 AtomicString {{namespace_prefix}}NS("{{namespace_uri}}", AtomicString::Const ructFromLiteral); 63 AtomicString {{namespace_prefix}}NS("{{namespace_uri}}", AtomicString::Const ructFromLiteral);
52 64
53 // Namespace 65 // Namespace
54 new ((void*)&{{namespace_prefix}}NamespaceURI) AtomicString({{namespace_pref ix}}NS); 66 new ((void*)&{{namespace_prefix}}NamespaceURI) AtomicString({{namespace_pref ix}}NS);
55 {% for name, tag_list in (tags + attrs)|groupby('name')|sort %} 67 {% set tagnames = tags|map(attribute='name')|list() %}
56 StringImpl* {{tag_list[0]|symbol}}Impl = StringImpl::createStatic("{{name}}" , {{name|length}}, {{name|hash}}); 68 {% set attrnames = attrs|map(attribute='name')|list() %}
69 static const NameEntry kNames[] = {
70 {% for name, tag_list in (tags + attrs)|groupby('name')|sort(attribute=0, ca se_sensitive=True) %}
71 { "{{name}}", {{name|hash}}, {{name|length}}, {{ (name in tagnames)|int }}, {{ (name in attrnames)|int }} },
57 {% endfor %} 72 {% endfor %}
73 };
58 74
59 // Tags 75 {% if tags %}
60 {% for tag in tags|sort %} 76 size_t tag_i = 0;
61 QualifiedName::createStatic((void*)&{{tag|symbol}}Tag, {{tag|symbol}}Impl, { {namespace_prefix}}NS); 77 {% endif %}
62 {% endfor %} 78 size_t attr_i = 0;
79 for (size_t i = 0; i < WTF_ARRAY_LENGTH(kNames); i++) {
80 StringImpl* stringImpl = StringImpl::createStatic(kNames[i].name, kNames [i].length, kNames[i].hash);
81 {% if tags %}
82 if (kNames[i].isTag) {
83 void* address = reinterpret_cast<{{namespace}}QualifiedName*>(&{{suf fix}}TagStorage) + tag_i;
84 QualifiedName::createStatic(address, stringImpl, {{namespace_prefix} }NS);
85 tag_i++;
86 }
63 87
64 // Attrs 88 if (!kNames[i].isAttr)
65 {% for attr in attrs|sort %} 89 continue;
66 {% if use_namespace_for_attrs %} 90 {% endif %}
67 QualifiedName::createStatic((void*)&{{attr|symbol}}Attr, {{attr|symbol}}Impl , {{namespace_prefix}}NS); 91 void* address = reinterpret_cast<QualifiedName*>(&{{suffix}}AttrStorage) + attr_i;
68 {% else %} 92 {% if use_namespace_for_attrs %}
69 QualifiedName::createStatic((void*)&{{attr|symbol}}Attr, {{attr|symbol}}Impl ); 93 QualifiedName::createStatic(address, stringImpl, {{namespace_prefix}}NS) ;
94 {% else %}
95 QualifiedName::createStatic(address, stringImpl);
96 {% endif %}
97 attr_i++;
98 }
99 {% if tags %}
100 ASSERT(tag_i == {{namespace}}TagsCount);
70 {% endif %} 101 {% endif %}
71 {% endfor %} 102 ASSERT(attr_i == {{namespace}}AttrsCount);
72 } 103 }
73 104
74 } // {{namespace}} 105 } // {{namespace}}
75 } // namespace blink 106 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698