Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Understanding code that GCC compiles (http://www.go4expert.com/forums/understanding-code-gcc-compiles-t24644/)

shpric002 21Jan2011 19:34

Understanding code that GCC compiles
 
Hi,

When I started coding things where fairly simple, you create variables, write some instructions, and all works fine.

After playing around with some Linux Kernel drivers, there are some odd extensions to gcc that are not "normal". Does anyone know how this works:

static const char * const pm_tests[__TEST_AFTER_LAST] = {
[TEST_NONE] = "none",
[TEST_CORE] = "core",
[TEST_CPUS] = "processors",
[TEST_PLATFORM] = "platform",
[TEST_DEVICES] = "devices",
[TEST_FREEZER] = "freezer",
};

What do the things in "[...]" do?

Thanks,
Richard

poornaMoksha 31Jan2011 12:05

Re: Understanding code that GCC compiles
 
Can you provide some more information about your code...like are these 'TEST_NONE' etc macros, if yes then what are the values of all these macros??

shpric002 31Jan2011 12:12

Re: Understanding code that GCC compiles
 
Hi,

The code comes from:
kernel/power/main.c#L76

The [...] are found in an enum in:
kernel/power/power.h#L216

This is from Linux Kernel v2.6.37.


Thanks.

xpi0t0s 1Feb2011 20:04

Re: Understanding code that GCC compiles
 
Yep, but what are the values of those macros? As in: look them up. You need to learn how to find your way around code if you want to be a programmer. If you don't know how to find out, RTFM for "grep".

shpric002 1Feb2011 21:18

Re: Understanding code that GCC compiles
 
Gee what an answer.....

I wrote "enum". Gee I wonder what that means....I guess it means they are enums....but I'm obviously not as good as you to figure out that it is a simple macro going from zero to max value. I should really make an effort to write something like "The [...] are found in an enum in" and post a better link like "here is file link you can find", etc. But, hey, if I could only use something like a Linux Kernel cross reference site I could find out where in a file they are and list it here and tell the file and line like "kernel/power/power.h#L216". If only I could find my way around code like that....

But that's ok, obviously, posting useless information like that doesn't cut the mustard here.

So if you can't find your way around the code, I'll copy paste some nice GPL code for you:
static const char * const pm_tests[__TEST_AFTER_LAST] = {
[TEST_NONE] = "none",
[TEST_CORE] = "core",
[TEST_CPUS] = "processors",
[TEST_PLATFORM] = "platform",
[TEST_DEVICES] = "devices",
[TEST_FREEZER] = "freezer",
};

enum {
/* keep first */
TEST_NONE,
TEST_CORE,
TEST_CPUS,
TEST_PLATFORM,
TEST_DEVICES,
TEST_FREEZER,
/* keep last */
__TEST_AFTER_LAST
};

And just for you:

TEST_NONE = 0
TEST_CORE = 1
TEST_CPUS = 2
TEST_PLATFORM = 3
TEST_DEVICES = 4
TEST_FREEZER = 5
__TEST_AFTER_LAST = 6

And:
static const char * const pm_tests[6] = {
[0] = "none",
[1] = "core",
[2] = "processors",
[3] = "platform",
[4] = "devices",
[5] = "freezer",
};

I wonder how I got that if I cannot find my way around code.........

xpi0t0s 1Feb2011 22:45

Re: Understanding code that GCC compiles
 
It's not any syntax I've seen before. Seems equivalent to
Code:

static const char * const pm_tests[6] = {"none", "core", etc };
You may well have looked all that stuff up behind the scenes, but if you don't say then I have no way of knowing. Have a look yourself at the sparse info you posted and see if you can draw any conclusion other than a n00b completely out of his depth.

Yes, of course I know what enums are, but it's a non-standard extension, so possibly more of it could be non-standard as well. I only know standard C++.

No need for the attitude really. I'm just trying to be helpful, and IME it's more helpful to show someone how to get the answers than just to give them those answers.


All times are GMT +5.5. The time now is 19:46.