Simple fix for #635 (preprocessor: remove 'asm(...)')

This commit is contained in:
Daniel Marjamäki 2009-09-11 21:22:41 +02:00
parent 9828117aff
commit 8837e0dcff
3 changed files with 57 additions and 0 deletions

View File

@ -433,6 +433,40 @@ std::string Preprocessor::removeComments(const std::string &str)
return code.str(); return code.str();
} }
void Preprocessor::removeAsm(std::string &str)
{
std::string::size_type pos = 0;
while ((pos = str.find("\nasm(", pos)) != std::string::npos)
{
unsigned int newlines = 0;
std::string::size_type pos2 = pos + 5;
while (pos2 < str.length() && str[pos2] != ')')
{
if (str[pos2] == '\n')
++newlines;
++pos2;
}
str.erase(pos + 1, pos2 - pos);
str.insert(pos, std::string(newlines, '\n'));
}
pos = 0;
while ((pos = str.find("\nasm __volatile(", pos)) != std::string::npos)
{
unsigned int newlines = 0;
std::string::size_type pos2 = pos + 5;
while (pos2 < str.length() && str[pos2] != ')')
{
if (str[pos2] == '\n')
++newlines;
++pos2;
}
str.erase(pos + 1, pos2 - pos);
str.insert(pos, std::string(newlines, '\n'));
}
}
void Preprocessor::preprocess(std::istream &istr, std::map<std::string, std::string> &result, const std::string &filename, const std::list<std::string> &includePaths) void Preprocessor::preprocess(std::istream &istr, std::map<std::string, std::string> &result, const std::string &filename, const std::list<std::string> &includePaths)
{ {
std::list<std::string> configs; std::list<std::string> configs;
@ -518,6 +552,9 @@ void Preprocessor::preprocess(std::istream &istr, std::string &processedFile, st
// Remove space characters that are after or before new line character // Remove space characters that are after or before new line character
processedFile = removeSpaceNearNL(processedFile); processedFile = removeSpaceNearNL(processedFile);
// Remove asm(...)
removeAsm(processedFile);
// Replace "defined A" with "defined(A)" // Replace "defined A" with "defined(A)"
{ {
std::istringstream istr(processedFile.c_str()); std::istringstream istr(processedFile.c_str());

View File

@ -126,6 +126,13 @@ private:
static std::string getdef(std::string line, bool def); static std::string getdef(std::string line, bool def);
public: public:
/**
* Remove asm(...) from a string
* @param str Code
*/
static void removeAsm(std::string &str);
/** /**
* Evaluate condition 'numerically' * Evaluate condition 'numerically'
* @param cfg configuration * @param cfg configuration

View File

@ -101,6 +101,8 @@ private:
TEST_CASE(multiline4); TEST_CASE(multiline4);
TEST_CASE(multiline5); TEST_CASE(multiline5);
TEST_CASE(remove_asm);
TEST_CASE(if_defined); // "#if defined(AAA)" => "#ifdef AAA" TEST_CASE(if_defined); // "#if defined(AAA)" => "#ifdef AAA"
TEST_CASE(if_not_defined); // "#if !defined(AAA)" => "#ifndef AAA" TEST_CASE(if_not_defined); // "#if !defined(AAA)" => "#ifndef AAA"
@ -762,6 +764,17 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void remove_asm()
{
std::string str1("\nasm(\n\n\n);");
Preprocessor::removeAsm(str1);
ASSERT_EQUALS("\n\n\n\n;", str1);
std::string str2("\nasm __volatile(\n\n\n);");
Preprocessor::removeAsm(str2);
ASSERT_EQUALS("\n\n\n\n;", str2);
}
void if_defined() void if_defined()
{ {
const char filedata[] = "#if defined(AAA)\n" const char filedata[] = "#if defined(AAA)\n"