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

Side by Side Diff: tools/clang/rewrite_to_chrome_style/tests/template-original.cc

Issue 2616213003: Get more consistent decisions if a statement is constant in templates (Closed)
Patch Set: consistant-consts Created 3 years, 11 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
« no previous file with comments | « tools/clang/rewrite_to_chrome_style/tests/template-expected.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <type_traits> 5 #include <type_traits>
6 6
7 namespace not_blink { 7 namespace not_blink {
8 8
9 void function(int x) {} 9 void function(int x) {}
10 10
(...skipping 18 matching lines...) Expand all
29 template <typename T = Class> 29 template <typename T = Class>
30 class TemplatedClass { 30 class TemplatedClass {
31 public: 31 public:
32 void anotherMethod() { T::staticMethodTemplate(123); } 32 void anotherMethod() { T::staticMethodTemplate(123); }
33 }; 33 };
34 34
35 } // not_blink 35 } // not_blink
36 36
37 namespace blink { 37 namespace blink {
38 38
39 bool functionNotMarkedConstexpr(int a) {
40 return a == 4 || a == 10;
41 }
42
43 template <typename T>
44 bool templatedFunctionNotMarkedConstexpr(T t) {
45 return !!t;
46 }
47
48 int g_globalNumber;
49
39 template <typename T, int number> 50 template <typename T, int number>
40 void F() { 51 void F() {
41 // We don't assert on this, and we don't end up considering it a const for 52 // These are const but hacker_case so we leave them alone.
42 // now.
43 const int maybe_a_const = sizeof(T); 53 const int maybe_a_const = sizeof(T);
44 const int is_a_const = number; 54 const int is_a_const = number;
55 // These are const expressions so they get a k prefix.
56 const int maybeAConstToo = sizeof(T);
57 const int isAConstToo = number;
58 // These are built from calls to functions which produces inconsistent
59 // results so they should not be considered const to be safe.
60 const bool fromAMethod = functionNotMarkedConstexpr(number);
61 const bool fromATemplatedMethod = templatedFunctionNotMarkedConstexpr(number);
62 // A complex statement of const things is const.
63 const bool complexConst = number || (number + 1);
64 // A complex statement with a non-const thing is not const.
65 const bool complexNotConst = number || (g_globalNumber + 1);
45 } 66 }
46 67
47 template <int number, typename... T> 68 template <int number, typename... T>
48 void F() { 69 void F() {
49 // We don't assert on this, and we don't end up considering it a const for 70 // These are const but hacker_case so we leave them alone.
50 // now.
51 const int maybe_a_const = sizeof...(T); 71 const int maybe_a_const = sizeof...(T);
52 const int is_a_const = number; 72 const int is_a_const = number;
73 // These are const expressions so they get a k prefix.
74 const int maybeAConstToo = sizeof...(T);
75 const int isAConstToo = number;
53 } 76 }
54 77
55 namespace test_template_arg_is_function { 78 namespace test_template_arg_is_function {
56 79
57 void f(int x) {} 80 void f(int x) {}
58 81
59 template <typename T, void g(T)> 82 template <typename T, void g(T)>
60 void h(T x) { 83 void h(T x) {
61 g(x); 84 g(x);
62 } 85 }
63 86
64 void test() { 87 void test() {
65 // f should be rewritten. 88 // f should be rewritten.
66 h<int, f>(0); 89 h<int, f>(0);
67 // Non-Blink should stay the same. 90 // Non-Blink should stay the same.
68 h<int, not_blink::function>(1); 91 h<int, not_blink::function>(1);
92
93 // The int one makes the methods called from F() considered as constexpr, and
94 // can be collapsed to not have template arguments before it reaches the AST.
95 F<int, 10>();
96 // The enum one makes them not constexpr, as it doesn't collapse away the
97 // template stuff as much. This can lead to conflicting decisions about
98 // the names inside F() vs the above instantiation.
99 enum E { A };
100 F<E, 11>();
69 } 101 }
70 102
71 } // namespace test_template_arg_is_function 103 } // namespace test_template_arg_is_function
72 104
73 namespace test_template_arg_is_method { 105 namespace test_template_arg_is_method {
74 106
75 class Class { 107 class Class {
76 public: 108 public:
77 void method() {} 109 void method() {}
78 }; 110 };
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after
302 // Need to rewrite |f| to |F| below (because this method name 334 // Need to rewrite |f| to |F| below (because this method name
303 // does get rewritten when processing blink::test_unnamed_arg::Class). 335 // does get rewritten when processing blink::test_unnamed_arg::Class).
304 // See also https://crbug.com/670434. 336 // See also https://crbug.com/670434.
305 output->f(123); 337 output->f(123);
306 } 338 }
307 }; 339 };
308 340
309 } // namespace blink_methods_called_from_mojo_traits_are_not_rewritten 341 } // namespace blink_methods_called_from_mojo_traits_are_not_rewritten
310 342
311 } // namespace not_blink 343 } // namespace not_blink
OLDNEW
« no previous file with comments | « tools/clang/rewrite_to_chrome_style/tests/template-expected.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698