From 77a598ab7267d04f0b1fc0277e0314c3780313c0 Mon Sep 17 00:00:00 2001 From: Don Bright Date: Sun, 19 May 2013 18:04:51 -0500 Subject: throw warning when include file disappears. refactoring. diff --git a/src/lexer.l b/src/lexer.l index 29f3531..189a92b 100644 --- a/src/lexer.l +++ b/src/lexer.l @@ -113,31 +113,15 @@ use[ \t\r\n>]*"<" { BEGIN(cond_use); } [^\t\r\n>]+ { filename = yytext; } ">" { BEGIN(INITIAL); - fs::path fullpath = find_valid_path( sourcepath(), filename ); + fs::path fullpath = find_valid_path( sourcepath(), filename, openfilenames ); if ( fullpath.empty() ) { PRINTB("WARNING: Can't open 'use' file '%s'.", filename); } else { - handle_dep(usepath.string()); - parserlval.text = strdup(usepath.string().c_str()); + handle_dep(fullpath.string()); + parserlval.text = strdup(fullpath.string().c_str()); return TOK_USE; } -/* - if (boosty::is_absolute(fs::path(filename))) { - usepath = filename; - } - else { - usepath = sourcepath() / filename; - if (!fs::exists(usepath)) { - usepath = locate_file(filename); - } - } - // Only accept regular files which exists - if (usepath.has_parent_path() && fs::exists(usepath) && !fs::is_directory(usepath)) { - - } else { - } -*/ - } + } } <> { @@ -208,66 +192,43 @@ fs::path sourcepath() Rules for include 1) include 2) include + + Globals used: filepath, sourcepath, filename */ void includefile() { - PRINTB("includefile lex %s",filename.c_str()); - PRINTB("includefile filepath %s",filepath.c_str()); - PRINTB("includefile sourcepath %s",sourcepath.c_str()); - - if (filename.empty()) return; - - fs::path dirinfo = sourcepath(); - if (!filepath.empty()) { - if (boosty::is_absolute(fs::path(filepath))) { - dirinfo = filepath; - } - else { - dirinfo /= filepath; - } - } - - fs::path finfo = dirinfo / filename; - PRINTB("dirinfo0 %s",boosty::stringy(dirinfo) ); - PRINTB("finfo0 %s",boosty::stringy(finfo) ); - if (!exists(finfo)) { - finfo = locate_file((fs::path(filepath) / filename).string()); - } - PRINTB("dinfo %s",boosty::stringy(dirinfo) ); - PRINTB("fnam %s",boosty::stringy(filename) ); - PRINTB("finfo %s",boosty::stringy(finfo) ); - if (!exists(finfo) || finfo.empty()) { - // deal with some unusual situations with is_absolute() and Wine - fs::path fnp( fs::path(filepath) / filename ); - if (fs::exists( fnp ) && !fs::is_directory( fnp )) { - finfo = fnp; - } + PRINTB("lex includefile filename %s",filename.c_str()); + PRINTB("lex includefile filepath %s",filepath.c_str()); + PRINTB("lex includefile sourcepath %s",sourcepath().c_str()); + BOOST_FOREACH(std::string of, openfilenames ) { + PRINTB("lex includefile openfilename: %s",of); } - if (finfo.empty()) { - PRINTB("WARNING: Can't find 'include' file '%s'.", filename); - PRINTB("finfo %s",boosty::stringy(finfo) ); - finfo = dirinfo / filename; - } + fs::path fullpath = find_valid_path( sourcepath(), filename, openfilenames ); + if ( fullpath.empty() ) { + PRINTB("WARNING: Can't open 'include' file '%s'.", filename); + if (path_stack.size()>0) path_stack.pop_back(); + return; + }; + PRINTB("lex fullpath %s",fullpath ); - std::string fullname = boosty::absolute(finfo).string(); - PRINTB("fullname1 %s",fullname.c_str() ); - // Detect circular includes - BOOST_FOREACH(std::string &s, openfilenames) { - if (s == fullname) return; - } + std::string fullname = boosty::stringy( fullpath ); + PRINTB("lex fullname %s",fullname ); filepath.clear(); - path_stack.push_back(finfo.parent_path()); + path_stack.push_back(fullpath.parent_path()); handle_dep(fullname); - rootmodule->registerInclude(fullname); + yyin = fopen(fullname.c_str(), "r"); if (!yyin) { - PRINTB("WARNING: Can't open 'include' file '%s'.", filename); + PRINTB("WARNING: Can't open 'include' file '%s'.", fullname); path_stack.pop_back(); return; } + + rootmodule->registerInclude(fullname); + openfiles.push_back(yyin); openfilenames.push_back(fullname); filename.clear(); diff --git a/src/parsersettings.cc b/src/parsersettings.cc index 4273f7c..799949c 100644 --- a/src/parsersettings.cc +++ b/src/parsersettings.cc @@ -24,15 +24,21 @@ fs::path search_libs(const std::string &filename) { BOOST_FOREACH(const std::string &dir, librarypath) { fs::path usepath = fs::path(dir) / filename; - if (fs::exists(usepath) && !fs::is_directory(usepath)) return usepath.string(); + if (fs::exists(usepath) && !fs::is_directory(usepath)) { + PRINTB("found %s in %s", filename % dir ); + return usepath.string(); + } } return fs::path(); } // files must be 'ordinary' - they mus exist and be non-directories -bool check_valid( fs::path p ) +bool check_valid( fs::path p, std::vector openfilenames ) { - // if p empty PRINT("WARNING: 'use' filename is blank"); + if (p.empty()) { + PRINTB("WARNING: %s invalid - file path is blank",p); + return false; + } if (!p.has_parent_path()) { PRINTB("WARNING: %s invalid - no parent path",p); return false; @@ -46,23 +52,30 @@ bool check_valid( fs::path p ) PRINTB("WARNING: %s invalid - points to a directory",p); return false; } + std::string fullname = boosty::stringy( p ); + BOOST_FOREACH(std::string &s, openfilenames) { + PRINTB("WARNING: circular include with %s", fullname); + if (s == fullname) return false; + } return true; } // check if file is valid, search path for valid simple file // return empty path on failure -fs::path find_valid_path( fs::path sourcepath, std::string filename ) +fs::path find_valid_path( fs::path sourcepath, std::string filename, + std::vector openfilenames ) { fs::path fpath = fs::path( filename ); if ( boosty::is_absolute( fpath ) ) - if ( check_valid( fpath ) ) + if ( check_valid( fpath, openfilenames ) ) return boosty::absolute( fpath ); + fpath = sourcepath / filename; - if ( check_valid( fpath ) ) return fpath; + if ( check_valid( fpath, openfilenames ) ) return fpath; fpath = search_libs( filename ); - if ( check_valid( fpath ) ) return fpath; + if ( check_valid( fpath, openfilenames ) ) return fpath; return fs::path(); } diff --git a/src/parsersettings.h b/src/parsersettings.h index 4c74a87..12b4a61 100644 --- a/src/parsersettings.h +++ b/src/parsersettings.h @@ -9,6 +9,7 @@ extern int parser_error_pos; void parser_init(const std::string &applicationpath); void add_librarydir(const std::string &libdir); fs::path search_libs(const std::string &filename); -fs::path find_valid_path( fs::path sourcepath, std::string filename ); +fs::path find_valid_path( fs::path sourcepath, std::string filename, + std::vector openfilenames ); #endif -- cgit v0.10.1