summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorRamsay Jones <ramsay@ramsayjones.plus.com>2018-11-19 20:52:42 +0000
committerLuc Van Oostenryck <luc.vanoostenryck@gmail.com>2018-11-24 18:29:01 +0100
commit500e7908ab50a9388688dff8704df0fc20d0bea9 (patch)
tree5a8c265fa63418f120ef13be0b7409a7697f122e
parent5607ba28a4e67dd549cd2b7420f4bbe251d85a88 (diff)
downloadsparse-500e7908ab50a9388688dff8704df0fc20d0bea9.tar.gz
pre-process: add the -dM option to dump macro definitions
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 <ramsay@ramsayjones.plus.com> Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
-rw-r--r--lib.c41
-rw-r--r--lib.h1
-rw-r--r--validation/preprocessor/dump-macros-only.c36
-rw-r--r--validation/preprocessor/dump-macros.c6
4 files changed, 75 insertions, 9 deletions
diff --git a/lib.c b/lib.c
index 5c2059f2..8f86b7b8 100644
--- a/lib.c
+++ b/lib.c
@@ -288,6 +288,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;
@@ -796,16 +797,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<CHARS>, where <CHARS> 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;
}
@@ -1313,8 +1332,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 cde74e93..c30d4e3d 100644
--- a/lib.h
+++ b/lib.h
@@ -178,6 +178,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 00000000..cee6f870
--- /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 6940a20c..dc2d7d19 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\\[\\])
*/