* [PATCH 2/3] add sparse_basename()
2017-04-05 1:14 [PATCH 0/3] add real support for __BASE_FILE__ Luc Van Oostenryck
2017-04-05 1:14 ` [PATCH 1/3] build: add missing #include "compat.h" Luc Van Oostenryck
@ 2017-04-05 1:14 ` Luc Van Oostenryck
2017-04-05 1:14 ` [PATCH 3/3] add real support for __BASE_FILE__ Luc Van Oostenryck
2 siblings, 0 replies; 4+ messages in thread
From: Luc Van Oostenryck @ 2017-04-05 1:14 UTC (permalink / raw)
To: linux-sparse; +Cc: Christopher Li, Luc Van Oostenryck
Add sparse_basename() as a simplified version of basename()
working on Unix and non-unix environments.
Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
Makefile | 1 +
compat.h | 1 +
compat/basename.c | 16 ++++++++++++++++
3 files changed, 18 insertions(+)
create mode 100644 compat/basename.c
diff --git a/Makefile b/Makefile
index e8edd95f6..b4826728c 100644
--- a/Makefile
+++ b/Makefile
@@ -107,6 +107,7 @@ LIB_OBJS= target.o parse.o tokenize.o pre-process.o symbol.o lib.o scope.o \
expression.o show-parse.o evaluate.o expand.o inline.o linearize.o \
char.o sort.o allocate.o compat-$(OS).o ptrlist.o \
builtin.o \
+ compat/basename.o \
flow.o cse.o simplify.o memops.o liveness.o storage.o unssa.o dissect.o
LIB_FILE= libsparse.a
diff --git a/compat.h b/compat.h
index 9814ae3e8..5b69548af 100644
--- a/compat.h
+++ b/compat.h
@@ -24,5 +24,6 @@ struct stat;
void *blob_alloc(unsigned long size);
void blob_free(void *addr, unsigned long size);
long double string_to_ld(const char *nptr, char **endptr);
+const char *sparse_basename(const char *path);
#endif
diff --git a/compat/basename.c b/compat/basename.c
new file mode 100644
index 000000000..c9770229c
--- /dev/null
+++ b/compat/basename.c
@@ -0,0 +1,16 @@
+#include "compat.h"
+#include <string.h>
+
+const char *sparse_basename(const char *path)
+{
+ const char *last;
+
+ last = strrchr(path, '/');
+ path = last ? last + 1 : path;
+#if defined(_WIN32) || defined(_WIN64)
+ last = strrchr(path, '\\');
+ path = last ? last + 1 : path;
+#endif
+
+ return path;
+}
--
2.12.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH 3/3] add real support for __BASE_FILE__
2017-04-05 1:14 [PATCH 0/3] add real support for __BASE_FILE__ Luc Van Oostenryck
2017-04-05 1:14 ` [PATCH 1/3] build: add missing #include "compat.h" Luc Van Oostenryck
2017-04-05 1:14 ` [PATCH 2/3] add sparse_basename() Luc Van Oostenryck
@ 2017-04-05 1:14 ` Luc Van Oostenryck
2 siblings, 0 replies; 4+ messages in thread
From: Luc Van Oostenryck @ 2017-04-05 1:14 UTC (permalink / raw)
To: linux-sparse; +Cc: Christopher Li, Luc Van Oostenryck
There was some support of it but it was probably just
for sparse to not complain as it was a define that
expanded to the fixed name "base_file.c".
Fix this by letting it expand to the basename of the input
file.
Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
ident-list.h | 1 +
lib.c | 3 ---
pre-process.c | 4 ++++
validation/preprocessor/__BASE_FILE__.c | 11 +++++++++++
4 files changed, 16 insertions(+), 3 deletions(-)
create mode 100644 validation/preprocessor/__BASE_FILE__.c
diff --git a/ident-list.h b/ident-list.h
index 8cc66a50c..21564bc5b 100644
--- a/ident-list.h
+++ b/ident-list.h
@@ -104,6 +104,7 @@ __IDENT(pragma_ident, "__pragma__", 0);
__IDENT(__VA_ARGS___ident, "__VA_ARGS__", 0);
__IDENT(__LINE___ident, "__LINE__", 0);
__IDENT(__FILE___ident, "__FILE__", 0);
+__IDENT(__BASE_FILE___ident, "__BASE_FILE__", 0);
__IDENT(__DATE___ident, "__DATE__", 0);
__IDENT(__TIME___ident, "__TIME__", 0);
__IDENT(__func___ident, "__func__", 0);
diff --git a/lib.c b/lib.c
index 272d2c88a..0b4564f5a 100644
--- a/lib.c
+++ b/lib.c
@@ -1038,9 +1038,6 @@ void create_builtin_stream(void)
add_pre_buffer("#define __builtin_ms_va_end(arg)\n");
add_pre_buffer("#define __builtin_va_arg_pack()\n");
- /* FIXME! We need to do these as special magic macros at expansion time! */
- add_pre_buffer("#define __BASE_FILE__ \"base_file.c\"\n");
-
if (optimize)
add_pre_buffer("#define __OPTIMIZE__ 1\n");
if (optimize_size)
diff --git a/pre-process.c b/pre-process.c
index 7c57ba1cd..dd698c5bc 100644
--- a/pre-process.c
+++ b/pre-process.c
@@ -172,6 +172,10 @@ static int expand_one_symbol(struct token **list)
replace_with_integer(token, token->pos.line);
} else if (token->ident == &__FILE___ident) {
replace_with_string(token, stream_name(token->pos.stream));
+ } else if (token->ident == &__BASE_FILE___ident) {
+ const char *base = stream_name(token->pos.stream);
+ base = sparse_basename(base);
+ replace_with_string(token, base);
} else if (token->ident == &__DATE___ident) {
if (!t)
time(&t);
diff --git a/validation/preprocessor/__BASE_FILE__.c b/validation/preprocessor/__BASE_FILE__.c
new file mode 100644
index 000000000..d1cce9633
--- /dev/null
+++ b/validation/preprocessor/__BASE_FILE__.c
@@ -0,0 +1,11 @@
+__BASE_FILE__
+
+/*
+ * check-name: __BASE_FILE__
+ * check-command: sparse -E $file
+ *
+ * check-output-start
+
+"__BASE_FILE__.c"
+ * check-output-end
+ */
--
2.12.0
^ permalink raw reply related [flat|nested] 4+ messages in thread