aruu

[MIRROR] Actual replacement for Busybox | suckless
git clone https://git.daat.foo/aruu.git
Log | Files | Refs | README | LICENSE

attr.c (3911B)


      1 #include <limits.h>
      2 #include <stdbool.h>
      3 #include <string.h>
      4 #include "util.h"
      5 #include "cc.h"
      6 
      7 enum attrprefix {
      8   PREFIXNONE = 1, /* standard attribute */
      9   PREFIXGNU,
     10 };
     11 
     12 static const char *
     13 strip(const char *name, char *buf, size_t buflen)
     14 {
     15   size_t len;
     16 
     17   len = strlen(name);
     18   if (len >= 4 && name[0] == '_' && name[1] == '_' && name[len - 2] == '_'
     19       && name[len - 1] == '_') {
     20     if (len - 4 >= buflen)
     21       return "";
     22     memcpy(buf, name + 2, len - 4);
     23     buf[len - 4] = '\0';
     24     return buf;
     25   }
     26   return name;
     27 }
     28 
     29 static bool
     30 parseattr(struct attr *a, enum attrkind allowed, enum attrprefix prefix)
     31 {
     32   const char   *name, *prefixname = "";
     33   char          namebuf[32], *section;
     34   enum attrkind kind;
     35   int           paren;
     36 
     37   if (tok.kind < TIDENT)
     38     return false;
     39   name = strip(tokenstr(tok.kind), namebuf, sizeof namebuf);
     40   next();
     41   if (!prefix) {
     42     if (consume(TCOLONCOLON)) {
     43       if (strcmp(name, "gnu") == 0)
     44         prefix = PREFIXGNU;
     45       name = strip(expect(TIDENT, "after attribute prefix"), namebuf, sizeof namebuf);
     46     } else {
     47       prefix = PREFIXNONE;
     48     }
     49   }
     50   kind = 0;
     51   switch (prefix) {
     52     case PREFIXNONE:
     53       if (strcmp(name, "noreturn") == 0 || strcmp(name, "_Noreturn") == 0)
     54         kind = ATTRNORETURN;
     55       break;
     56     case PREFIXGNU:
     57       prefixname = "GNU ";
     58       if (strcmp(name, "aligned") == 0) {
     59         kind = ATTRALIGNED;
     60         if (consume(TLPAREN)) {
     61           unsigned long long i;
     62 
     63           i = intconstexpr(&filescope, false);
     64           if (!i || i & i - 1 || i > INT_MAX)
     65             error(&tok.loc, "invalid alignment %llu", i);
     66           if (a)
     67             a->align = i;
     68           expect(TRPAREN, "after alignment");
     69         } else {
     70           if (a)
     71             a->align = 16;
     72         }
     73       } else if (strcmp(name, "constructor") == 0) {
     74         kind = ATTRCONSTRUCTOR;
     75       } else if (strcmp(name, "destructor") == 0) {
     76         kind = ATTRDESTRUCTOR;
     77       } else if (strcmp(name, "packed") == 0) {
     78         kind = ATTRPACKED;
     79       } else if (strcmp(name, "section") == 0) {
     80         kind = ATTRSECTION;
     81         expect(TLPAREN, "after 'section'");
     82         section = expect(TSTRINGLIT, "for section name");
     83         if (a)
     84           a->section = section;
     85         expect(TRPAREN, "after section name");
     86       }
     87       break;
     88   }
     89   if (kind) {
     90     if (!(kind & allowed))
     91       error(&tok.loc, "%sattribute '%s' is not supported here", prefixname, name);
     92     if (a)
     93       a->kind |= kind;
     94   } else if (consume(TLPAREN)) {
     95     /* skip arguments */
     96     for (paren = 1; paren > 0; next()) {
     97       switch (tok.kind) {
     98         case TLPAREN:
     99           ++paren;
    100           break;
    101         case TRPAREN:
    102           --paren;
    103           break;
    104         default:
    105           break;
    106       }
    107     }
    108   }
    109   return true;
    110 }
    111 
    112 static bool
    113 attrspec(struct attr *a, enum attrkind allowed)
    114 {
    115   if (tok.kind != TLBRACK || !peek(TLBRACK))
    116     return false;
    117   while (parseattr(a, allowed, 0) || consume(TCOMMA))
    118     ;
    119   expect(TRBRACK, "to end attribute specifier");
    120   expect(TRBRACK, "to end attribute specifier");
    121   return true;
    122 }
    123 
    124 bool
    125 attr(struct attr *a, enum attrkind allowed)
    126 {
    127   if (!attrspec(a, allowed))
    128     return false;
    129   while (attrspec(a, allowed))
    130     ;
    131   return true;
    132 }
    133 
    134 static bool
    135 gnuattrspec(struct attr *a, enum attrkind allowed)
    136 {
    137   if (!consume(T__ATTRIBUTE__))
    138     return false;
    139   while (parseattr(a, allowed, PREFIXGNU) || consume(TCOMMA))
    140     ;
    141   expect(TLPAREN, "after '__attribute__' to begin attribute specifier");
    142   expect(TLPAREN, "after '__attribute__' to begin attribute specifier");
    143   while (parseattr(a, allowed, PREFIXGNU) || consume(TCOMMA))
    144     ;
    145   expect(TRPAREN, "to end attribute specifier");
    146   expect(TRPAREN, "to end attribute specifier");
    147   return true;
    148 }
    149 
    150 bool
    151 gnuattr(struct attr *a, enum attrkind allowed)
    152 {
    153   if (!gnuattrspec(a, allowed))
    154     return false;
    155   while (gnuattrspec(a, allowed))
    156     ;
    157   return true;
    158 }