linux-sparse.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH][RFC] handle -I and -include combination
@ 2012-06-18 12:40 kosaki.motohiro
  2012-06-26  7:52 ` Christopher Li
  0 siblings, 1 reply; 5+ messages in thread
From: kosaki.motohiro @ 2012-06-18 12:40 UTC (permalink / raw)
  To: linux-sparse; +Cc: KOSAKI Motohiro

From: KOSAKI Motohiro <kosaki.motohiro@gmail.com>

Currently, "-include" behavior is far different between sparse
and gcc. gcc can compile following testcase. but sparse can't.

This patch fixes it.

===================================================
Makefile
--------------
bar:
    $(CC) -I./include-for-foo -include foo.h main.c -o bar

main.c
------------------
 #include <stdio.h>
int main(void)
{
    printf("foo = %d\n", FOO);
    return 0;
}

include-for-foo/foo.h
----------------------
 #define FOO 42
===================================================

Signed-off-by: KOSAKI Motohiro <kosaki.motohiro@gmail.com>
---
 lib.c |   53 +++++++++++++++++++++++++----------------------------
 1 files changed, 25 insertions(+), 28 deletions(-)

diff --git a/lib.c b/lib.c
index 396e9f1..958f65e 100644
--- a/lib.c
+++ b/lib.c
@@ -189,6 +189,8 @@ void die(const char *fmt, ...)
 
 static struct token *pre_buffer_begin = NULL;
 static struct token *pre_buffer_end = NULL;
+static struct token *pre_buffer_begin2 = NULL;
+static struct token *pre_buffer_end2 = NULL;
 
 int Waddress_space = 1;
 int Wbitwise = 0;
@@ -224,11 +226,6 @@ static enum { STANDARD_C89,
               STANDARD_GNU89,
               STANDARD_GNU99, } standard = STANDARD_GNU89;
 
-#define CMDLINE_INCLUDE 20
-int cmdline_include_nr = 0;
-struct cmdline_include cmdline_include[CMDLINE_INCLUDE];
-
-
 void add_pre_buffer(const char *fmt, ...)
 {
 	va_list args;
@@ -247,6 +244,25 @@ void add_pre_buffer(const char *fmt, ...)
 	pre_buffer_end = end;
 }
 
+void add_pre_buffer2(const char *fmt, ...)
+{
+	va_list args;
+	unsigned int size;
+	struct token *begin, *end;
+	char buffer[4096];
+
+	va_start(args, fmt);
+	size = vsnprintf(buffer, sizeof(buffer), fmt, args);
+	va_end(args);
+	begin = tokenize_buffer(buffer, size, &end);
+	if (!pre_buffer_begin2)
+		pre_buffer_begin2 = begin;
+	if (pre_buffer_end2)
+		pre_buffer_end2->next = begin;
+	pre_buffer_end2 = end;
+}
+
+
 static char **handle_switch_D(char *arg, char **next)
 {
 	const char *name = arg + 1;
@@ -299,16 +315,7 @@ static char **handle_switch_I(char *arg, char **next)
 
 static void add_cmdline_include(char *filename)
 {
-	int fd = open(filename, O_RDONLY);
-	if (fd < 0) {
-		perror(filename);
-		return;
-	}
-	if (cmdline_include_nr >= CMDLINE_INCLUDE)
-		die("too many include files for %s\n", filename);
-	cmdline_include[cmdline_include_nr].filename = filename;
-	cmdline_include[cmdline_include_nr].fd = fd;
-	cmdline_include_nr++;
+	add_pre_buffer2("#include \"%s\"\n", filename);
 }
 
 static char **handle_switch_i(char *arg, char **next)
@@ -892,19 +899,9 @@ static struct symbol_list *sparse_file(const char *filename)
  */
 static struct symbol_list *sparse_initial(void)
 {
-	struct token *token;
-	int i;
-
-	// Prepend any "include" file to the stream.
-	// We're in global scope, it will affect all files!
-	token = NULL;
-	for (i = cmdline_include_nr - 1; i >= 0; i--)
-		token = tokenize(cmdline_include[i].filename, cmdline_include[i].fd,
-				 token, includepath);

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH][RFC] handle -I and -include combination
  2012-06-18 12:40 [PATCH][RFC] handle -I and -include combination kosaki.motohiro
@ 2012-06-26  7:52 ` Christopher Li
  2012-06-27 17:55   ` KOSAKI Motohiro
  0 siblings, 1 reply; 5+ messages in thread
From: Christopher Li @ 2012-06-26  7:52 UTC (permalink / raw)
  To: kosaki.motohiro; +Cc: linux-sparse

On Mon, Jun 18, 2012 at 5:40 AM,  <kosaki.motohiro@gmail.com> wrote:
> From: KOSAKI Motohiro <kosaki.motohiro@gmail.com>
>
> Currently, "-include" behavior is far different between sparse
> and gcc. gcc can compile following testcase. but sparse can't.
>
> This patch fixes it.
>

Thanks for the patch. The patch has some small problem though:
There are some other user of the cmdline_include array.
Remove it will disable those options.

Also the include order of the header file is now reversed:

$  gcc -include a.h -include b.h  foo.c
will include a.h then b.h.
Your patch will include b.h then a.h.
Granted, the "isystem" has the same bug here too.

Chris

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH][RFC] handle -I and -include combination
  2012-06-26  7:52 ` Christopher Li
