1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Understanding code that GCC compiles

Discussion in 'C' started by shpric002, Jan 21, 2011.

  1. shpric002

    shpric002 New Member

    Joined:
    Jan 21, 2011
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  2. poornaMoksha

    poornaMoksha New Member

    Joined:
    Jan 29, 2011
    Messages:
    150
    Likes Received:
    33
    Trophy Points:
    0
    Occupation:
    Software developer
    Location:
    India
    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??
     
  3. shpric002

    shpric002 New Member

    Joined:
    Jan 21, 2011
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    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".
     
  5. shpric002

    shpric002 New Member

    Joined:
    Jan 21, 2011
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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.........
     
  6. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    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.
     
    shabbir likes this.

Share This Page