smatch.h: introduce sm_pedantic()
[smatch.git] / macro_table.c
blob251bd99f71c8e4f53f6cb4f99c6838dbb17eef84
1 /*
2 * sparse/macro_table.c
4 * Copyright (C) 2010 Dan Carpenter.
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include "lib.h"
29 #include "parse.h"
30 #include "cwchash/hashtable.h"
32 static struct hashtable *macro_table;
34 static DEFINE_HASHTABLE_INSERT(do_insert_macro, struct position, struct string_list);
35 static DEFINE_HASHTABLE_SEARCH(do_search_macro, struct position, struct string_list);
37 static inline unsigned int position_hash(void *_pos)
39 struct position *pos = _pos;
41 return pos->line | (pos->pos << 22) | (pos->stream << 18);
44 static inline int equalkeys(void *_pos1, void *_pos2)
46 struct position *pos1 = _pos1;
47 struct position *pos2 = _pos2;
49 return pos1->line == pos2->line && pos1->pos == pos2->pos &&
50 pos1->stream == pos2->stream;
53 static void insert_macro_string(struct string_list **str_list, char *new)
55 add_ptr_list(str_list, new);
58 void store_macro_pos(struct token *token)
60 struct string_list *list;
62 if (!macro_table)
63 macro_table = create_hashtable(5000, position_hash, equalkeys);
65 list = do_search_macro(macro_table, &token->pos);
66 insert_macro_string(&list, token->ident->name);
68 do_insert_macro(macro_table, &token->pos, list);
71 char *get_macro_name(struct position pos)
73 struct string_list *list;
75 if (!macro_table)
76 return NULL;
77 list = do_search_macro(macro_table, &pos);
78 return first_ptr_list((struct ptr_list *)list);
81 char *get_inner_macro(struct position pos)
83 struct string_list *list;
85 if (!macro_table)
86 return NULL;
87 list = do_search_macro(macro_table, &pos);
88 return last_ptr_list((struct ptr_list *)list);
91 struct string_list *get_all_macros(struct position pos)
93 if (!macro_table)
94 return NULL;
95 return do_search_macro(macro_table, &pos);