Use streq() instead of strcmp() == 0.
[cparser] / parser.c
index 09930b0..da35413 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -23,6 +23,7 @@
 #include <stdarg.h>
 #include <stdbool.h>
 
+#include "adt/strutil.h"
 #include "parser.h"
 #include "diagnostic.h"
 #include "format_check.h"
@@ -1071,22 +1072,6 @@ static string_t parse_string_literals(void)
        return result;
 }
 
-/**
- * compare two string, ignoring double underscores on the second.
- */
-static int strcmp_underscore(const char *s1, const char *s2)
-{
-       if (s2[0] == '_' && s2[1] == '_') {
-               size_t len2 = strlen(s2);
-               size_t len1 = strlen(s1);
-               if (len1 == len2-4 && s2[len2-2] == '_' && s2[len2-1] == '_') {
-                       return strncmp(s1, s2+2, len2-4);
-               }
-       }
-
-       return strcmp(s1, s2);
-}
-
 static attribute_t *allocate_attribute_zero(attribute_kind_t kind)
 {
        attribute_t *attribute = allocate_ast_zero(sizeof(*attribute));
@@ -1231,8 +1216,7 @@ static attribute_t *parse_attribute_gnu_single(void)
                }
 
                const char *attribute_name = get_attribute_name(kind);
-               if (attribute_name != NULL
-                               && strcmp_underscore(attribute_name, name) == 0)
+               if (attribute_name != NULL && streq_underscore(attribute_name, name))
                        break;
        }
 
@@ -1833,11 +1817,10 @@ static void descend_into_subtype(type_path_t *path)
        top->type              = top_type;
 
        if (is_type_compound(top_type)) {
-               compound_t *compound  = top_type->compound.compound;
-               entity_t   *entry     = compound->members.entities;
+               compound_t *const compound = top_type->compound.compound;
+               entity_t   *const entry    = skip_unnamed_bitfields(compound->members.entities);
 
                if (entry != NULL) {
-                       assert(entry->kind == ENTITY_COMPOUND_MEMBER);
                        top->v.compound_entry = &entry->declaration;
                        path->top_type = entry->declaration.type;
                } else {
@@ -1977,7 +1960,7 @@ static void advance_current_object(type_path_t *path, size_t top_path_level)
        } else if (is_type_struct(type)) {
                declaration_t *entry = top->v.compound_entry;
 
-               entity_t *next_entity = entry->base.next;
+               entity_t *const next_entity = skip_unnamed_bitfields(entry->base.next);
                if (next_entity != NULL) {
                        assert(is_declaration(next_entity));
                        entry = &next_entity->declaration;
@@ -2628,9 +2611,9 @@ static attribute_t *parse_attribute_ms_property(attribute_t *attribute)
 
                symbol_t **prop;
                symbol_t  *symbol = token.identifier.symbol;
-               if (strcmp(symbol->string, "put") == 0) {
+               if (streq(symbol->string, "put")) {
                        prop = &property->put_symbol;
-               } else if (strcmp(symbol->string, "get") == 0) {
+               } else if (streq(symbol->string, "get")) {
                        prop = &property->get_symbol;
                } else {
                        errorf(HERE, "expected put or get in property declspec");
@@ -2666,7 +2649,7 @@ static attribute_t *parse_microsoft_extended_decl_modifier_single(void)
                for (attribute_kind_t k = ATTRIBUTE_MS_FIRST; k <= ATTRIBUTE_MS_LAST;
                     ++k) {
                        const char *attribute_name = get_attribute_name(k);
-                       if (attribute_name != NULL && strcmp(attribute_name, name) == 0) {
+                       if (attribute_name != NULL && streq(attribute_name, name)) {
                                kind = k;
                                break;
                        }
@@ -3889,6 +3872,10 @@ static entity_t *parse_declarator(const declaration_specifiers_t *specifiers,
                handle_entity_attributes(attributes, entity);
        }
 
+       if (entity->kind == ENTITY_FUNCTION && !freestanding) {
+               adapt_special_functions(&entity->function);
+       }
+
        return entity;
 }
 
@@ -3979,7 +3966,7 @@ warn_arg_count:
  */
 static bool is_sym_main(const symbol_t *const sym)
 {
-       return strcmp(sym->string, "main") == 0;
+       return streq(sym->string, "main");
 }
 
 static void error_redefined_as_different_kind(const source_position_t *pos,
@@ -4105,8 +4092,22 @@ entity_t *record_entity(entity_t *entity, const bool is_definition)
                                goto finish;
                        }
                        if (previous_entity->kind == ENTITY_TYPEDEF) {
-                               /* TODO: C++ allows this for exactly the same type */
-                               errorf(pos, "redefinition of '%N' (declared %P)", entity, ppos);
+                               type_t *const type      = skip_typeref(entity->typedefe.type);
+                               type_t *const prev_type
+                                       = skip_typeref(previous_entity->typedefe.type);
+                               if (c_mode & _CXX) {
+                                       /* C++ allows double typedef if they are identical
+                                        * (after skipping typedefs) */
+                                       if (type == prev_type)
+                                               goto finish;
+                               } else {
+                                       /* GCC extension: redef in system headers is allowed */
+                                       if ((pos->is_system_header || ppos->is_system_header) &&
+                                           types_compatible(type, prev_type))
+                                               goto finish;
+                               }
+                               errorf(pos, "redefinition of '%N' (declared %P)",
+                                      entity, ppos);
                                goto finish;
                        }
 
@@ -4186,7 +4187,7 @@ warn_redundant_declaration: ;
                                                merge_in_attributes(decl, prev_decl->attributes);
                                        } else if (!is_definition        &&
                                                        is_type_valid(prev_type) &&
-                                                       strcmp(ppos->input_name, "<builtin>") != 0) {
+                                                       !pos->is_system_header) {
                                                warningf(WARN_REDUNDANT_DECLS, pos, "redundant declaration for '%Y' (declared %P)", symbol, ppos);
                                        }
                                } else if (current_function == NULL) {
@@ -10370,9 +10371,9 @@ static void parse_linkage_specification(void)
 
        linkage_kind_t old_linkage = current_linkage;
        linkage_kind_t new_linkage;
-       if (strcmp(linkage, "C") == 0) {
+       if (streq(linkage, "C")) {
                new_linkage = LINKAGE_C;
-       } else if (strcmp(linkage, "C++") == 0) {
+       } else if (streq(linkage, "C++")) {
                new_linkage = LINKAGE_CXX;
        } else {
                errorf(&pos, "linkage string \"%s\" not recognized", linkage);