simple support for __attribute__((alias("symbol")))
[cparser] / attribute.c
index a29299a..8059435 100644 (file)
@@ -295,6 +295,38 @@ static void handle_attribute_asm(const attribute_t *attribute,
        return;
 }
 
+static void handle_attribute_alias(const attribute_t *attribute,
+                                  entity_t *entity)
+{
+       // TODO: return if not at function_scope
+       attribute_argument_t *arg = attribute->a.arguments;
+       if (arg == NULL) {
+               errorf(&attribute->pos,
+                      "__attribute__((alias(X))) misses argument");
+               return;
+       }
+       const char *string = get_argument_string(arg);
+       if (string == NULL) {
+               errorf(&attribute->pos,
+                      "__attribute__((alias(X))) argument is not a string");
+               return;
+       }
+       symbol_t *symbol = symbol_table_insert(string);
+
+       switch (entity->kind) {
+       case ENTITY_VARIABLE:
+               entity->variable.alias = symbol;
+               break;
+       case ENTITY_FUNCTION:
+               entity->function.alias = symbol;
+               break;
+       default:
+               warningf(WARN_OTHER, &attribute->pos, "alias attribute on '%N' ignored", entity);
+               break;
+       }
+       return;
+}
+
 void handle_entity_attributes(const attribute_t *attributes, entity_t *entity)
 {
        if (entity->kind == ENTITY_TYPEDEF) {
@@ -338,6 +370,10 @@ void handle_entity_attributes(const attribute_t *attributes, entity_t *entity)
                case ATTRIBUTE_MS_RESTRICT:      modifiers |= DM_RESTRICT; break;
                case ATTRIBUTE_MS_NOALIAS:       modifiers |= DM_NOALIAS; break;
 
+               case ATTRIBUTE_GNU_ALIAS:
+                       handle_attribute_alias(attribute, entity);
+                       break;
+
                case ATTRIBUTE_GNU_PACKED:
                        handle_attribute_packed_e(attribute, entity);
                        break;