cxx0x-for-range.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // RUN: not %clang_cc1 -fsyntax-only -fdiagnostics-parseable-fixits -std=c++11 %s 2>&1 | FileCheck %s
  2. template<typename T, typename U>
  3. struct pair {};
  4. template<typename T, typename U>
  5. struct map {
  6. typedef pair<T,U> *iterator;
  7. iterator begin();
  8. iterator end();
  9. };
  10. template<typename T, typename U>
  11. pair<T,U> &tie(T &, U &);
  12. int foo(map<char*,int> &m) {
  13. char *p;
  14. int n;
  15. for (pair<char*,int> x : m) {
  16. (void)x;
  17. }
  18. for (tie(p, n) : m) { // expected-error {{for range declaration must declare a variable}}
  19. (void)p;
  20. (void)n;
  21. }
  22. return n;
  23. }
  24. namespace PR19176 {
  25. struct Vector {
  26. struct iterator {
  27. int &operator*();
  28. iterator &operator++();
  29. iterator &operator++(int);
  30. bool operator==(const iterator &) const;
  31. };
  32. iterator begin();
  33. iterator end();
  34. };
  35. void f() {
  36. Vector v;
  37. int a[] = {1, 2, 3, 4};
  38. for (auto foo = a) // expected-error {{range-based 'for' statement uses ':', not '='}}
  39. // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:19-[[@LINE-1]]:20}:":"
  40. (void)foo;
  41. for (auto i
  42. =
  43. v) // expected-error@-1 {{range-based 'for' statement uses ':', not '='}}
  44. // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:7-[[@LINE-2]]:8}:":"
  45. (void)i;
  46. #define FORRANGE(v, a) for (DECLVARWITHINIT(v) a) // expected-note {{expanded from macro}}
  47. #define DECLAUTOVAR(v) auto v
  48. #define DECLVARWITHINIT(v) DECLAUTOVAR(v) = // expected-note {{expanded from macro}}
  49. FORRANGE(i, a) { // expected-error {{range-based 'for' statement uses ':', not '='}}
  50. }
  51. }
  52. }