blob: 8345b2433a3fe149381c291db9d4ec853c4d7268 [file] [log] [blame]
Daniel Dunbar8fbe78f2009-12-15 20:14:24 +00001// RUN: %clang_cc1 -fsyntax-only -verify %s
Charles Lie7cbb3e2015-11-17 20:25:05 +00002// RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 %s
3// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
4
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005void f(int i);
Chris Lattner0369c572008-11-23 23:12:31 +00006void f(int i = 0); // expected-note {{previous definition is here}}
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00007void f(int i = 17); // expected-error {{redefinition of default argument}}
8
9
10void g(int i, int j, int k = 3);
11void g(int i, int j = 2, int k);
12void g(int i = 1, int j, int k);
13
14void h(int i, int j = 2, int k = 3,
15 int l, // expected-error {{missing default argument on parameter 'l'}}
16 int, // expected-error {{missing default argument on parameter}}
17 int n);// expected-error {{missing default argument on parameter 'n'}}
18
19struct S { } s;
Douglas Gregor4f4946a2010-04-22 00:20:18 +000020void i(int = s) { } // expected-error {{no viable conversion}} \
21// expected-note{{passing argument to parameter here}}
Douglas Gregor5496d4c2008-11-04 13:57:51 +000022
23struct X {
24 X(int);
25};
26
Kaelyn Uhrain476c8232013-07-08 23:13:44 +000027void j(X x = 17); // expected-note{{'::j' declared here}}
Douglas Gregor5496d4c2008-11-04 13:57:51 +000028
Charles Lie7cbb3e2015-11-17 20:25:05 +000029struct Y { // expected-note 2{{candidate constructor (the implicit copy constructor) not viable}}
30#if __cplusplus >= 201103L // C++11 or later
31// expected-note@-2 2 {{candidate constructor (the implicit move constructor) not viable}}
32#endif
33
Richard Smith25195542020-01-09 12:27:48 -080034 explicit Y(int); // expected-note 2{{explicit constructor is not a candidate}}
Douglas Gregor5496d4c2008-11-04 13:57:51 +000035};
36
Douglas Gregor4f4946a2010-04-22 00:20:18 +000037void k(Y y = 17); // expected-error{{no viable conversion}} \
38// expected-note{{passing argument to parameter 'y' here}}
Douglas Gregor58354032008-12-24 00:01:03 +000039
Douglas Gregor4f4946a2010-04-22 00:20:18 +000040void kk(Y = 17); // expected-error{{no viable conversion}} \
41// expected-note{{passing argument to parameter here}}
James Molloye9430032012-03-13 08:55:35 +000042
43int l () {
44 int m(int i, int j, int k = 3);
45 if (1)
46 {
47 int m(int i, int j = 2, int k = 4);
48 m(8);
49 }
50 return 0;
51}
52
53int i () {
54 void j (int f = 4);
55 {
Kaelyn Uhrain476c8232013-07-08 23:13:44 +000056 void j (int f);
57 j(); // expected-error{{too few arguments to function call, expected 1, have 0; did you mean '::j'?}}
58 }
59 void jj (int f = 4);
60 {
61 void jj (int f); // expected-note{{'jj' declared here}}
62 jj(); // expected-error{{too few arguments to function call, single argument 'f' was not specified}}
Richard Smithd72da152012-05-15 06:21:54 +000063 }
64}
65
66int i2() {
67 void j(int f = 4); // expected-note{{'j' declared here}}
68 {
69 j(2, 3); // expected-error{{too many arguments to function call, expected at most single argument 'f', have 2}}
James Molloye9430032012-03-13 08:55:35 +000070 }
71}
Serge Pavlovb4b35782014-07-22 01:54:49 +000072
73int pr20055_f(int x = 0, int y = UNDEFINED); // expected-error{{use of undeclared identifier}}
74int pr20055_v = pr20055_f(0);
Richard Smith5971e8c2014-08-27 22:31:34 +000075
76void PR20769() { void PR20769(int = 1); }
77void PR20769(int = 2);
78
79void PR20769_b(int = 1);
80void PR20769_b() { void PR20769_b(int = 2); }
Richard Smithde47d662019-05-24 21:08:12 +000081
82#if __cplusplus >= 201103L
83template<typename T> constexpr int f1() { return 0; }
84// This is OK, but in order to see that we must instantiate f<int>, despite it
85// being in an unused default argument.
86void g1(char c = {f1<int>()}) {} // expected-warning {{braces around scalar}}
87
88// This is formally ill-formed, but we choose to not trigger instantiation here
89// (at least, not until g2 is actually called in a way that uses the default
90// argument).
91template<typename T> int f2() { return T::error; }
92void g2(int c = f2<int>()) {}
93
94// FIXME: Provide a note pointing at the first use of the default argument?
95template<typename T> int f3() { return T::error; } // expected-error {{no members}}
96void g3(int c = f3<int>()) {} // expected-note {{in instantiation of}}
97void use_g3() { g3(); }
Richard Smithe92be7c2021-01-22 15:43:47 -080098
99namespace PR47682 {
100 inline namespace A {
101 void f(int = 0);
102 }
103}
104void PR47682::f(int) {}
105void PR47682_test() { PR47682::f(); }
Richard Smithde47d662019-05-24 21:08:12 +0000106#endif