From patchwork Mon Nov 19 20:52:42 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ramsay Jones X-Patchwork-Id: 10689429 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id B31A814BD for ; Mon, 19 Nov 2018 20:52:47 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A559E2A5E2 for ; Mon, 19 Nov 2018 20:52:47 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 99E332A601; Mon, 19 Nov 2018 20:52:47 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1B5B12A5E2 for ; Mon, 19 Nov 2018 20:52:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730904AbeKTHSK (ORCPT ); Tue, 20 Nov 2018 02:18:10 -0500 Received: from avasout04.plus.net ([212.159.14.19]:52919 "EHLO avasout04.plus.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730828AbeKTHSJ (ORCPT ); Tue, 20 Nov 2018 02:18:09 -0500 Received: from [10.0.2.15] ([146.198.133.33]) by smtp with ESMTPA id OqXDg9poIYyh2OqXEgYm8p; Mon, 19 Nov 2018 20:52:45 +0000 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.3 cv=MoN8FVSe c=1 sm=1 tr=0 a=VCDsReDbrwk4B7AcQzWGLw==:117 a=VCDsReDbrwk4B7AcQzWGLw==:17 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=t_2rAVzgGEZjFWMiDsEA:9 a=QEXdDO2ut3YA:10 a=yJM6EZoI5SlJf8ks9Ge_:22 X-AUTH: ramsayjones@:2500 To: Luc Van Oostenryck Cc: Sparse Mailing-list From: Ramsay Jones Subject: [PATCH 7/9] pre-process: add the -dM option to dump macro definitions Message-ID: <2ee1506a-5e42-5736-5577-230e5f365fb5@ramsayjones.plus.com> Date: Mon, 19 Nov 2018 20:52:42 +0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.2.1 MIME-Version: 1.0 Content-Language: en-GB X-CMAE-Envelope: MS4wfGuG/x41Z94JG+gCgy7OjzYyxPAXg1e+i+UcgxjLZFqJrQcVRcKS4YFDi8gr8hnk6YiyHgJIqf7zhUrmz59clU07HpvzTKUZy+2L7G/PSs5V7KxMIwAc q4uzdVW6/tH38SMEmAVQKq9hZYulFgcCP5CG7Mz9d1aIkq3ofKVW7xSOgpkBOfz2IounsGTgFOmDow== Sender: linux-sparse-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-sparse@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The current -dD option outputs the macro definitions, in addition to the pre-processed text. In contrast, the -dM option outputs only the macro definitions. Signed-off-by: Ramsay Jones --- lib.c | 41 +++++++++++++++++----- lib.h | 1 + validation/preprocessor/dump-macros-only.c | 36 +++++++++++++++++++ validation/preprocessor/dump-macros.c | 6 ++++ 4 files changed, 75 insertions(+), 9 deletions(-) create mode 100644 validation/preprocessor/dump-macros-only.c diff --git a/lib.c b/lib.c index 07a5b9c..23c3d27 100644 --- a/lib.c +++ b/lib.c @@ -287,6 +287,7 @@ int Wunknown_attribute = 0; int Wvla = 1; int dump_macro_defs = 0; +int dump_macros_only = 0; int dbg_compound = 0; int dbg_dead = 0; @@ -794,16 +795,34 @@ static char **handle_switch_v(char *arg, char **next) return next; } -static struct flag dumps[] = { - { "D", &dump_macro_defs}, -}; - static char **handle_switch_d(char *arg, char **next) { - char ** ret = handle_onoff_switch(arg, next, dumps, ARRAY_SIZE(dumps)); - if (ret) - return ret; + char *arg_char = arg + 1; + /* + * -d, where is a sequence of characters, not preceded + * by a space. If you specify characters whose behaviour conflicts, + * the result is undefined. + */ + while (*arg_char) { + switch (*arg_char) { + case 'M': /* dump just the macro definitions */ + dump_macros_only = 1; + dump_macro_defs = 0; + break; + case 'D': /* like 'M', but also output pre-processed text */ + dump_macro_defs = 1; + dump_macros_only = 0; + break; + case 'N': /* like 'D', but only output macro names not bodies */ + break; + case 'I': /* like 'D', but also output #include directives */ + break; + case 'U': /* like 'D', but only output expanded macros */ + break; + } + arg_char++; + } return next; } @@ -1311,8 +1330,12 @@ static struct symbol_list *sparse_tokenstream(struct token *token) // Preprocess the stream token = preprocess(token); - if (dump_macro_defs && !builtin) - dump_macro_definitions(); + if (dump_macro_defs || dump_macros_only) { + if (!builtin) + dump_macro_definitions(); + if (dump_macros_only) + return NULL; + } if (preprocess_only) { while (!eof_token(token)) { diff --git a/lib.h b/lib.h index cd1af9c..ae0e981 100644 --- a/lib.h +++ b/lib.h @@ -177,6 +177,7 @@ extern int Wunknown_attribute; extern int Wvla; extern int dump_macro_defs; +extern int dump_macros_only; extern int dbg_compound; extern int dbg_dead; diff --git a/validation/preprocessor/dump-macros-only.c b/validation/preprocessor/dump-macros-only.c new file mode 100644 index 0000000..cee6f87 --- /dev/null +++ b/validation/preprocessor/dump-macros-only.c @@ -0,0 +1,36 @@ + +#define ABC abc +#undef ABC + +#define DEF def +#undef DEF +#define DEF xyz + +#define NYDEF ydef + +#define STRING(x) #x +#define CONCAT(x,y) x ## y + +#define unlocks(...) annotate(unlock_func(__VA_ARGS__)) +#define apply(x,...) x(__VA_ARGS__) + +int main(int argc, char *argv[]) +{ + return 0; +} +/* + * check-name: dump-macros only -dM + * check-command: sparse -E -dM -DIJK=ijk -UNDEF -UNYDEF $file + * + * check-output-ignore +check-output-pattern(1): #define __CHECKER__ 1 +check-output-contains: #define IJK ijk +check-output-contains: #define DEF xyz +check-output-contains: #define NYDEF ydef +check-output-contains: #define STRING(x) #x +check-output-contains: #define CONCAT(x,y) x ## y +check-output-contains: #define unlocks(...) annotate(unlock_func(__VA_ARGS__)) +check-output-contains: #define apply(x,...) x(__VA_ARGS__) +check-output-excludes: int main(int argc, char \\*argv\\[\\]) +check-output-excludes: ^\\[^#] + */ diff --git a/validation/preprocessor/dump-macros.c b/validation/preprocessor/dump-macros.c index 6940a20..dc2d7d1 100644 --- a/validation/preprocessor/dump-macros.c +++ b/validation/preprocessor/dump-macros.c @@ -12,6 +12,11 @@ #define unlocks(...) annotate(unlock_func(__VA_ARGS__)) #define apply(x,...) x(__VA_ARGS__) + +int main(int argc, char *argv[]) +{ + return 0; +} /* * check-name: dump-macros * check-command: sparse -E -dD -DIJK=ijk -UNDEF -UNYDEF $file @@ -25,4 +30,5 @@ check-output-contains: #define STRING(x) #x check-output-contains: #define CONCAT(x,y) x ## y check-output-contains: #define unlocks(...) annotate(unlock_func(__VA_ARGS__)) check-output-contains: #define apply(x,...) x(__VA_ARGS__) +check-output-contains: int main(int argc, char \\*argv\\[\\]) */