From 8837e0dcffc6284e60eed3f3ee73a9362960bbc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 11 Sep 2009 21:22:41 +0200 Subject: [PATCH] Simple fix for #635 (preprocessor: remove 'asm(...)') --- src/preprocessor.cpp | 37 +++++++++++++++++++++++++++++++++++++ src/preprocessor.h | 7 +++++++ test/testpreprocessor.cpp | 13 +++++++++++++ 3 files changed, 57 insertions(+) diff --git a/src/preprocessor.cpp b/src/preprocessor.cpp index e49fdf70d..570c34c65 100644 --- a/src/preprocessor.cpp +++ b/src/preprocessor.cpp @@ -433,6 +433,40 @@ std::string Preprocessor::removeComments(const std::string &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 &result, const std::string &filename, const std::list &includePaths) { std::list 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 processedFile = removeSpaceNearNL(processedFile); + // Remove asm(...) + removeAsm(processedFile); + // Replace "defined A" with "defined(A)" { std::istringstream istr(processedFile.c_str()); diff --git a/src/preprocessor.h b/src/preprocessor.h index a42c33ee8..aac473b5c 100644 --- a/src/preprocessor.h +++ b/src/preprocessor.h @@ -126,6 +126,13 @@ private: static std::string getdef(std::string line, bool def); public: + + /** + * Remove asm(...) from a string + * @param str Code + */ + static void removeAsm(std::string &str); + /** * Evaluate condition 'numerically' * @param cfg configuration diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index 4cc0443e3..eeaec0e66 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -101,6 +101,8 @@ private: TEST_CASE(multiline4); TEST_CASE(multiline5); + TEST_CASE(remove_asm); + TEST_CASE(if_defined); // "#if defined(AAA)" => "#ifdef AAA" TEST_CASE(if_not_defined); // "#if !defined(AAA)" => "#ifndef AAA" @@ -762,6 +764,17 @@ private: 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() { const char filedata[] = "#if defined(AAA)\n"