From 222e2a7620e6520ffaf4fc4e69d79c18da31542e Mon Sep 17 00:00:00 2001 From: "Zancanaro; Carlo" Date: Mon, 24 Sep 2012 09:58:17 +1000 Subject: Add the clang library to the repo (with some of my changes, too). --- clang/test/SemaCXX/for-range-examples.cpp | 182 ++++++++++++++++++++++++++++++ 1 file changed, 182 insertions(+) create mode 100644 clang/test/SemaCXX/for-range-examples.cpp (limited to 'clang/test/SemaCXX/for-range-examples.cpp') diff --git a/clang/test/SemaCXX/for-range-examples.cpp b/clang/test/SemaCXX/for-range-examples.cpp new file mode 100644 index 0000000..8bda510 --- /dev/null +++ b/clang/test/SemaCXX/for-range-examples.cpp @@ -0,0 +1,182 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++11 + +namespace value_range_detail { + template + class value_range_iter { + T t; + public: + value_range_iter(const T &t) : t(t) {} + T operator*() const { return t; } + bool operator!=(const value_range_iter &o) const { return t != o.t; } + value_range_iter &operator++() { ++t; return *this; } + }; + + template + struct value_range { + value_range(const T &a, const T &b) : begin_(a), end_(b) {} + value_range_iter begin_, end_; + }; + + template + value_range_iter begin(const value_range &r) { return r.begin_; } + template + value_range_iter end(const value_range &r) { return r.end_; } + + + struct end_t {}; + + template + class value_range_step_iter { + T it, step; + public: + value_range_step_iter(const T &it, const T &step) : it(it), step(step) {} + T operator*() const { return it; } + bool operator!=(value_range_step_iter end) const { return it != end.it; } + value_range_step_iter &operator++() { it += step; return *this; } + }; + + template + class value_range_step { + T it, step, end_; + public: + value_range_step(const T &it, const T &end, const T &step) : + it(it), end_(end), step(step) {} + typedef value_range_step_iter iterator; + iterator begin() const { return iterator(it, step); } + iterator end() const { return iterator(end_, step); } + }; +} + +template +value_range_detail::value_range range(const T &a, const T &b) { return value_range_detail::value_range(a, b); } + +template +value_range_detail::value_range_step range(const T &a, const T &b, const T &step) { return value_range_detail::value_range_step(a, b, step); } + + +namespace map_range { + template + class vector { + T storage[100]; + decltype(sizeof(char)) size; + public: + vector() : size() {} + void push_back(T t) { storage[size++] = t; } + T *begin() { return storage; } + T *end() { return storage + size; } + }; + + template struct tuple_elem { + T t; + tuple_elem() {} + tuple_elem(T t) : t(t) {} + }; + template + struct tuple : tuple_elem... { + tuple() : tuple_elem()... {} + tuple(A... a) : tuple_elem(a)... {} + template B &get() { return tuple_elem::t; } + }; + + template + class map_iter { + F f; + I i; + public: + map_iter(F f, I i) : f(f), i(i) {} + auto operator*() const -> decltype(f(*i)) { return f(*i); } + bool operator!=(const map_iter &o) const { return i != o.i; } + map_iter &operator++() { ++i; return *this; } + }; + + template + struct iter_pair { + T begin_, end_; + iter_pair(T begin, T end) : begin_(begin), end_(end) {} + }; + template T begin(iter_pair p) { return p.begin_; } + template T end(iter_pair p) { return p.end_; } + + template class mem_fun_impl; + template + class mem_fun_impl { + typedef R (T::*F)(A...); + F f; + public: + mem_fun_impl(F f) : f(f) {} + R operator()(T &t, A &&...a) const { return (t.*f)(static_cast(a)...); } + }; + template mem_fun_impl mem_fun(F f) { return mem_fun_impl(f); } + + template + auto map(const F &f, T &t) -> iter_pair> { + typedef map_iter iter; + return iter_pair(iter(f, t.begin()), iter(f, t.end())); + } +} + +#define assert(b) if (!b) { return 1; } +int main() { + int total = 0; + + for (auto n : range(1, 5)) { + total += n; + } + assert(total == 10); + + for (auto n : range(10, 100, 10)) { + total += n; + } + assert(total == 460); + + map_range::vector chars; + chars.push_back('a'); + chars.push_back('b'); + chars.push_back('c'); + for (char c : chars) { + ++total; + } + assert(total == 463); + + typedef map_range::tuple T; + map_range::vector pairs; + pairs.push_back(T(42, 12.9)); + pairs.push_back(T(6, 4.2)); + pairs.push_back(T(9, 1.1)); + for (auto a : map(map_range::mem_fun(&T::get), pairs)) { + total += a; + } + assert(total == 500); +} + +// PR11793 +namespace test2 { + class A { + int xs[10]; // expected-note {{implicitly declared private here}} + }; + void test(A &a) { + for (int x : a.xs) { } // expected-error {{'xs' is a private member of 'test2::A'}} + } +} + +namespace test3 { + // Make sure this doesn't crash + struct A {}; + struct B { ~B(); operator bool(); }; + struct C { B operator!=(const C&); C& operator++(); int operator*(); }; + C begin(const A&); + C end(const A&); + template void f() { for (auto a : A()) {} } + void g() { f(); } +} + +namespace test4 { + void f() { + int y; + + // Make sure these don't crash. Better diagnostics would be nice. + for (: {1, 2, 3}) {} // expected-error {{expected expression}} expected-error {{expected ';'}} + for (x : {1, 2, 3}) {} // expected-error {{undeclared identifier}} expected-error {{expected ';'}} + for (y : {1, 2, 3}) {} // expected-error {{must declare a variable}} expected-warning {{result unused}} + } +} -- cgit v1.2.3