8sa1-gcc/gcc/testsuite/c-c++-common/cpp/macro-arg-count-1.c
David Malcolm 954ad1127e libcpp: show macro definition when used with wrong argument count
Consider:

demo.c: In function 'test':
demo.c:5:40: error: macro "LOG_2" requires 3 arguments, but only 2 given
5 |   LOG_2 ("loading file: %s\n", filename);
  |                                        ^

This patch adds a note showing the definition of the macro in
question, giving:

demo.c: In function 'test':
demo.c:5:40: error: macro "LOG_2" requires 3 arguments, but only 2 given
5 |   LOG_2 ("loading file: %s\n", filename);
  |                                        ^
In file included from demo.c:1:
logging.h:1: note: macro "LOG_2" defined here
1 | #define LOG_2(FMT, ARG0, ARG1) do { fprintf (stderr, (FMT), (ARG0), (ARG1)); }
  | 

gcc/testsuite/ChangeLog:
	* g++.dg/diagnostic/macro-arg-count.C: Move to...
	* c-c++-common/cpp/macro-arg-count-1.c: ...here, generalizing
	output for C vs C++.  Expect notes showing the definitions of the
	macros.
	* c-c++-common/cpp/macro-arg-count-2.c: New test, adapted from the
	above.

libcpp/ChangeLog:
	* macro.c (_cpp_arguments_ok): If the argument count is wrong, add
	a note showing the definition of the macro.

From-SVN: r265040
2018-10-11 13:21:28 +00:00

67 lines
2.2 KiB
C

/* { dg-options "-fdiagnostics-show-caret" } */
#define MACRO_1(X,Y) /* { dg-line "def_of_MACRO_1" } */
void test_1 ()
{
MACRO_1(42); /* { dg-line "use_of_MACRO_1" } */
/* { dg-error "macro \"MACRO_1\" requires 2 arguments, but only 1 given" "" { target *-*-* } use_of_MACRO_1 } */
/* { dg-begin-multiline-output "" }
MACRO_1(42);
^
{ dg-end-multiline-output "" } */
/* { dg-message "-: macro .MACRO_1. defined here" "" { target *-*-* } def_of_MACRO_1 }
/* { dg-begin-multiline-output "" }
#define MACRO_1(X,Y)
{ dg-end-multiline-output "" } */
/* { dg-error "'MACRO_1' undeclared" "" { target c } use_of_MACRO_1 }
/* { dg-error "'MACRO_1' was not declared in this scope" "" { target c++ } use_of_MACRO_1 }
/* { dg-begin-multiline-output "" }
MACRO_1(42);
^~~~~~~
{ dg-end-multiline-output "" } */
/* { dg-bogus "had not yet been defined" "" { target *-*-* } use_of_MACRO_1 } */
}
#define MACRO_2(X,Y) /* { dg-line "def_of_MACRO_2" } */
void test_2 ()
{
MACRO_2(1, 2, 3); /* { dg-line "use_of_MACRO_2" } */
/* { dg-error "macro \"MACRO_2\" passed 3 arguments, but takes just 2" "" { target *-*-* } use_of_MACRO_2 } */
/* { dg-begin-multiline-output "" }
MACRO_2(1, 2, 3);
^
{ dg-end-multiline-output "" } */
/* { dg-message "-: macro .MACRO_2. defined here" "" { target *-*-* } def_of_MACRO_2 }
/* { dg-begin-multiline-output "" }
#define MACRO_2(X,Y)
{ dg-end-multiline-output "" } */
/* { dg-error "'MACRO_2' undeclared" "" { target c } use_of_MACRO_2 } */
/* { dg-error "'MACRO_2' was not declared in this scope" "" { target c++ } use_of_MACRO_2 } */
/* { dg-begin-multiline-output "" }
MACRO_2(1, 2, 3);
^~~~~~~
{ dg-end-multiline-output "" } */
/* { dg-bogus "had not yet been defined" "" { target *-*-* } use_of_MACRO_2 } */
}
#define MACRO_3
void test_3 ()
{
MACRO_3 (42);
}
#define MACRO_4(X,Y)
void test_4 ()
{
MACRO_4; /* { dg-line "use_of_MACRO_4" } */
/* { dg-error "'MACRO_4' undeclared" "" { target c } use_of_MACRO_4 } */
/* { dg-error "'MACRO_4' was not declared in this scope" "" { target c++ } use_of_MACRO_4 } */
/* { dg-begin-multiline-output "" }
MACRO_4;
^~~~~~~
{ dg-end-multiline-output "" } */
}