Jump to content

Template:GNU C Compiler Internals/GCC Hacks

From Wikibooks, open books for an open world

toString() method for each structure as in Java

[edit source]

Invoke a block of code from a function as in Ruby

[edit source]

Linux implementation of lists allows to invoke a block of code on each element of the list:

pagelist.c:

       list_for_each_prev(pos, head) {
                struct nfs_page *p = nfs_list_entry(pos);
                if (page_index(p->wb_page) < pg_idx)
                        break;
       }

list_for_each_prev takes the code in the brackets as a parameter. The trick is to use a macro that rolls out to a for() loop whose body becomes the code in the brackets. The goal of this project is to allow programmers to use code blocks in function calls.

Dereference function results when a structure is returned

[edit source]

C allows one to dereference the return value of a function if it is a pointer to a structure:

 get_struct()->field=0;

If the function returns the structure, not a pointer to it, then a compile-time error is generated:

 get_struct().field=0;
 > request for member `field' in something not a structure or union

This extension addresses the problem of dereferencing structures that are return values.

Use functions to initialize a variable

[edit source]

When a variable is defined and initialized the initializer is constant. You will get an error if you try to use a function, no matter what this function is:

 int getint() { return 1; }
 int i=getint();
 > initializer element is not constant

When a variable is used the function it was initialized with is called.

Default values of function arguments as in C++

[edit source]
 void func(int a=0) {
   printf("a=%d\n", a);
 }
 int main() {
   func();
 }
 > syntax error before '=' token

Reference parameters as in C++

[edit source]
 void test(int &a, int &b);
 int x,y;
 test(x,y);


GCC switches in object file

[edit source]

GCC switches in object fileusage: /usr/local/bin/paster serve [options] CONFIG_FILE [start|stop|restart|status] Serve the described application

If start/stop/restart is given, then it will start (normal operation), stop (--stop-daemon), or do both. You probably want ``--daemon`` as well for stopping.

Options:

 -h, --help            show this help message and exit
 -v, --verbose
 -q, --quiet
 -nNAME, --app-name=NAME
                       Load the named application (default main)
 -sSERVER_TYPE, --server=SERVER_TYPE
                       Use the named server.
 --server-name=SECTION_NAME
                       Use the named server as defined in the configuration
                       file (default: main)
 --daemon              Run in daemon (background) mode
 --pid-file=FILENAME   Save PID to file (default to paster.pid if running in
                       daemon mode)
 --log-file=LOG_FILE   Save output to the given log file (redirects stdout)
 --reload              Use auto-restart file monitor
 --reload-interval=RELOAD_INTERVAL
                       Seconds between checking files (low number can cause
                       significant CPU usage)
 --status              Show the status of the (presumably daemonized) server
 --user=USERNAME       Set the user (usually only possible when run as root)
 --group=GROUP         Set the group (usually only possible when run as root)
 --stop-daemon         Stop a daemonized server (given a PID file, or default
                       paster.pid file)

[app:main] use = egg:PasteEnabledPackage option1 = foo option2 = bar

[server:main] use = egg:PasteScript#wsgiutils host = 127.0.0.1 port = 80 sudo update-rc.d startup.sh defaults

Type information at run-time

[edit source]

There is no type information in C language available at run-time. The idea is to allow program to get the names and offsets of a structure's field at run time, the symbolic name of an enum declaration, etc. For example, instead of writing

  enum tree_code code;
  ...
  switch (code) {
    case VAR_DECL:
      printf("VAR_DECL\n");
      break;
    case BLOCK:
      printf("BLOCK\n");
      break;
    ...
  }

one could write

  printf("%s\n", type_info(code).name);
Prev: [[GNU C Compiler Internals/Creating a Compiler Extension_{{{1}}}|Function Overloading in C]] [[GNU C Compiler Internals/GCC Hacks_{{{1}}}|GCC Hacks]] Next: [[GNU C Compiler Internals/Links_{{{1}}}|Links]]