summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Malcolm <dmalcolm@redhat.com>2018-10-11 13:21:28 +0000
committerDavid Malcolm <dmalcolm@gcc.gnu.org>2018-10-11 13:21:28 +0000
commit954ad1127eb4d367be1ece52e34ce15334d40c5e (patch)
treedec8d87521395a312aab96b828e7959479b364f8
parentc727b835139299adc623951aff782cdfaff655d9 (diff)
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
-rw-r--r--gcc/testsuite/ChangeLog9
-rw-r--r--gcc/testsuite/c-c++-common/cpp/macro-arg-count-1.c66
-rw-r--r--gcc/testsuite/c-c++-common/cpp/macro-arg-count-2.c36
-rw-r--r--gcc/testsuite/g++.dg/diagnostic/macro-arg-count.C51
-rw-r--r--libcpp/ChangeLog5
-rw-r--r--libcpp/macro.c4
6 files changed, 120 insertions, 51 deletions
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index a0a5dbb55f2..3588376437b 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,12 @@
+2018-10-11 David Malcolm <dmalcolm@redhat.com>
+
+ * 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.
+
2018-10-11 Martin Jambor <mjambor@suse.cz>
* gcc.dg/warn-abs-1.c: Guard tests assuming size of long double is
diff --git a/gcc/testsuite/c-c++-common/cpp/macro-arg-count-1.c b/gcc/testsuite/c-c++-common/cpp/macro-arg-count-1.c
new file mode 100644
index 00000000000..7773c47e765
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/cpp/macro-arg-count-1.c
@@ -0,0 +1,66 @@
+/* { 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 "" } */
+}
diff --git a/gcc/testsuite/c-c++-common/cpp/macro-arg-count-2.c b/gcc/testsuite/c-c++-common/cpp/macro-arg-count-2.c
new file mode 100644
index 00000000000..ef64488348d
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/cpp/macro-arg-count-2.c
@@ -0,0 +1,36 @@
+/* { dg-options "-traditional-cpp" } */
+
+#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 c } use_of_MACRO_1 } */
+ /* { dg-error "macro \"MACRO_1\" requires 2 arguments, but only 1 given" "" { target c++ } use_of_MACRO_1 } */
+ /* { dg-message "-: macro .MACRO_1. defined here" "" { target *-*-* } def_of_MACRO_1 } */
+ /* { dg-error "'MACRO_1' was not declared in this scope" "" { target c++ } use_of_MACRO_1 } */
+ /* { 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 c } use_of_MACRO_2 } */
+ /* { dg-error "macro \"MACRO_2\" passed 3 arguments, but takes just 2" "" { target c++ } use_of_MACRO_2 } */
+ /* { dg-message "-: macro .MACRO_2. defined here" "" { target *-*-* } def_of_MACRO_2 } */
+ /* { dg-error "'MACRO_2' was not declared in this scope" "" { target c++ } use_of_MACRO_2 } */
+ /* { 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' was not declared in this scope" "" { target c++ } use_of_MACRO_4 } */
+}
diff --git a/gcc/testsuite/g++.dg/diagnostic/macro-arg-count.C b/gcc/testsuite/g++.dg/diagnostic/macro-arg-count.C
deleted file mode 100644
index 12b2dbd3be7..00000000000
--- a/gcc/testsuite/g++.dg/diagnostic/macro-arg-count.C
+++ /dev/null
@@ -1,51 +0,0 @@
-// { dg-options "-fdiagnostics-show-caret" }
-
-#define MACRO_1(X,Y)
-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-error "'MACRO_1' was not declared in this scope" "" { target *-*-* } 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)
-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-error "'MACRO_2' was not declared in this scope" "" { target *-*-* } 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-error "'MACRO_4' was not declared in this scope" }
- /* { dg-begin-multiline-output "" }
- MACRO_4;
- ^~~~~~~
- { dg-end-multiline-output "" } */
-}
diff --git a/libcpp/ChangeLog b/libcpp/ChangeLog
index fe0dc8ddfe5..e240b2c7058 100644
--- a/libcpp/ChangeLog
+++ b/libcpp/ChangeLog
@@ -1,3 +1,8 @@
+2018-10-11 David Malcolm <dmalcolm@redhat.com>
+
+ * macro.c (_cpp_arguments_ok): If the argument count is wrong, add
+ a note showing the definition of the macro.
+
2018-10-11 Nathan Sidwell <nathan@acm.org>
* include/line-map.h (LINEMAPS_MACRO_LOWEST_LOCATION): Fix
diff --git a/libcpp/macro.c b/libcpp/macro.c
index 073816dd221..aacaf8c3020 100644
--- a/libcpp/macro.c
+++ b/libcpp/macro.c
@@ -964,6 +964,10 @@ _cpp_arguments_ok (cpp_reader *pfile, cpp_macro *macro, const cpp_hashnode *node
"macro \"%s\" passed %u arguments, but takes just %u",
NODE_NAME (node), argc, macro->paramc);
+ if (macro->line > RESERVED_LOCATION_COUNT)
+ cpp_error_at (pfile, CPP_DL_NOTE, macro->line, "macro \"%s\" defined here",
+ NODE_NAME (node));
+
return false;
}