Browse Source

Cleanup and improvement of removed_include_guard: now also identifies #if !defined(...

merge-requests/365/head
eidheim 9 years ago
parent
commit
c1307fc5d7
  1. 18
      src/source_clang.cc
  2. 4
      tests/source_clang_test.cc

18
src/source_clang.cc

@ -252,7 +252,8 @@ void Source::ClangViewParse::remove_include_guard(std::string &buffer) {
if(!(language && (language->get_id()=="chdr" || language->get_id()=="cpphdr"))) if(!(language && (language->get_id()=="chdr" || language->get_id()=="cpphdr")))
return; return;
static std::regex ifndef_regex("^[ \t]*#ifndef[ \t]+([A-Za-z0-9_]+).*$"); static std::regex ifndef_regex1("^[ \t]*#ifndef[ \t]+([A-Za-z0-9_]+).*$");
static std::regex ifndef_regex2("^[ \t]*#if[ \t]+![ \t]*defined[ \t]*\\([ \t]*([A-Za-z0-9_]+).*$");
static std::regex define_regex("^[ \t]*#define[ \t]+([A-Za-z0-9_]+).*$"); static std::regex define_regex("^[ \t]*#define[ \t]+([A-Za-z0-9_]+).*$");
static std::regex endif_regex("^[ \t]*#endif.*$"); static std::regex endif_regex("^[ \t]*#endif.*$");
std::vector<std::pair<size_t, size_t>> ranges; std::vector<std::pair<size_t, size_t>> ranges;
@ -262,17 +263,16 @@ void Source::ClangViewParse::remove_include_guard(std::string &buffer) {
std::string line; std::string line;
std::string preprocessor_identifier; std::string preprocessor_identifier;
for(size_t c=0;c<buffer.size();++c) { for(size_t c=0;c<buffer.size();++c) {
if(!line_comment && !multiline_comment && buffer[c]=='/') { if(!line_comment && !multiline_comment && buffer[c]=='/' && c+1<buffer.size() && (buffer[c+1]=='/' || buffer[c+1]=='*')) {
if(c+1<buffer.size()) {
if(buffer[c+1]=='/') if(buffer[c+1]=='/')
line_comment=true; line_comment=true;
else if(buffer[c+1]=='*') else
multiline_comment=true; multiline_comment=true;
++c;
} }
} else if(multiline_comment && buffer[c]=='*' && c+1<buffer.size() && buffer[c+1]=='/') {
else if(multiline_comment && buffer[c]=='*') {
if(c+1<buffer.size() && buffer[c+1]=='/')
multiline_comment=false; multiline_comment=false;
++c;
} }
else if(buffer[c]=='\n') { else if(buffer[c]=='\n') {
bool empty_line=true; bool empty_line=true;
@ -285,7 +285,7 @@ void Source::ClangViewParse::remove_include_guard(std::string &buffer) {
std::smatch sm; std::smatch sm;
if(empty_line) {} if(empty_line) {}
else if(!found_ifndef && std::regex_match(line, sm, ifndef_regex)) { else if(!found_ifndef && (std::regex_match(line, sm, ifndef_regex1) || std::regex_match(line, sm, ifndef_regex2))) {
found_ifndef=true; found_ifndef=true;
ranges.emplace_back(start_of_line, c); ranges.emplace_back(start_of_line, c);
preprocessor_identifier=sm[1].str(); preprocessor_identifier=sm[1].str();
@ -307,7 +307,7 @@ void Source::ClangViewParse::remove_include_guard(std::string &buffer) {
else else
return; return;
} }
else if(buffer[c]!='\r' && !line_comment && !multiline_comment) else if(!line_comment && !multiline_comment && buffer[c]!='\r')
line+=buffer[c]; line+=buffer[c];
} }
if(found_ifndef && found_define) { if(found_ifndef && found_define) {

4
tests/source_clang_test.cc

@ -111,6 +111,10 @@ int main() {
clang_view->remove_include_guard(source); clang_view->remove_include_guard(source);
g_assert_cmpstr(source.c_str(),==," \n//test\n \n \n"); g_assert_cmpstr(source.c_str(),==," \n//test\n \n \n");
source="#if !defined(F)\n#define F\n#endif\n";
clang_view->remove_include_guard(source);
g_assert_cmpstr(source.c_str(),==," \n \n \n");
source="#ifndef F\ntest\n#define F\n#endif // F\n"; source="#ifndef F\ntest\n#define F\n#endif // F\n";
auto old_source=source; auto old_source=source;
clang_view->remove_include_guard(source); clang_view->remove_include_guard(source);

Loading…
Cancel
Save