aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJosh Triplett <josh@joshtriplett.org>2013-04-19 12:10:38 -0700
committerChristopher Li <sparse@chrisli.org>2013-04-21 20:55:10 -0700
commit9aadf85a29be2a74e46fc67b5313d41aee81dac7 (patch)
tree75a01db121c05159645cf68216db6b4f6f9b2506
parentf022d1c8b777af93ef25205cbc34cb6b64c698a7 (diff)
downloadsparse-9aadf85a29be2a74e46fc67b5313d41aee81dac7.tar.gz
Support #pragma once
"#pragma once" acts like a multiple-inclusion guard affecting the entire file, without an associated preprocessor symbol. This allows use of sparse on projects that rely on #pragma once without also using an ifndef-based multiple-inclusion guard, such as systemd; without this change, sparse will get into an include loop. Signed-off-by: Josh Triplett <josh@joshtriplett.org> Signed-off-by: Christopher Li <sparse@chrisli.org>
-rw-r--r--ident-list.h1
-rw-r--r--pre-process.c9
-rw-r--r--token.h2
-rw-r--r--validation/pragma-once.c5
4 files changed, 16 insertions, 1 deletions
diff --git a/ident-list.h b/ident-list.h
index e00cd964..e93aae76 100644
--- a/ident-list.h
+++ b/ident-list.h
@@ -97,6 +97,7 @@ IDENT(error); IDENT(__error__);
* itself by name, preventing these tokens from expanding when compiling
* sparse. */
IDENT(defined);
+IDENT(once);
__IDENT(pragma_ident, "__pragma__", 0);
__IDENT(__VA_ARGS___ident, "__VA_ARGS__", 0);
__IDENT(__LINE___ident, "__LINE__", 0);
diff --git a/pre-process.c b/pre-process.c
index 22ddf028..d5213186 100644
--- a/pre-process.c
+++ b/pre-process.c
@@ -745,6 +745,11 @@ static int already_tokenized(const char *path)
struct stream *s = input_streams + stream;
next = s->next_stream;
+ if (s->once) {
+ if (strcmp(path, s->name))
+ continue;
+ return 1;
+ }
if (s->constant != CONSTANT_FILE_YES)
continue;
if (strcmp(path, s->name))
@@ -1783,6 +1788,10 @@ static int handle_pragma(struct stream *stream, struct token **line, struct toke
{
struct token *next = *line;
+ if (match_ident(token->next, &once_ident) && eof_token(token->next->next)) {
+ stream->once = 1;
+ return 1;
+ }
token->ident = &pragma_ident;
token->pos.newline = 1;
token->pos.whitespace = 1;
diff --git a/token.h b/token.h
index b0d58dfb..401bf5eb 100644
--- a/token.h
+++ b/token.h
@@ -40,7 +40,7 @@ struct stream {
/* Use these to check for "already parsed" */
enum constantfile constant;
- int dirty, next_stream;
+ int dirty, next_stream, once;
struct ident *protect;
struct token *ifndef;
struct token *top_if;
diff --git a/validation/pragma-once.c b/validation/pragma-once.c
new file mode 100644
index 00000000..5e8b8254
--- /dev/null
+++ b/validation/pragma-once.c
@@ -0,0 +1,5 @@
+#pragma once
+#include "pragma-once.c"
+/*
+ * check-name: #pragma once
+ */