-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmacro_table.c
106 lines (86 loc) · 2.88 KB
/
macro_table.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
* sparse/macro_table.c
*
* Copyright (C) 2010 Dan Carpenter.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "lib.h"
#include "parse.h"
#include "cwchash/hashtable.h"
static struct hashtable *macro_table;
static DEFINE_HASHTABLE_INSERT(do_insert_macro, struct position, struct string_list);
static DEFINE_HASHTABLE_SEARCH(do_search_macro, struct position, struct string_list);
static inline unsigned int position_hash(void *_pos)
{
struct position *pos = _pos;
return pos->line | (pos->pos << 22) | (pos->stream << 18);
}
static inline int equalkeys(void *_pos1, void *_pos2)
{
struct position *pos1 = _pos1;
struct position *pos2 = _pos2;
return pos1->line == pos2->line && pos1->pos == pos2->pos &&
pos1->stream == pos2->stream;
}
static void insert_macro_string(struct string_list **str_list, char *new)
{
char *tmp;
if (!new)
return;
FOR_EACH_PTR(*str_list, tmp) {
if (strcmp(tmp, new) == 0)
return;
} END_FOR_EACH_PTR(tmp);
add_ptr_list(str_list, new);
}
void store_macro_pos(struct token *token)
{
struct string_list *list;
if (!macro_table)
macro_table = create_hashtable(5000, position_hash, equalkeys);
list = do_search_macro(macro_table, &token->pos);
insert_macro_string(&list, token->ident->name);
do_insert_macro(macro_table, &token->pos, list);
}
char *get_macro_name(struct position pos)
{
struct string_list *list;
if (!macro_table)
return NULL;
list = do_search_macro(macro_table, &pos);
return first_ptr_list((struct ptr_list *)list);
}
char *get_inner_macro(struct position pos)
{
struct string_list *list;
if (!macro_table)
return NULL;
list = do_search_macro(macro_table, &pos);
return last_ptr_list((struct ptr_list *)list);
}
struct string_list *get_all_macros(struct position pos)
{
if (!macro_table)
return NULL;
return do_search_macro(macro_table, &pos);
}