@ 2012-06-27 17:55   ` KOSAKI Motohiro
  2012-06-30  0:27     ` Christopher Li
  0 siblings, 1 reply; 5+ messages in thread
From: KOSAKI Motohiro @ 2012-06-27 17:55 UTC (permalink / raw)
  To: Christopher Li; +Cc: linux-sparse

Hi,

Thanks for reviewing.

On Tue, Jun 26, 2012 at 3:52 AM, Christopher Li <sparse@chrisli.org> wrote:
> On Mon, Jun 18, 2012 at 5:40 AM,  <kosaki.motohiro@gmail.com> wrote:
>> From: KOSAKI Motohiro <kosaki.motohiro@gmail.com>
>>
>> Currently, "-include" behavior is far different between sparse
>> and gcc. gcc can compile following testcase. but sparse can't.
>>
>> This patch fixes it.
>>
>
> Thanks for the patch. The patch has some small problem though:
> There are some other user of the cmdline_include array.
> Remove it will disable those options.

If I understand correctly, cmdline_include is only used from -include
and -imacro. and, -imacro is also needed to respect -I. Am I missing
something?

I'm now seeing  git://git.kernel.org/pub/scm/devel/sparse/sparse.git.


> Also the include order of the header file is now reversed:
>
> $  gcc -include a.h -include b.h  foo.c
> will include a.h then b.h.
> Your patch will include b.h then a.h.

Sorry, I can't reproduce this.

a.h
----------------
#ifdef FOO
#error a.h
#endif
#define FOO 'a'

b.h
----------------
#ifdef FOO
#error b.h
#endif
#define FOO 'b'

main.c
-------------
int main(void)
{
	printf("foo = %d\n", FOO);
	return 0;
}

result
---------------------
b.h:2:2: error: b.h

> Granted, the "isystem" has the same bug here too.
>
> Chris
--
To unsubscribe from this list: send the line "unsubscribe linux-sparse" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH][RFC] handle -I and -include combination
  2012-06-27 17:55   ` KOSAKI Motohiro
@ 2012-06-30  0:27     ` Christopher Li
  2012-06-30  7:38       ` KOSAKI Motohiro
  0 siblings, 1 reply; 5+ messages in thread
From: Christopher Li @ 2012-06-30  0:27 UTC (permalink / raw)
  To: KOSAKI Motohiro; +Cc: linux-sparse

On Wed, Jun 27, 2012 at 10:55 AM, KOSAKI Motohiro
<kosaki.motohiro@gmail.com> wrote:
> If I understand correctly, cmdline_include is only used from -include
> and -imacro. and, -imacro is also needed to respect -I. Am I missing
> something?

Actually, I misread your patch. You patch function fine.
The only thing I might change is the name of the "pre_buffer2".
If this buffer is only use by the include files, we might just all it
include_buffer or some thing like that. Let me know if you want to
resend one with a better name, or I can patch it for you.

Thanks for the patch. That will get rid of the static array as well.
Nice.

Chris

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH][RFC] handle -I and -include combination
  2012-06-30  0:27     ` Christopher Li
