Skip to content

Commit 94f03dc

Browse files
committed
drop symbol name lookup code
1 parent 4e41950 commit 94f03dc

File tree

10 files changed

+139
-293
lines changed

10 files changed

+139
-293
lines changed

src/data_external.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,17 +136,17 @@ void gendlopen::load_template(templates::name file)
136136
switch (file)
137137
{
138138
/* never add line directive to license part */
139-
CASE_X(license, "license.h", false);
139+
CASE_X(license, "license.h", false);
140140
CASE_X(filename_macros, "filename_macros.h", b);
141-
CASE_X(common_header, "common.h", b);
142-
CASE_X(c_header, "c.h", b);
143-
CASE_X(c_body, "c.c", b);
144-
CASE_X(cxx_header, "cxx.hpp", b);
145-
CASE_X(cxx_body, "cxx.cpp", b);
146-
CASE_X(min_c_header, "minimal.h", b);
141+
CASE_X(common_header, "common.h", b);
142+
CASE_X(c_header, "c.h", b);
143+
CASE_X(c_body, "c.c", b);
144+
CASE_X(cxx_header, "cxx.hpp", b);
145+
CASE_X(cxx_body, "cxx.cpp", b);
146+
CASE_X(min_c_header, "minimal.h", b);
147147
CASE_X(min_cxx_header, "minimal_cxxeh.hpp", b);
148-
CASE_X(plugin_header, "plugin.h", b);
149-
CASE_X(plugin_body, "plugin.c", b);
148+
CASE_X(plugin_header, "plugin.h", b);
149+
CASE_X(plugin_body, "plugin.c", b);
150150
}
151151
}
152152

src/gendlopen.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,6 @@ void gendlopen::process(const int &argc, char ** const &argv)
249249

250250
if (print_symbols()) {
251251
print_symbols_to_stdout(m_typedefs, m_prototypes, m_objects);
252-
} else if (print_lookup()) {
253-
save::symbol_name_lookup(m_pfx_upper, m_prototypes, m_objects);
254252
} else if (!custom_template().empty()) {
255253
process_custom_template();
256254
} else {

src/gendlopen.hpp

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,6 @@ namespace save
4949

5050
/* open output file stream for writing */
5151
void open_ofstream(const std::filesystem::path &opath, bool force);
52-
53-
/* creates the GDO_CHECK_SYMBOL_NAME() macro and saves it to save::ofs */
54-
size_t symbol_name_lookup(const std::string &pfx_upper, const vproto_t &v_prototypes, const vproto_t &v_objects);
5552
}
5653

5754

@@ -150,22 +147,21 @@ class gendlopen
150147
/* get() / set() an option */
151148

152149
/* type method name default value */
153-
OPT( std::string, input, {} )
154-
OPT( std::string, output, "-" )
155-
OPT( output::format, format, output::c )
156-
OPT( param::names, parameter_names, param::read )
157-
OPT( std::string, custom_template, {} )
150+
OPT( std::string, input, {} )
151+
OPT( std::string, output, "-" )
152+
OPT( output::format, format, output::c )
153+
OPT( param::names, parameter_names, param::read )
154+
OPT( std::string, custom_template, {} )
158155
OPT( std::string, templates_path, DEFAULT_TEMPLATES_PATH )
159-
OPT( std::string, default_lib, {} )
160-
OPT( bool, force, false )
161-
OPT( bool, separate, false )
162-
OPT( bool, ast_all_symbols, false )
163-
OPT( bool, print_symbols, false )
164-
OPT( bool, print_lookup, false )
165-
OPT( bool, read_options, true )
166-
OPT( bool, print_date, true )
167-
OPT( bool, line_directive, false )
168-
OPT( bool, pragma_once, true )
156+
OPT( std::string, default_lib, {} )
157+
OPT( bool, force, false )
158+
OPT( bool, separate, false )
159+
OPT( bool, ast_all_symbols, false )
160+
OPT( bool, print_symbols, false )
161+
OPT( bool, read_options, true )
162+
OPT( bool, print_date, true )
163+
OPT( bool, line_directive, false )
164+
OPT( bool, pragma_once, true )
169165

170166
void add_pfx(const std::string &s) { m_prefix_list.push_back(s); }
171167
void add_sym(const std::string &s) { m_symbol_list.push_back(s); }

src/generate.cpp

Lines changed: 61 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,62 @@ namespace /* anonymous */
117117

118118
#endif /* __MINGW32__ */
119119

120+
121+
/**
122+
* Look for a common symbol prefix.
123+
* Many APIs share a common prefix among their symbols.
124+
* If you want to load a specific symbol we can use this
125+
* later for a faster lookup.
126+
*/
127+
std::string get_common_prefix(vproto_t &v_prototypes, vproto_t &v_objects)
128+
{
129+
vstring_t vec;
130+
size_t n;
131+
132+
/* copy symbol names */
133+
for (const auto &e : v_prototypes) {
134+
vec.push_back(e.symbol);
135+
}
136+
137+
for (const auto &e : v_objects) {
138+
vec.push_back(e.symbol);
139+
}
140+
141+
/* need at least 2 symbols */
142+
if (vec.size() < 2) {
143+
return {};
144+
}
145+
146+
/* get shortest symbol length */
147+
size_t shortest_sym_len = vec.front().size();
148+
149+
/* skip first entry */
150+
for (auto it = vec.begin() + 1; it != vec.end(); it++) {
151+
/* prevent `min()' macro expansion from Windows headers */
152+
shortest_sym_len = std::min<size_t>(shortest_sym_len, (*it).size());
153+
}
154+
155+
/* compare each letter of every entry */
156+
for (n = 0; n < shortest_sym_len; n++) {
157+
/* skip first entry */
158+
for (auto it = vec.begin() + 1; it != vec.end(); it++) {
159+
if ((*it).empty()) {
160+
return {};
161+
}
162+
163+
/* compare against first entry */
164+
if ((*it).at(n) != vec.front().at(n)) {
165+
/* common prefix found (can be empty) */
166+
return vec.at(0).substr(0, n);
167+
}
168+
}
169+
}
170+
171+
/* shortest symbol name equals prefix, i.e. if a symbol `foo'
172+
* and `foobar' exist the prefix is `foo' */
173+
return vec.at(0).substr(0, n);
174+
}
175+
120176
} /* end anonymous namespace */
121177

