mirror of https://gitlab.com/cppit/libclangmm
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
112 lines
3.4 KiB
112 lines
3.4 KiB
|
6 years ago
|
#include "utility.hpp"
|
||
|
8 years ago
|
#include <regex>
|
||
|
11 years ago
|
|
||
|
9 years ago
|
std::string clangmm::to_string(CXString cx_string) {
|
||
|
11 years ago
|
std::string string;
|
||
|
8 years ago
|
if(cx_string.data != NULL) {
|
||
|
|
string = clang_getCString(cx_string);
|
||
|
11 years ago
|
clang_disposeString(cx_string);
|
||
|
|
}
|
||
|
|
return string;
|
||
|
8 years ago
|
}
|
||
|
|
|
||
|
8 years ago
|
clangmm::String::String(const CXString &cx_string) : cx_string(cx_string) {
|
||
|
8 years ago
|
if(cx_string.data != NULL)
|
||
|
|
c_str = clang_getCString(cx_string);
|
||
|
8 years ago
|
else
|
||
|
8 years ago
|
c_str = "";
|
||
|
8 years ago
|
}
|
||
|
|
|
||
|
8 years ago
|
clangmm::String::~String() {
|
||
|
8 years ago
|
if(cx_string.data != NULL)
|
||
|
8 years ago
|
clang_disposeString(cx_string);
|
||
|
|
}
|
||
|
|
|
||
|
8 years ago
|
void clangmm::remove_include_guard(std::string &buffer) {
|
||
|
|
static std::regex ifndef_regex1("^[ \t]*#[ \t]*ifndef[ \t]+([A-Za-z0-9_]+).*$");
|
||
|
|
static std::regex ifndef_regex2("^[ \t]*#[ \t]*if[ \t]+![ \t]*defined[ \t]*\\([ \t]*([A-Za-z0-9_]+).*$");
|
||
|
|
static std::regex define_regex("^[ \t]*#[ \t]*define[ \t]+([A-Za-z0-9_]+).*$");
|
||
|
|
static std::regex endif_regex("^[ \t]*#[ \t]*endif.*$");
|
||
|
|
std::vector<std::pair<size_t, size_t>> ranges;
|
||
|
8 years ago
|
bool found_ifndef = false, found_define = false;
|
||
|
|
bool line_comment = false, multiline_comment = false;
|
||
|
|
size_t start_of_line = 0;
|
||
|
8 years ago
|
std::string line;
|
||
|
|
std::string preprocessor_identifier;
|
||
|
8 years ago
|
for(size_t c = 0; c < buffer.size(); ++c) {
|
||
|
|
if(!line_comment && !multiline_comment && buffer[c] == '/' && c + 1 < buffer.size() && (buffer[c + 1] == '/' || buffer[c + 1] == '*')) {
|
||
|
|
if(buffer[c + 1] == '/')
|
||
|
|
line_comment = true;
|
||
|
8 years ago
|
else
|
||
|
8 years ago
|
multiline_comment = true;
|
||
|
8 years ago
|
++c;
|
||
|
|
}
|
||
|
8 years ago
|
else if(multiline_comment && buffer[c] == '*' && c + 1 < buffer.size() && buffer[c + 1] == '/') {
|
||
|
|
multiline_comment = false;
|
||
|
8 years ago
|
++c;
|
||
|
|
}
|
||
|
8 years ago
|
else if(buffer[c] == '\n') {
|
||
|
|
bool empty_line = true;
|
||
|
|
for(auto &chr : line) {
|
||
|
|
if(chr != ' ' && chr != '\t') {
|
||
|
|
empty_line = false;
|
||
|
8 years ago
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
8 years ago
|
|
||
|
8 years ago
|
std::smatch sm;
|
||
|
8 years ago
|
if(empty_line) {
|
||
|
|
}
|
||
|
8 years ago
|
else if(!found_ifndef && (std::regex_match(line, sm, ifndef_regex1) || std::regex_match(line, sm, ifndef_regex2))) {
|
||
|
8 years ago
|
found_ifndef = true;
|
||
|
8 years ago
|
ranges.emplace_back(start_of_line, c);
|
||
|
8 years ago
|
preprocessor_identifier = sm[1].str();
|
||
|
8 years ago
|
}
|
||
|
|
else if(found_ifndef && std::regex_match(line, sm, define_regex)) {
|
||
|
8 years ago
|
found_define = true;
|
||
|
8 years ago
|
ranges.emplace_back(start_of_line, c);
|
||
|
8 years ago
|
if(preprocessor_identifier != sm[1].str())
|
||
|
8 years ago
|
return;
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
else
|
||
|
|
return;
|
||
|
8 years ago
|
|
||
|
|
line_comment = false;
|
||
|
8 years ago
|
line.clear();
|
||
|
8 years ago
|
if(c + 1 < buffer.size())
|
||
|
|
start_of_line = c + 1;
|
||
|
8 years ago
|
else
|
||
|
|
return;
|
||
|
|
}
|
||
|
8 years ago
|
else if(!line_comment && !multiline_comment && buffer[c] != '\r')
|
||
|
|
line += buffer[c];
|
||
|
8 years ago
|
}
|
||
|
|
if(found_ifndef && found_define) {
|
||
|
8 years ago
|
size_t last_char_pos = std::string::npos;
|
||
|
|
for(size_t c = buffer.size() - 1; c != std::string::npos; --c) {
|
||
|
|
if(last_char_pos == std::string::npos) {
|
||
|
|
if(buffer[c] != ' ' && buffer[c] != '\t' && buffer[c] != '\r' && buffer[c] != '\n')
|
||
|
|
last_char_pos = c;
|
||
|
8 years ago
|
}
|
||
|
|
else {
|
||
|
8 years ago
|
if(buffer[c] == '\n' && c + 1 < buffer.size()) {
|
||
|
|
auto line = buffer.substr(c + 1, last_char_pos - c);
|
||
|
8 years ago
|
std::smatch sm;
|
||
|
|
if(std::regex_match(line, sm, endif_regex)) {
|
||
|
8 years ago
|
ranges.emplace_back(c + 1, last_char_pos + 1);
|
||
|
|
for(auto &range : ranges) {
|
||
|
|
for(size_t c = range.first; c < range.second; ++c) {
|
||
|
|
if(buffer[c] != '\r')
|
||
|
|
buffer[c] = ' ';
|
||
|
8 years ago
|
}
|
||
|
|
}
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|