@ 2012-06-30  7:38       ` KOSAKI Motohiro
  0 siblings, 0 replies; 5+ messages in thread
From: KOSAKI Motohiro @ 2012-06-30  7:38 UTC (permalink / raw)
  To: Christopher Li; +Cc: KOSAKI Motohiro, linux-sparse

(6/29/12 8:27 PM), Christopher Li wrote:
> On Wed, Jun 27, 2012 at 10:55 AM, KOSAKI Motohiro
> <kosaki.motohiro@gmail.com> wrote:
>> If I understand correctly, cmdline_include is only used from -include
>> and -imacro. and, -imacro is also needed to respect -I. Am I missing
>> something?
> 
> Actually, I misread your patch. You patch function fine.
> The only thing I might change is the name of the "pre_buffer2".
> If this buffer is only use by the include files, we might just all it
> include_buffer or some thing like that. Let me know if you want to
> resend one with a better name, or I can patch it for you.
> 
> Thanks for the patch. That will get rid of the static array as well.
> Nice.


Thanks. done.


From e4f94099b1f01910cf2de10667c12cdf872a6522 Mon Sep 17 00:00:00 2001
From: KOSAKI Motohiro <kosaki.motohiro@gmail.com>
Date: Mon, 18 Jun 2012 08:04:26 -0400
Subject: [PATCH] handle -I and -include combination

Currently, "-include" behavior is far different between sparse
and gcc. gcc can compile following testcase. but sparse can't.

This patch fixes it.

===================================================
 Makefile
 --------------
 bar:
    $(CC) -I./include-for-foo -include foo.h main.c -o bar

 main.c
 ------------------
 #include <stdio.h>
 int main(void)
 {
     printf("foo = %d¥n", FOO);
     return 0;
 }

 include-for-foo/foo.h
 ----------------------
 #define FOO 42
===================================================

Signed-off-by: KOSAKI Motohiro <kosaki.motohiro@gmail.com>
---
 lib.c |   52 ++++++++++++++++++++++++----------------------------
 lib.h |    9 ---------
 2 files changed, 24 insertions(+), 37 deletions(-)

diff --git a/lib.c b/lib.c
index 396e9f1..c698fa1 100644
--- a/lib.c
+++ b/lib.c
@@ -189,6 +189,8 @@ void die(const char *fmt, ...)
 
 static struct token *pre_buffer_begin = NULL;
 static struct token *pre_buffer_end = NULL;
+static struct token *include_buffer_begin = NULL;
+static struct token *include_buffer_end = NULL;
 
 int Waddress_space = 1;
 int Wbitwise = 0;
@@ -224,11 +226,6 @@ static enum { STANDARD_C89,
               STANDARD_GNU89,
               STANDARD_GNU99, } standard = STANDARD_GNU89;
 
-#define CMDLINE_INCLUDE 20
-int cmdline_include_nr = 0;
-struct cmdline_include cmdline_include[CMDLINE_INCLUDE];
-
-
 void add_pre_buffer(const char *fmt, ...)
 {
 	va_list args;
@@ -247,6 +244,24 @@ void add_pre_buffer(const char *fmt, ...)
 	pre_buffer_end = end;
 }
 
+void add_include_buffer(const char *fmt, ...)
+{
+	va_list args;
+	unsigned int size;
+	struct token *begin, *end;
+	char buffer[4096];
+
+	va_start(args, fmt);
+	size = vsnprintf(buffer, sizeof(buffer), fmt, args);
+	va_end(args);
+	begin = tokenize_buffer(buffer, size, &end);
+	if (!include_buffer_begin)
+		include_buffer_begin = begin;
+	if (include_buffer_end)
+		include_buffer_end->next = begin;
+	include_buffer_end = end;
+}
+
 static char **handle_switch_D(char *arg, char **next)
 {
 	const char *name = arg + 1;
@@ -299,16 +314,7 @@ static char **handle_switch_I(char *arg, char **next)
 
 static void add_cmdline_include(char *filename)
 {
-	int fd = open(filename, O_RDONLY);
-	if (fd < 0) {
-		perror(filename);
-		return;
-	}
-	if (cmdline_include_nr >= CMDLINE_INCLUDE)
-		die("too many include files for %s¥n", filename);
-	cmdline_include[cmdline_include_nr].filename = filename;
-	cmdline_include[cmdline_include_nr].fd = fd;
-	cmdline_include_nr++;
+	add_include_buffer("#include ¥"%s¥"¥n", filename);
 }
 
 static char **handle_switch_i(char *arg, char **next)
@@ -892,19 +898,9 @@ static struct symbol_list *sparse_file(const char *filename)
  */
 static struct symbol_list *sparse_initial(void)
 {
-	struct token *token;
-	int i;
-
-	// Prepend any "include" file to the stream.
-	// We're in global scope, it will affect all files!
-	token = NULL;
-	for (i = cmdline_include_nr - 1; i >= 0; i--)
-		token = tokenize(cmdline_include[i].filename, cmdline_include[i].fd,
-				 token, includepath);
-
-	// Prepend the initial built-in stream
-	if (token)
-		pre_buffer_end->next = token;
+	if (include_buffer_begin)
+		pre_buffer_end->next = include_buffer_begin;
+
 	return sparse_tokenstream(pre_buffer_begin);
 }
 
diff --git a/lib.h b/lib.h
index 2cea252..ee954fe 100644
--- a/lib.h
+++ b/lib.h
@@ -41,15 +41,6 @@ struct position {
 		     noexpand:1;
 };
 
-struct cmdline_include {
-	char *filename;
-	int fd;
-};
-
-extern struct cmdline_include cmdline_include[];
-extern int cmdline_include_nr;
-

^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2012-06-30  7:38 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-06-18 12:40 [PATCH][RFC] handle -I and -include combination kosaki.motohiro
2012-06-26  7:52 ` Christopher Li
2012-06-27 17:55   ` KOSAKI Motohiro
2012-06-30  0:27     ` Christopher Li
2012-06-30  7:38       ` KOSAKI Motohiro

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).