122178

@@ -409,16 +465,6 @@ void gendlopen::generate()
409465
bool is_cxx = false;
410466
size_t lines = 0;
411467

412-
/* whether to generate a symbol lookup macro and
413-
* where to save it */
414-
enum {
415-
GEN_MACRO_NONE,
416-
GEN_MACRO_HEADER,
417-
GEN_MACRO_BODY
418-
};
419-
420-
int gen_macro = GEN_MACRO_NONE;
421-
422468
/************* lambda functions *************/
423469

424470
auto lf_print_lineno = [&, this] () {
@@ -465,11 +511,6 @@ void gendlopen::generate()
465511
lines += save::extra_defines(m_defines);
466512
lines += save::includes(m_includes, is_cxx);
467513
lines += save::typedefs(m_typedefs);
468-
469-
/* save macro in header */
470-
if (gen_macro == GEN_MACRO_HEADER) {
471-
lines += save::symbol_name_lookup(m_pfx_upper, m_prototypes, m_objects);
472-
}
473514
};
474515

475516
auto lf_header_template_data = [&] () {
@@ -488,11 +529,6 @@ void gendlopen::generate()
488529
save::ofs << "#define " << m_pfx_upper << "_INCLUDED_IN_BODY\n";
489530
save::ofs << "#include \"" << header_name << "\"\n";
490531
save::ofs << '\n';
491-
492-
/* save macro in body */
493-
if (gen_macro == GEN_MACRO_BODY) {
494-
save::symbol_name_lookup(m_pfx_upper, m_prototypes, m_objects);
495-
}
496532
};
497533

498534
auto lf_body_template_data = [&] () {
@@ -516,11 +552,10 @@ void gendlopen::generate()
516552
switch (m_format)
517553
{
518554
case output::c:
519-
gen_macro = m_separate ? GEN_MACRO_BODY : GEN_MACRO_HEADER;
555+
case output::plugin:
520556
break;
521557

522558
case output::cxx:
523-
gen_macro = GEN_MACRO_HEADER;
524559
is_cxx = true;
525560
break;
526561

@@ -533,9 +568,6 @@ void gendlopen::generate()
533568
is_cxx = true;
534569
break;
535570

536-
case output::plugin:
537-
break;
538-
539571
[[unlikely]] case output::error:
540572
throw error(std::string(__func__) + ": m_format == output::error");
541573
}
@@ -567,6 +599,10 @@ void gendlopen::generate()
567599
m_defines += "#define " + m_pfx_upper + "_HARDCODED_DEFAULT_LIBW " + lib_w + '\n';
568600
}
569601

602+
/* common symbol prefix (can be empty) */
603+
m_defines += "#define " + m_pfx_upper + "_COMMON_PREFIX "
604+
"\"" + get_common_prefix(m_prototypes, m_objects) + "\"\n";
605+
570606
/* create template data */
571607
create_template_lists(header_data, body_data);
572608

src/help.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ namespace help
8888
" modes are: read (default), skip, create\n"
8989
" -ast-all-symbols use all symbols from a Clang AST (`-P' and `-S' are ignored)\n"
9090
" -print-symbols print list of found symbols and exit\n"
91-
" -print-lookup print a C lookup code macro generated from the found symbols and exit\n"
9291
" -ignore-options ignore `%option' lines from input file\n"
9392
" -no-date don't show current date in output\n"
9493
" -no-pragma-once use `#ifndef' header guard instead of `#pragma once'\n"
@@ -310,11 +309,6 @@ namespace help
310309
"\n"
311310

312311

313-
" -print-lookup\n"
314-
" Print a C lookup code macro generated from the found symbols and exit.\n"
315-
"\n"
316-
317-
318312
" -ignore-options\n"
319313
" Ignore lines beginning with `%option' from the input file.\n"
320314
"\n"

src/meson.build

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ gendlopen_src = files(
1616
'parameter_names.cpp',
1717
'parse.cpp',
1818
'substitute.cpp',
19-
'symbol_name_lookup.cpp',
2019
'tokenize.cpp',
2120
'utils.cpp'
2221
)

src/options.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,6 @@ void gendlopen::parse_cmdline(const int &argc, char ** const &argv)
226226
} else if (a.get_noarg("print-symbols")) {
227227
print_symbols(true);
228228
continue;
229-
} else if (a.get_noarg("print-lookup")) {
230-
print_lookup(true);
231-
continue;
232229
}
233230
break;
234231

0 commit comments

Comments
 (0)