From 01ada12136c64ad9ff305f456637d43b9f6d4356 Mon Sep 17 00:00:00 2001 From: David Malcolm Date: Tue, 21 Nov 2017 00:40:53 +0000 Subject: C++: provide macro used-before-defined hint (PR c++/72786) This patch uses the name_hint/deferred_diagnostic to provide a message in the C++ frontend if a macro is used before it is defined e.g.: test.c:6:24: error: expected ';' at end of member declaration virtual void clone() const OVERRIDE { } ^~~~~ ; test.c:6:30: error: 'OVERRIDE' does not name a type virtual void clone() const OVERRIDE { } ^~~~~~~~ test.c:6:30: note: the macro 'OVERRIDE' had not yet been defined test.c:15:0: note: it was later defined here #define OVERRIDE override It's possible to do it from the C++ frontend as tokenization happens up-front (and hence the macro already exists when the above is parsed); I attempted to do it from the C frontend, but because the C frontend only tokenizes on-demand during parsing, the macro isn't known about until later. gcc/cp/ChangeLog: PR c++/72786 * name-lookup.c (class macro_use_before_def): New class. (lookup_name_fuzzy): Detect macro that were used before being defined, and report them as such. gcc/ChangeLog: PR c++/72786 * spellcheck.h (best_match::blithely_get_best_candidate): New accessor. gcc/testsuite/ChangeLog: PR c++/72786 * g++.dg/spellcheck-macro-ordering-2.C: New test case. * g++.dg/spellcheck-macro-ordering.C: Add dg-message directives for macro used-before-defined. libcpp/ChangeLog: PR c++/72786 * include/cpplib.h (cpp_macro_definition_location): New decl. * macro.c (cpp_macro_definition): New function. From-SVN: r254978 --- libcpp/ChangeLog | 6 ++++++ libcpp/include/cpplib.h | 1 + libcpp/macro.c | 8 ++++++++ 3 files changed, 15 insertions(+) (limited to 'libcpp') diff --git a/libcpp/ChangeLog b/libcpp/ChangeLog index 70c834c..cc5d4d3 100644 --- a/libcpp/ChangeLog +++ b/libcpp/ChangeLog @@ -1,3 +1,9 @@ +2017-11-20 David Malcolm + + PR c++/72786 + * include/cpplib.h (cpp_macro_definition_location): New decl. + * macro.c (cpp_macro_definition): New function. + 2017-11-13 Tom Tromey * pch.c (cpp_read_state): Set n__VA_OPT__. diff --git a/libcpp/include/cpplib.h b/libcpp/include/cpplib.h index 101b33a..4d04a48 100644 --- a/libcpp/include/cpplib.h +++ b/libcpp/include/cpplib.h @@ -889,6 +889,7 @@ extern const cpp_token *cpp_get_token_with_location (cpp_reader *, extern bool cpp_fun_like_macro_p (cpp_hashnode *); extern const unsigned char *cpp_macro_definition (cpp_reader *, cpp_hashnode *); +extern source_location cpp_macro_definition_location (cpp_hashnode *); extern void _cpp_backup_tokens (cpp_reader *, unsigned int); extern const cpp_token *cpp_peek_token (cpp_reader *, int); diff --git a/libcpp/macro.c b/libcpp/macro.c index bf473ea..43f2baa 100644 --- a/libcpp/macro.c +++ b/libcpp/macro.c @@ -3646,3 +3646,11 @@ cpp_macro_definition (cpp_reader *pfile, cpp_hashnode *node) *buffer = '\0'; return pfile->macro_buffer; } + +/* Get the line at which the macro was defined. */ + +source_location +cpp_macro_definition_location (cpp_hashnode *node) +{ + return node->value.macro->line; +} -- cgit v1.1