2009-01-24 20:28:30 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2010-04-13 21:23:17 +02:00
|
|
|
* Copyright (C) 2007-2010 Daniel Marjamäki and Cppcheck team.
|
2009-01-24 20:28:30 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2009-09-27 17:08:31 +02:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2009-01-24 20:28:30 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "preprocessor.h"
|
|
|
|
#include "tokenize.h"
|
|
|
|
#include "token.h"
|
2009-05-19 21:19:15 +02:00
|
|
|
#include "filelister.h"
|
2010-07-21 16:00:12 +02:00
|
|
|
#include "path.h"
|
2010-07-24 22:12:56 +02:00
|
|
|
#include "errorlogger.h"
|
2010-07-26 22:05:17 +02:00
|
|
|
#include "settings.h"
|
2009-01-24 20:28:30 +01:00
|
|
|
|
|
|
|
#include <algorithm>
|
2009-04-27 21:29:03 +02:00
|
|
|
#include <stdexcept>
|
2009-01-24 20:28:30 +01:00
|
|
|
#include <sstream>
|
|
|
|
#include <fstream>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cctype>
|
2009-05-19 21:19:15 +02:00
|
|
|
#include <cstring>
|
2009-04-05 20:14:02 +02:00
|
|
|
#include <vector>
|
2009-07-22 18:47:50 +02:00
|
|
|
#include <set>
|
2009-12-09 17:13:48 +01:00
|
|
|
#include <stack>
|
2009-01-24 20:28:30 +01:00
|
|
|
|
2009-12-06 19:38:53 +01:00
|
|
|
Preprocessor::Preprocessor(Settings *settings, ErrorLogger *errorLogger) : _settings(settings), _errorLogger(errorLogger)
|
2009-06-26 13:19:55 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-08-06 19:38:21 +02:00
|
|
|
void Preprocessor::writeError(const std::string &fileName, const unsigned int linenr, ErrorLogger *errorLogger, const std::string &errorType, const std::string &errorText)
|
2009-05-19 21:19:15 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!errorLogger)
|
2009-05-19 21:19:15 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
|
|
|
|
ErrorLogger::ErrorMessage::FileLocation loc;
|
2009-12-09 17:13:48 +01:00
|
|
|
loc.line = linenr;
|
2010-07-17 00:27:40 +02:00
|
|
|
loc.setfile(fileName);
|
2009-05-19 21:19:15 +02:00
|
|
|
locationList.push_back(loc);
|
2009-12-09 17:13:48 +01:00
|
|
|
errorLogger->reportErr(ErrorLogger::ErrorMessage(locationList,
|
2010-07-14 22:11:32 +02:00
|
|
|
Severity::error,
|
2009-12-09 17:13:48 +01:00
|
|
|
errorText,
|
|
|
|
errorType));
|
2009-05-19 21:19:15 +02:00
|
|
|
}
|
|
|
|
|
2009-12-20 09:35:51 +01:00
|
|
|
static unsigned char readChar(std::istream &istr)
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
2009-12-22 22:44:21 +01:00
|
|
|
unsigned char ch = (unsigned char)istr.get();
|
2009-01-24 20:28:30 +01:00
|
|
|
|
|
|
|
// Handling of newlines..
|
2010-04-02 07:30:58 +02:00
|
|
|
if (ch == '\r')
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
|
|
|
ch = '\n';
|
2010-04-02 07:30:58 +02:00
|
|
|
if ((char)istr.peek() == '\n')
|
2009-01-24 20:28:30 +01:00
|
|
|
(void)istr.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ch;
|
|
|
|
}
|
|
|
|
|
2010-04-15 18:37:51 +02:00
|
|
|
// Splits a string that contains the specified separator into substrings
|
|
|
|
static std::list<std::string> split(const std::string &s, char separator)
|
|
|
|
{
|
|
|
|
std::list<std::string> parts;
|
|
|
|
|
|
|
|
std::string::size_type prevPos = 0;
|
|
|
|
for (std::string::size_type pos = 0; pos < s.length(); ++pos)
|
|
|
|
{
|
|
|
|
if (s[pos] == separator)
|
|
|
|
{
|
|
|
|
if (pos > prevPos)
|
|
|
|
parts.push_back(s.substr(prevPos, pos - prevPos));
|
|
|
|
prevPos = pos + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (prevPos < s.length())
|
|
|
|
parts.push_back(s.substr(prevPos));
|
|
|
|
|
|
|
|
return parts;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Concatenates a list of strings, inserting a separator between parts
|
|
|
|
static std::string join(const std::list<std::string> &list, char separator)
|
|
|
|
{
|
|
|
|
std::string s;
|
|
|
|
for (std::list<std::string>::const_iterator it = list.begin(); it != list.end(); ++it)
|
|
|
|
{
|
|
|
|
if (!s.empty())
|
|
|
|
s += separator;
|
|
|
|
|
|
|
|
s += *it;
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2009-01-24 20:28:30 +01:00
|
|
|
/** Just read the code into a string. Perform simple cleanup of the code */
|
2009-12-06 19:38:53 +01:00
|
|
|
std::string Preprocessor::read(std::istream &istr, const std::string &filename, Settings *settings)
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
|
|
|
// Get filedata from stream..
|
|
|
|
bool ignoreSpace = true;
|
|
|
|
|
|
|
|
// need space.. #if( => #if (
|
|
|
|
bool needSpace = false;
|
|
|
|
|
|
|
|
// handling <backspace><newline>
|
|
|
|
// when this is encountered the <backspace><newline> will be "skipped".
|
|
|
|
// on the next <newline>, extra newlines will be added
|
|
|
|
unsigned int newlines = 0;
|
|
|
|
|
|
|
|
std::ostringstream code;
|
2010-04-02 07:30:58 +02:00
|
|
|
for (unsigned char ch = readChar(istr); istr.good(); ch = readChar(istr))
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
|
|
|
// Replace assorted special chars with spaces..
|
2010-04-02 07:30:58 +02:00
|
|
|
if (((ch & 0x80) == 0) && (ch != '\n') && (std::isspace(ch) || std::iscntrl(ch)))
|
2009-01-24 20:28:30 +01:00
|
|
|
ch = ' ';
|
|
|
|
|
|
|
|
// Skip spaces after ' ' and after '#'
|
2010-04-02 07:30:58 +02:00
|
|
|
if (ch == ' ' && ignoreSpace)
|
2009-01-24 20:28:30 +01:00
|
|
|
continue;
|
2009-05-14 21:53:49 +02:00
|
|
|
ignoreSpace = bool(ch == ' ' || ch == '#' || ch == '\n');
|
2009-01-24 20:28:30 +01:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (needSpace)
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (ch == '(' || ch == '!')
|
2009-01-24 20:28:30 +01:00
|
|
|
code << " ";
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (!std::isalpha(ch))
|
2009-01-24 20:28:30 +01:00
|
|
|
needSpace = false;
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
if (ch == '#')
|
2009-01-24 20:28:30 +01:00
|
|
|
needSpace = true;
|
|
|
|
|
2009-05-13 21:18:02 +02:00
|
|
|
// <backspace><newline>..
|
2010-08-06 08:50:31 +02:00
|
|
|
// for gcc-compatibility the trailing spaces should be ignored
|
|
|
|
// for vs-compatibility the trailing spaces should be kept
|
|
|
|
// See tickets #640 and #1869
|
|
|
|
// The solution for now is to have a compiler-dependent behaviour.
|
2010-04-02 07:30:58 +02:00
|
|
|
if (ch == '\\')
|
2009-05-13 21:18:02 +02:00
|
|
|
{
|
2010-08-06 08:50:31 +02:00
|
|
|
unsigned char chNext;
|
|
|
|
|
|
|
|
#ifdef __GNUC__
|
|
|
|
// gcc-compatibility: ignore spaces
|
2010-04-02 07:30:58 +02:00
|
|
|
for (;;)
|
2009-05-13 21:18:02 +02:00
|
|
|
{
|
2009-12-22 22:44:21 +01:00
|
|
|
chNext = (unsigned char)istr.peek();
|
2010-04-02 07:30:58 +02:00
|
|
|
if (chNext != '\n' && chNext != '\r' &&
|
|
|
|
(std::isspace(chNext) || std::iscntrl(chNext)))
|
2009-05-13 21:18:02 +02:00
|
|
|
{
|
|
|
|
// Skip whitespace between <backspace> and <newline>
|
|
|
|
(void)readChar(istr);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2010-08-06 08:50:31 +02:00
|
|
|
#else
|
|
|
|
// keep spaces
|
|
|
|
chNext = (unsigned char)istr.peek();
|
|
|
|
#endif
|
2010-04-02 07:30:58 +02:00
|
|
|
if (chNext == '\n' || chNext == '\r')
|
2009-05-13 21:18:02 +02:00
|
|
|
{
|
|
|
|
++newlines;
|
|
|
|
(void)readChar(istr); // Skip the "<backspace><newline>"
|
|
|
|
}
|
|
|
|
else
|
|
|
|
code << "\\";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Just some code..
|
|
|
|
else
|
|
|
|
{
|
2010-08-06 19:38:21 +02:00
|
|
|
code << char(ch);
|
2009-05-13 21:18:02 +02:00
|
|
|
|
|
|
|
// if there has been <backspace><newline> sequences, add extra newlines..
|
2010-04-02 07:30:58 +02:00
|
|
|
if (ch == '\n' && newlines > 0)
|
2009-05-13 21:18:02 +02:00
|
|
|
{
|
|
|
|
code << std::string(newlines, '\n');
|
|
|
|
newlines = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-06 19:38:53 +01:00
|
|
|
return removeParantheses(removeComments(code.str(), filename, settings));
|
2009-05-13 21:18:02 +02:00
|
|
|
}
|
|
|
|
|
2009-10-10 09:29:06 +02:00
|
|
|
static bool hasbom(const std::string &str)
|
|
|
|
{
|
|
|
|
return bool(str.size() > 3 &&
|
2009-10-11 16:38:55 +02:00
|
|
|
static_cast<unsigned char>(str[0]) == 0xef &&
|
|
|
|
static_cast<unsigned char>(str[1]) == 0xbb &&
|
|
|
|
static_cast<unsigned char>(str[2]) == 0xbf);
|
2009-10-10 09:29:06 +02:00
|
|
|
}
|
2009-05-13 21:18:02 +02:00
|
|
|
|
|
|
|
|
2009-12-06 19:38:53 +01:00
|
|
|
std::string Preprocessor::removeComments(const std::string &str, const std::string &filename, Settings *settings)
|
2009-05-13 21:18:02 +02:00
|
|
|
{
|
|
|
|
// For the error report
|
2010-08-06 19:38:21 +02:00
|
|
|
unsigned int lineno = 1;
|
2009-05-13 21:18:02 +02:00
|
|
|
|
|
|
|
// handling <backspace><newline>
|
|
|
|
// when this is encountered the <backspace><newline> will be "skipped".
|
|
|
|
// on the next <newline>, extra newlines will be added
|
|
|
|
unsigned int newlines = 0;
|
|
|
|
std::ostringstream code;
|
2009-12-20 09:35:51 +01:00
|
|
|
unsigned char previous = 0;
|
2009-12-06 19:38:53 +01:00
|
|
|
std::vector<std::string> suppressionIDs;
|
|
|
|
|
2010-08-06 21:02:43 +02:00
|
|
|
for (std::string::size_type i = hasbom(str) ? 3U : 0U; i < str.length(); ++i)
|
2009-05-13 21:18:02 +02:00
|
|
|
{
|
2010-08-06 19:38:21 +02:00
|
|
|
unsigned char ch = static_cast<unsigned char>(str[i]);
|
2010-04-02 07:30:58 +02:00
|
|
|
if (ch & 0x80)
|
2010-02-27 13:08:59 +01:00
|
|
|
{
|
2010-02-27 17:06:33 +01:00
|
|
|
std::ostringstream errmsg;
|
|
|
|
errmsg << "The code contains characters that are unhandled. "
|
2010-04-15 20:08:51 +02:00
|
|
|
<< "Neither unicode nor extended ascii are supported. "
|
|
|
|
<< "(line=" << lineno << ", character code=" << std::hex << (int(ch) & 0xff) << ")";
|
2010-07-23 13:54:52 +02:00
|
|
|
writeError(filename, lineno, _errorLogger, "syntaxError", errmsg.str());
|
2010-02-27 13:08:59 +01:00
|
|
|
}
|
2009-05-13 21:18:02 +02:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (str.compare(i, 6, "#error") == 0 || str.compare(i, 8, "#warning") == 0)
|
2009-12-22 20:38:12 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (str.compare(i, 6, "#error") == 0)
|
2009-12-22 20:38:12 +01:00
|
|
|
code << "#error";
|
|
|
|
|
|
|
|
i = str.find("\n", i);
|
2010-04-02 07:30:58 +02:00
|
|
|
if (i == std::string::npos)
|
2009-12-22 20:38:12 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
--i;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-12-06 19:38:53 +01:00
|
|
|
// We have finished a line that didn't contain any comment
|
|
|
|
// (the '\n' is swallowed when a // comment is detected)
|
2010-04-02 07:30:58 +02:00
|
|
|
if (ch == '\n' && !suppressionIDs.empty())
|
2009-12-06 19:38:53 +01:00
|
|
|
{
|
|
|
|
// Add the suppressions.
|
2010-04-02 07:30:58 +02:00
|
|
|
for (size_t j(0); j < suppressionIDs.size(); ++j)
|
2010-07-23 23:12:56 +02:00
|
|
|
{
|
|
|
|
const std::string errmsg(settings->nomsg.addSuppression(suppressionIDs[j], filename, lineno));
|
|
|
|
if (!errmsg.empty())
|
|
|
|
{
|
|
|
|
writeError(filename, lineno, _errorLogger, "cppcheckError", errmsg);
|
|
|
|
}
|
|
|
|
}
|
2009-12-06 19:38:53 +01:00
|
|
|
suppressionIDs.clear();
|
|
|
|
}
|
|
|
|
|
2009-01-24 20:28:30 +01:00
|
|
|
// Remove comments..
|
2010-04-02 07:30:58 +02:00
|
|
|
if (str.compare(i, 2, "//", 0, 2) == 0)
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
2009-12-06 19:38:53 +01:00
|
|
|
size_t commentStart = i + 2;
|
2009-05-14 21:53:49 +02:00
|
|
|
i = str.find('\n', i);
|
2010-04-02 07:30:58 +02:00
|
|
|
if (i == std::string::npos)
|
2009-05-14 21:53:49 +02:00
|
|
|
break;
|
2009-01-24 20:28:30 +01:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (settings && settings->_inlineSuppressions)
|
2009-12-06 19:38:53 +01:00
|
|
|
{
|
|
|
|
std::string comment(str, commentStart, i - commentStart);
|
|
|
|
std::istringstream iss(comment);
|
|
|
|
std::string word;
|
|
|
|
iss >> word;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (word == "cppcheck-suppress")
|
2009-12-06 19:38:53 +01:00
|
|
|
{
|
|
|
|
iss >> word;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (iss)
|
2009-12-06 19:38:53 +01:00
|
|
|
suppressionIDs.push_back(word);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-14 21:53:49 +02:00
|
|
|
code << "\n";
|
|
|
|
previous = '\n';
|
|
|
|
++lineno;
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (str.compare(i, 2, "/*", 0, 2) == 0)
|
2009-05-14 21:53:49 +02:00
|
|
|
{
|
2009-12-20 09:35:51 +01:00
|
|
|
unsigned char chPrev = 0;
|
2009-05-14 21:53:49 +02:00
|
|
|
++i;
|
2010-04-02 07:30:58 +02:00
|
|
|
while (i < str.length() && (chPrev != '*' || ch != '/'))
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
2009-05-14 21:53:49 +02:00
|
|
|
chPrev = ch;
|
|
|
|
++i;
|
2010-08-06 19:38:21 +02:00
|
|
|
ch = static_cast<unsigned char>(str[i]);
|
2010-04-02 07:30:58 +02:00
|
|
|
if (ch == '\n')
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
2009-08-19 23:27:47 +02:00
|
|
|
++newlines;
|
2009-01-24 20:28:30 +01:00
|
|
|
++lineno;
|
2009-05-14 21:53:49 +02:00
|
|
|
}
|
2009-01-24 20:28:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// String or char constants..
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (ch == '\"' || ch == '\'')
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
2010-08-06 19:38:21 +02:00
|
|
|
code << char(ch);
|
|
|
|
char chNext;
|
2009-01-24 20:28:30 +01:00
|
|
|
do
|
|
|
|
{
|
2009-05-13 21:18:02 +02:00
|
|
|
++i;
|
|
|
|
chNext = str[i];
|
2010-04-02 07:30:58 +02:00
|
|
|
if (chNext == '\\')
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
2009-05-13 21:18:02 +02:00
|
|
|
++i;
|
|
|
|
char chSeq = str[i];
|
2010-04-02 07:30:58 +02:00
|
|
|
if (chSeq == '\n')
|
2009-01-24 20:28:30 +01:00
|
|
|
++newlines;
|
|
|
|
else
|
|
|
|
{
|
2010-08-06 19:38:21 +02:00
|
|
|
code << chNext;
|
|
|
|
code << chSeq;
|
|
|
|
previous = static_cast<unsigned char>(chSeq);
|
2009-01-24 20:28:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2009-05-14 21:53:49 +02:00
|
|
|
{
|
2010-08-06 19:38:21 +02:00
|
|
|
code << chNext;
|
|
|
|
previous = static_cast<unsigned char>(chNext);
|
2009-05-14 21:53:49 +02:00
|
|
|
}
|
2009-01-24 20:28:30 +01:00
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
while (i < str.length() && chNext != ch && chNext != '\n');
|
2009-01-24 20:28:30 +01:00
|
|
|
}
|
2009-05-11 20:12:29 +02:00
|
|
|
|
2009-01-24 20:28:30 +01:00
|
|
|
|
|
|
|
// Just some code..
|
|
|
|
else
|
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (ch == ' ' && previous == ' ')
|
2009-05-14 21:53:49 +02:00
|
|
|
{
|
|
|
|
// Skip double white space
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-08-06 19:38:21 +02:00
|
|
|
code << char(ch);
|
2009-05-14 21:53:49 +02:00
|
|
|
previous = ch;
|
|
|
|
}
|
|
|
|
|
2009-01-24 20:28:30 +01:00
|
|
|
|
|
|
|
// if there has been <backspace><newline> sequences, add extra newlines..
|
2010-04-02 07:30:58 +02:00
|
|
|
if (ch == '\n')
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
2009-12-06 19:38:53 +01:00
|
|
|
++lineno;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (newlines > 0)
|
2009-12-06 19:38:53 +01:00
|
|
|
{
|
|
|
|
code << std::string(newlines, '\n');
|
|
|
|
newlines = 0;
|
|
|
|
previous = '\n';
|
|
|
|
}
|
2009-01-24 20:28:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return code.str();
|
|
|
|
}
|
|
|
|
|
2009-09-11 23:34:24 +02:00
|
|
|
|
2009-10-04 07:25:30 +02:00
|
|
|
std::string Preprocessor::removeParantheses(const std::string &str)
|
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (str.find("\n#if") == std::string::npos && str.compare(0, 3, "#if") != 0)
|
2009-10-04 07:25:30 +02:00
|
|
|
return str;
|
|
|
|
|
|
|
|
std::istringstream istr(str.c_str());
|
|
|
|
std::ostringstream ret;
|
|
|
|
std::string line;
|
2010-04-02 07:30:58 +02:00
|
|
|
while (std::getline(istr, line))
|
2009-10-04 07:25:30 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (line.compare(0, 3, "#if") == 0 || line.compare(0, 5, "#elif") == 0)
|
2009-10-04 07:25:30 +02:00
|
|
|
{
|
2009-10-04 15:41:50 +02:00
|
|
|
std::string::size_type pos;
|
|
|
|
pos = 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
while ((pos = line.find(" (", pos)) != std::string::npos)
|
2009-10-04 15:41:50 +02:00
|
|
|
line.erase(pos, 1);
|
|
|
|
pos = 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
while ((pos = line.find("( ", pos)) != std::string::npos)
|
2009-10-04 15:41:50 +02:00
|
|
|
line.erase(pos + 1, 1);
|
|
|
|
pos = 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
while ((pos = line.find(" )", pos)) != std::string::npos)
|
2009-10-04 15:41:50 +02:00
|
|
|
line.erase(pos, 1);
|
|
|
|
pos = 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
while ((pos = line.find(") ", pos)) != std::string::npos)
|
2009-10-04 15:41:50 +02:00
|
|
|
line.erase(pos + 1, 1);
|
|
|
|
|
|
|
|
// Remove inner paranthesis "((..))"..
|
|
|
|
pos = 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
while ((pos = line.find("((", pos)) != std::string::npos)
|
2009-10-04 15:41:50 +02:00
|
|
|
{
|
|
|
|
++pos;
|
|
|
|
std::string::size_type pos2 = line.find_first_of("()", pos + 1);
|
2010-04-02 07:30:58 +02:00
|
|
|
if (pos2 != std::string::npos && line[pos2] == ')')
|
2009-10-04 15:41:50 +02:00
|
|
|
{
|
|
|
|
line.erase(pos2, 1);
|
|
|
|
line.erase(pos, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-06 10:47:36 +02:00
|
|
|
// "#if(A) => #if A", but avoid "#if (defined A) || defined (B)"
|
2010-04-02 07:30:58 +02:00
|
|
|
if ((line.compare(0, 4, "#if(") == 0 || line.compare(0, 6, "#elif(") == 0) &&
|
|
|
|
line[line.length() - 1] == ')')
|
2009-10-04 07:25:30 +02:00
|
|
|
{
|
2009-10-06 10:47:36 +02:00
|
|
|
int ind = 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
for (std::string::size_type i = 0; i < line.length(); ++i)
|
2009-10-06 10:47:36 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (line[i] == '(')
|
2009-10-06 10:47:36 +02:00
|
|
|
++ind;
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (line[i] == ')')
|
2009-10-06 10:47:36 +02:00
|
|
|
{
|
|
|
|
--ind;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (ind == 0)
|
2009-10-06 10:47:36 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (i == line.length() - 1)
|
2009-10-06 10:47:36 +02:00
|
|
|
{
|
2010-03-28 14:15:25 +02:00
|
|
|
line[line.find('(')] = ' ';
|
2009-10-06 10:47:36 +02:00
|
|
|
line.erase(line.length() - 1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-10-04 07:25:30 +02:00
|
|
|
}
|
2009-10-06 10:47:36 +02:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (line.compare(0, 4, "#if(") == 0)
|
2009-10-06 10:47:36 +02:00
|
|
|
line.insert(3, " ");
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (line.compare(0, 6, "#elif(") == 0)
|
2009-10-06 10:47:36 +02:00
|
|
|
line.insert(5, " ");
|
2009-10-04 07:25:30 +02:00
|
|
|
}
|
|
|
|
ret << line << "\n";
|
|
|
|
}
|
2009-10-06 10:47:36 +02:00
|
|
|
|
2009-10-04 07:25:30 +02:00
|
|
|
return ret.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-11 23:34:24 +02:00
|
|
|
|
|
|
|
static void _removeAsm(std::string &str, const std::string::size_type pos)
|
2009-09-11 21:22:41 +02:00
|
|
|
{
|
2009-09-11 23:34:24 +02:00
|
|
|
unsigned int newlines = 0;
|
|
|
|
bool instr = false;
|
|
|
|
int parlevel = 0;
|
|
|
|
std::string::size_type pos2 = pos + 1;
|
2010-04-02 07:30:58 +02:00
|
|
|
while (pos2 < str.length())
|
2009-09-11 21:22:41 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (str[pos2] == '\"')
|
2009-09-11 23:34:24 +02:00
|
|
|
instr = !instr;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (str[pos2] == '\n')
|
2009-09-11 23:34:24 +02:00
|
|
|
++newlines;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (!instr)
|
2009-09-11 21:22:41 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (str[pos2] == '(')
|
2009-09-11 23:34:24 +02:00
|
|
|
++parlevel;
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (str[pos2] == ')')
|
2009-09-11 23:34:24 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (parlevel <= 1)
|
2009-09-11 23:34:24 +02:00
|
|
|
break;
|
|
|
|
--parlevel;
|
|
|
|
}
|
2009-09-11 21:22:41 +02:00
|
|
|
}
|
2009-09-11 23:34:24 +02:00
|
|
|
|
|
|
|
++pos2;
|
2009-09-11 21:22:41 +02:00
|
|
|
}
|
2009-09-11 23:34:24 +02:00
|
|
|
str.erase(pos + 1, pos2 - pos);
|
|
|
|
str.insert(pos, std::string(newlines, '\n'));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Preprocessor::removeAsm(std::string &str)
|
|
|
|
{
|
|
|
|
std::string::size_type pos = 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
while ((pos = str.find("\nasm(", pos)) != std::string::npos)
|
2009-11-02 18:31:22 +01:00
|
|
|
{
|
|
|
|
_removeAsm(str, pos++);
|
|
|
|
str.insert(pos, "asm()");
|
|
|
|
}
|
2009-09-11 23:34:24 +02:00
|
|
|
|
|
|
|
pos = 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
while ((pos = str.find("\nasm (", pos)) != std::string::npos)
|
2009-11-02 18:31:22 +01:00
|
|
|
{
|
|
|
|
_removeAsm(str, pos++);
|
|
|
|
str.insert(pos, "asm()");
|
|
|
|
}
|
2009-09-11 21:22:41 +02:00
|
|
|
|
|
|
|
pos = 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
while ((pos = str.find("\nasm __volatile(", pos)) != std::string::npos)
|
2009-09-11 23:34:24 +02:00
|
|
|
_removeAsm(str, pos);
|
|
|
|
|
|
|
|
pos = 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
while ((pos = str.find("\nasm __volatile (", pos)) != std::string::npos)
|
2009-09-11 23:34:24 +02:00
|
|
|
_removeAsm(str, pos);
|
2009-09-11 21:22:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-25 21:10:30 +02:00
|
|
|
void Preprocessor::preprocess(std::istream &istr, std::map<std::string, std::string> &result, const std::string &filename, const std::list<std::string> &includePaths)
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
|
|
|
std::list<std::string> configs;
|
|
|
|
std::string data;
|
|
|
|
preprocess(istr, data, configs, filename, includePaths);
|
2010-04-02 07:30:58 +02:00
|
|
|
for (std::list<std::string>::const_iterator it = configs.begin(); it != configs.end(); ++it)
|
2009-07-25 21:10:30 +02:00
|
|
|
result[ *it ] = Preprocessor::getcode(data, *it, filename, _errorLogger);
|
2009-01-24 20:28:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Preprocessor::removeSpaceNearNL(const std::string &str)
|
|
|
|
{
|
|
|
|
std::string tmp;
|
2010-08-06 19:38:21 +02:00
|
|
|
char prev = 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
for (unsigned int i = 0; i < str.size(); i++)
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (str[i] == ' ' &&
|
2010-08-06 19:38:21 +02:00
|
|
|
((i > 0 && prev == '\n') ||
|
2010-04-02 07:30:58 +02:00
|
|
|
(i + 1 < str.size() && str[i+1] == '\n')
|
|
|
|
)
|
2009-01-24 20:28:30 +01:00
|
|
|
)
|
|
|
|
{
|
|
|
|
// Ignore space that has new line in either side of it
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tmp.append(1, str[i]);
|
2010-08-06 19:38:21 +02:00
|
|
|
prev = str[i];
|
2009-01-24 20:28:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Preprocessor::replaceIfDefined(const std::string &str)
|
|
|
|
{
|
|
|
|
std::string ret(str);
|
2009-06-21 08:03:42 +02:00
|
|
|
std::string::size_type pos;
|
|
|
|
|
|
|
|
pos = 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
while ((pos = ret.find("#if defined(", pos)) != std::string::npos)
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
|
|
|
std::string::size_type pos2 = ret.find(")", pos + 9);
|
2010-04-02 07:30:58 +02:00
|
|
|
if (pos2 > ret.length() - 1)
|
2009-01-24 20:28:30 +01:00
|
|
|
break;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (ret[pos2+1] == '\n')
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
|
|
|
ret.erase(pos2, 1);
|
|
|
|
ret.erase(pos + 3, 9);
|
|
|
|
ret.insert(pos + 3, "def ");
|
|
|
|
}
|
|
|
|
++pos;
|
|
|
|
}
|
|
|
|
|
2009-06-21 08:03:42 +02:00
|
|
|
pos = 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
while ((pos = ret.find("#if !defined(", pos)) != std::string::npos)
|
2009-06-21 08:03:42 +02:00
|
|
|
{
|
|
|
|
std::string::size_type pos2 = ret.find(")", pos + 9);
|
2010-04-02 07:30:58 +02:00
|
|
|
if (pos2 > ret.length() - 1)
|
2009-06-21 08:03:42 +02:00
|
|
|
break;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (ret[pos2+1] == '\n')
|
2009-06-21 08:03:42 +02:00
|
|
|
{
|
|
|
|
ret.erase(pos2, 1);
|
|
|
|
ret.erase(pos + 3, 10);
|
|
|
|
ret.insert(pos + 3, "ndef ");
|
|
|
|
}
|
|
|
|
++pos;
|
|
|
|
}
|
|
|
|
|
2009-10-04 15:41:50 +02:00
|
|
|
pos = 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
while ((pos = ret.find("#elif defined(", pos)) != std::string::npos)
|
2009-10-04 15:41:50 +02:00
|
|
|
{
|
|
|
|
std::string::size_type pos2 = ret.find(")", pos + 9);
|
2010-04-02 07:30:58 +02:00
|
|
|
if (pos2 > ret.length() - 1)
|
2009-10-04 15:41:50 +02:00
|
|
|
break;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (ret[pos2+1] == '\n')
|
2009-10-04 15:41:50 +02:00
|
|
|
{
|
|
|
|
ret.erase(pos2, 1);
|
|
|
|
ret.erase(pos + 6, 8);
|
|
|
|
}
|
|
|
|
++pos;
|
|
|
|
}
|
|
|
|
|
2009-01-24 20:28:30 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-04-15 22:45:38 +02:00
|
|
|
void Preprocessor::preprocessWhitespaces(std::string &processedFile)
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
|
|
|
// Replace all tabs with spaces..
|
|
|
|
std::replace(processedFile.begin(), processedFile.end(), '\t', ' ');
|
|
|
|
|
|
|
|
// Remove all indentation..
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!processedFile.empty() && processedFile[0] == ' ')
|
2009-01-24 20:28:30 +01:00
|
|
|
processedFile.erase(0, processedFile.find_first_not_of(" "));
|
|
|
|
|
|
|
|
// Remove space characters that are after or before new line character
|
|
|
|
processedFile = removeSpaceNearNL(processedFile);
|
2010-04-15 22:45:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Preprocessor::preprocess(std::istream &srcCodeStream, std::string &processedFile, std::list<std::string> &resultConfigurations, const std::string &filename, const std::list<std::string> &includePaths)
|
|
|
|
{
|
|
|
|
processedFile = read(srcCodeStream, filename, _settings);
|
|
|
|
|
|
|
|
// normalize the whitespaces of the file
|
|
|
|
preprocessWhitespaces(processedFile);
|
2009-01-24 20:28:30 +01:00
|
|
|
|
2009-09-11 21:22:41 +02:00
|
|
|
// Remove asm(...)
|
|
|
|
removeAsm(processedFile);
|
|
|
|
|
2009-07-22 20:11:27 +02:00
|
|
|
// Replace "defined A" with "defined(A)"
|
|
|
|
{
|
|
|
|
std::istringstream istr(processedFile.c_str());
|
|
|
|
std::ostringstream ostr;
|
|
|
|
std::string line;
|
2010-04-02 07:30:58 +02:00
|
|
|
while (std::getline(istr, line))
|
2009-07-22 20:11:27 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (line.compare(0, 4, "#if ") == 0 || line.compare(0, 6, "#elif ") == 0)
|
2009-07-22 20:11:27 +02:00
|
|
|
{
|
|
|
|
std::string::size_type pos = 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
while ((pos = line.find(" defined ")) != std::string::npos)
|
2009-07-22 20:11:27 +02:00
|
|
|
{
|
|
|
|
line[pos+8] = '(';
|
|
|
|
pos = line.find_first_of(" |&", pos + 8);
|
2010-04-02 07:30:58 +02:00
|
|
|
if (pos == std::string::npos)
|
2009-07-22 20:11:27 +02:00
|
|
|
line += ")";
|
|
|
|
else
|
|
|
|
line.insert(pos, ")");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ostr << line << "\n";
|
|
|
|
}
|
|
|
|
processedFile = ostr.str();
|
|
|
|
}
|
|
|
|
|
2009-01-24 20:28:30 +01:00
|
|
|
handleIncludes(processedFile, filename, includePaths);
|
|
|
|
|
|
|
|
processedFile = replaceIfDefined(processedFile);
|
|
|
|
|
|
|
|
// Get all possible configurations..
|
2010-05-21 09:49:57 +02:00
|
|
|
if (!_settings || (_settings && _settings->userDefines.empty()))
|
|
|
|
resultConfigurations = getcfgs(processedFile, filename);
|
2009-01-24 20:28:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Get the DEF in this line: "#ifdef DEF"
|
|
|
|
std::string Preprocessor::getdef(std::string line, bool def)
|
|
|
|
{
|
|
|
|
// If def is true, the line must start with "#ifdef"
|
2010-04-02 07:30:58 +02:00
|
|
|
if (def && line.find("#ifdef ") != 0 && line.find("#if ") != 0 && line.find("#elif ") != 0 && line.find("#if defined ") != 0)
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
// If def is false, the line must start with "#ifndef"
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!def && line.find("#ifndef ") != 0)
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the "#ifdef" or "#ifndef"
|
2010-04-02 07:30:58 +02:00
|
|
|
if (line.find("#if defined ") == 0)
|
2009-06-14 22:37:18 +02:00
|
|
|
line.erase(0, 11);
|
|
|
|
else
|
|
|
|
line.erase(0, line.find(" "));
|
2009-01-24 20:28:30 +01:00
|
|
|
|
|
|
|
// Remove all spaces.
|
2009-12-11 19:28:37 +01:00
|
|
|
std::string::size_type pos = 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
while ((pos = line.find(" ", pos)) != std::string::npos)
|
2009-12-11 19:28:37 +01:00
|
|
|
{
|
2009-12-20 09:35:51 +01:00
|
|
|
const unsigned char chprev = (pos > 0) ? line[pos-1] : (unsigned char)0;
|
|
|
|
const unsigned char chnext = (pos + 1 < line.length()) ? line[pos+1] : (unsigned char)0;
|
2010-04-02 07:30:58 +02:00
|
|
|
if ((std::isalnum(chprev) || chprev == '_') && (std::isalnum(chnext) || chnext == '_'))
|
2009-12-11 19:28:37 +01:00
|
|
|
++pos;
|
|
|
|
else
|
|
|
|
line.erase(pos, 1);
|
|
|
|
}
|
2009-01-24 20:28:30 +01:00
|
|
|
|
|
|
|
// The remaining string is our result.
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-11-20 19:18:57 +01:00
|
|
|
std::list<std::string> Preprocessor::getcfgs(const std::string &filedata, const std::string &filename)
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
|
|
|
std::list<std::string> ret;
|
|
|
|
ret.push_back("");
|
|
|
|
|
2010-03-19 16:13:45 +01:00
|
|
|
std::list<std::string> deflist, ndeflist;
|
2009-01-24 20:28:30 +01:00
|
|
|
|
2009-07-22 18:47:50 +02:00
|
|
|
// constants defined through "#define" in the code..
|
|
|
|
std::set<std::string> defines;
|
|
|
|
|
2009-01-24 20:28:30 +01:00
|
|
|
// How deep into included files are we currently parsing?
|
|
|
|
// 0=>Source file, 1=>Included by source file, 2=>included by header that was included by source file, etc
|
|
|
|
int filelevel = 0;
|
|
|
|
|
2010-02-06 09:30:48 +01:00
|
|
|
bool includeguard = false;
|
|
|
|
|
2009-11-20 19:18:57 +01:00
|
|
|
unsigned int linenr = 0;
|
2009-01-24 20:28:30 +01:00
|
|
|
std::istringstream istr(filedata);
|
|
|
|
std::string line;
|
2010-04-02 07:30:58 +02:00
|
|
|
while (getline(istr, line))
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
2009-11-20 19:18:57 +01:00
|
|
|
++linenr;
|
|
|
|
|
2010-07-25 18:19:37 +02:00
|
|
|
if (_errorLogger)
|
2010-08-03 16:36:21 +02:00
|
|
|
_errorLogger->reportProgress(filename, "Preprocessing (get configurations 1)", 0);
|
|
|
|
|
2010-07-25 18:19:37 +02:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (line.compare(0, 6, "#file ") == 0)
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
2010-02-06 09:30:48 +01:00
|
|
|
includeguard = true;
|
2009-01-24 20:28:30 +01:00
|
|
|
++filelevel;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (line == "#endfile")
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
2010-02-06 09:30:48 +01:00
|
|
|
includeguard = false;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (filelevel > 0)
|
2009-01-24 20:28:30 +01:00
|
|
|
--filelevel;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-07-25 15:19:25 +02:00
|
|
|
if (line.compare(0, 8, "#define ") == 0)
|
2009-07-22 18:47:50 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (line.find(" ", 8) == std::string::npos)
|
2009-07-25 13:58:34 +02:00
|
|
|
defines.insert(line.substr(8));
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::string s = line.substr(8);
|
|
|
|
s[s.find(" ")] = '=';
|
|
|
|
defines.insert(s);
|
|
|
|
}
|
2009-07-22 18:47:50 +02:00
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!line.empty() && line.compare(0, 3, "#if") != 0)
|
2010-02-06 09:30:48 +01:00
|
|
|
includeguard = false;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (includeguard)
|
2009-01-24 20:28:30 +01:00
|
|
|
continue;
|
|
|
|
|
2010-03-19 16:13:45 +01:00
|
|
|
bool from_negation = false;
|
|
|
|
|
|
|
|
std::string def = getdef(line, true);
|
2010-04-02 07:30:58 +02:00
|
|
|
if (def.empty())
|
2010-03-19 16:13:45 +01:00
|
|
|
{
|
|
|
|
def = getdef(line, false);
|
|
|
|
// sub conditionals of ndef blocks need to be
|
|
|
|
// constructed _without_ the negated define
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!def.empty())
|
2010-03-19 16:13:45 +01:00
|
|
|
from_negation = true;
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!def.empty())
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
2009-11-20 19:18:57 +01:00
|
|
|
int par = 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
for (std::string::size_type pos = 0; pos < def.length(); ++pos)
|
2009-11-20 19:18:57 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (def[pos] == '(')
|
2009-11-20 19:18:57 +01:00
|
|
|
++par;
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (def[pos] == ')')
|
2009-11-20 19:18:57 +01:00
|
|
|
{
|
|
|
|
--par;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (par < 0)
|
2009-11-20 19:18:57 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
if (par != 0)
|
2009-11-20 19:18:57 +01:00
|
|
|
{
|
2010-04-09 21:40:37 +02:00
|
|
|
std::ostringstream lineStream;
|
|
|
|
lineStream << __LINE__;
|
2009-11-20 19:18:57 +01:00
|
|
|
|
|
|
|
ErrorLogger::ErrorMessage errmsg;
|
|
|
|
ErrorLogger::ErrorMessage::FileLocation loc;
|
2010-07-17 00:27:40 +02:00
|
|
|
loc.setfile(filename);
|
2009-11-20 19:18:57 +01:00
|
|
|
loc.line = linenr;
|
|
|
|
errmsg._callStack.push_back(loc);
|
2010-07-14 18:36:34 +02:00
|
|
|
errmsg._severity = Severity::fromString("error");
|
2009-11-20 19:18:57 +01:00
|
|
|
errmsg._msg = "mismatching number of '(' and ')' in this line: " + def;
|
2010-04-09 21:40:37 +02:00
|
|
|
errmsg._id = "preprocessor" + lineStream.str();
|
2009-11-20 19:18:57 +01:00
|
|
|
_errorLogger->reportErr(errmsg);
|
|
|
|
ret.clear();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-11-21 19:53:07 +01:00
|
|
|
// Replace defined constants
|
|
|
|
{
|
|
|
|
std::map<std::string, std::string> varmap;
|
2010-04-02 07:30:58 +02:00
|
|
|
for (std::set<std::string>::const_iterator it = defines.begin(); it != defines.end(); ++it)
|
2009-11-21 19:53:07 +01:00
|
|
|
{
|
2009-11-21 20:14:29 +01:00
|
|
|
std::string::size_type pos = it->find("=");
|
2010-04-02 07:30:58 +02:00
|
|
|
if (pos == std::string::npos)
|
2009-11-21 19:53:07 +01:00
|
|
|
continue;
|
2009-11-21 20:14:29 +01:00
|
|
|
const std::string varname(it->substr(0, pos));
|
|
|
|
const std::string value(it->substr(pos + 1));
|
2009-11-21 19:53:07 +01:00
|
|
|
varmap[varname] = value;
|
|
|
|
}
|
|
|
|
|
2009-12-13 17:18:27 +01:00
|
|
|
simplifyCondition(varmap, def, false);
|
2009-11-21 19:53:07 +01:00
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (! deflist.empty() && line.find("#elif ") == 0)
|
2009-01-24 20:28:30 +01:00
|
|
|
deflist.pop_back();
|
|
|
|
deflist.push_back(def);
|
|
|
|
def = "";
|
2010-04-15 18:37:51 +02:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
for (std::list<std::string>::const_iterator it = deflist.begin(); it != deflist.end(); ++it)
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (*it == "0")
|
2009-01-24 20:28:30 +01:00
|
|
|
break;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (*it == "1" || *it == "!")
|
2009-01-24 20:28:30 +01:00
|
|
|
continue;
|
2009-09-05 19:01:57 +02:00
|
|
|
|
|
|
|
// don't add "T;T":
|
|
|
|
// treat two and more similar nested conditions as one
|
2010-04-02 07:30:58 +02:00
|
|
|
if (def != *it)
|
2009-09-05 19:01:57 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (! def.empty())
|
2009-09-05 19:01:57 +02:00
|
|
|
def += ";";
|
|
|
|
def += *it;
|
|
|
|
}
|
2009-01-24 20:28:30 +01:00
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
if (from_negation)
|
2010-03-19 16:13:45 +01:00
|
|
|
{
|
|
|
|
ndeflist.push_back(deflist.back());
|
|
|
|
deflist.pop_back();
|
|
|
|
std::string nmark("!");
|
|
|
|
deflist.push_back(nmark);
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (std::find(ret.begin(), ret.end(), def) == ret.end())
|
2010-03-19 16:13:45 +01:00
|
|
|
{
|
2009-01-24 20:28:30 +01:00
|
|
|
ret.push_back(def);
|
2010-03-19 16:13:45 +01:00
|
|
|
}
|
2009-01-24 20:28:30 +01:00
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (line.find("#else") == 0 && ! deflist.empty())
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (deflist.back() == "!")
|
2010-03-19 16:13:45 +01:00
|
|
|
{
|
|
|
|
deflist.pop_back();
|
|
|
|
deflist.push_back(ndeflist.back());
|
|
|
|
ndeflist.pop_back();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-04-09 21:40:37 +02:00
|
|
|
std::string tempDef((deflist.back() == "1") ? "0" : "1");
|
2010-03-19 16:13:45 +01:00
|
|
|
deflist.pop_back();
|
2010-04-09 21:40:37 +02:00
|
|
|
deflist.push_back(tempDef);
|
2010-03-19 16:13:45 +01:00
|
|
|
}
|
2009-01-24 20:28:30 +01:00
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (line.find("#endif") == 0 && ! deflist.empty())
|
2010-03-19 16:13:45 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (deflist.back() == "!")
|
2010-03-19 16:13:45 +01:00
|
|
|
ndeflist.pop_back();
|
2009-01-24 20:28:30 +01:00
|
|
|
deflist.pop_back();
|
2010-03-19 16:13:45 +01:00
|
|
|
}
|
2009-01-24 20:28:30 +01:00
|
|
|
}
|
|
|
|
|
2009-07-22 18:47:50 +02:00
|
|
|
// Remove defined constants from ifdef configurations..
|
2010-08-03 16:36:21 +02:00
|
|
|
unsigned int count = 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
for (std::list<std::string>::iterator it = ret.begin(); it != ret.end(); ++it)
|
2009-07-22 18:47:50 +02:00
|
|
|
{
|
2010-07-25 18:19:37 +02:00
|
|
|
if (_errorLogger)
|
2010-08-03 16:36:21 +02:00
|
|
|
_errorLogger->reportProgress(filename, "Preprocessing (get configurations 2)", (100 * count++) / ret.size());
|
2010-07-25 18:19:37 +02:00
|
|
|
|
2009-07-25 13:58:34 +02:00
|
|
|
std::string cfg(*it);
|
2010-04-02 07:30:58 +02:00
|
|
|
for (std::set<std::string>::const_iterator it2 = defines.begin(); it2 != defines.end(); ++it2)
|
2009-07-22 18:47:50 +02:00
|
|
|
{
|
|
|
|
std::string::size_type pos = 0;
|
2009-07-25 13:58:34 +02:00
|
|
|
|
|
|
|
// Get name of define
|
|
|
|
std::string defineName(*it2);
|
2010-04-02 07:30:58 +02:00
|
|
|
if (defineName.find("=") != std::string::npos)
|
2009-07-25 13:58:34 +02:00
|
|
|
defineName.erase(defineName.find("="));
|
|
|
|
|
|
|
|
// Remove ifdef configurations that match the defineName
|
2010-04-02 07:30:58 +02:00
|
|
|
while ((pos = cfg.find(defineName, pos)) != std::string::npos)
|
2009-07-22 18:47:50 +02:00
|
|
|
{
|
|
|
|
std::string::size_type pos1 = pos;
|
|
|
|
++pos;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (pos1 > 0 && cfg[pos1-1] != ';')
|
2009-07-22 18:47:50 +02:00
|
|
|
continue;
|
2009-07-25 13:58:34 +02:00
|
|
|
std::string::size_type pos2 = pos1 + defineName.length();
|
2010-04-02 07:30:58 +02:00
|
|
|
if (pos2 < cfg.length() && cfg[pos2] != ';')
|
2009-07-22 18:47:50 +02:00
|
|
|
continue;
|
|
|
|
--pos;
|
2009-07-25 13:58:34 +02:00
|
|
|
cfg.erase(pos, defineName.length());
|
2009-07-22 18:47:50 +02:00
|
|
|
}
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
if (cfg.length() != it->length())
|
2009-07-22 18:47:50 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
while (cfg.length() > 0 && cfg[0] == ';')
|
2009-07-25 13:58:34 +02:00
|
|
|
cfg.erase(0, 1);
|
2009-07-22 18:47:50 +02:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
while (cfg.length() > 0 && cfg[cfg.length()-1] == ';')
|
2009-07-25 13:58:34 +02:00
|
|
|
cfg.erase(cfg.length() - 1);
|
2009-07-22 18:47:50 +02:00
|
|
|
|
|
|
|
std::string::size_type pos = 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
while ((pos = cfg.find(";;", pos)) != std::string::npos)
|
2009-07-25 13:58:34 +02:00
|
|
|
cfg.erase(pos, 1);
|
2009-07-22 18:47:50 +02:00
|
|
|
|
2009-07-25 13:58:34 +02:00
|
|
|
*it = cfg;
|
2009-07-22 18:47:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-26 13:19:55 +02:00
|
|
|
// convert configurations: "defined(A) && defined(B)" => "A;B"
|
2010-04-02 07:30:58 +02:00
|
|
|
for (std::list<std::string>::iterator it = ret.begin(); it != ret.end(); ++it)
|
2009-06-26 13:19:55 +02:00
|
|
|
{
|
|
|
|
std::string s(*it);
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (s.find("&&") != std::string::npos)
|
2009-06-26 13:19:55 +02:00
|
|
|
{
|
2009-11-20 19:18:57 +01:00
|
|
|
Tokenizer tokenizer(_settings, _errorLogger);
|
2010-04-09 21:40:37 +02:00
|
|
|
std::istringstream tempIstr(s.c_str());
|
|
|
|
if (!tokenizer.tokenize(tempIstr, filename.c_str()))
|
2009-11-09 00:11:52 +01:00
|
|
|
{
|
2010-04-09 21:40:37 +02:00
|
|
|
std::ostringstream lineStream;
|
|
|
|
lineStream << __LINE__;
|
2009-11-20 19:18:57 +01:00
|
|
|
|
|
|
|
ErrorLogger::ErrorMessage errmsg;
|
|
|
|
ErrorLogger::ErrorMessage::FileLocation loc;
|
2010-07-17 00:27:40 +02:00
|
|
|
loc.setfile(filename);
|
2009-11-20 19:18:57 +01:00
|
|
|
loc.line = 1;
|
|
|
|
errmsg._callStack.push_back(loc);
|
2010-07-14 18:36:34 +02:00
|
|
|
errmsg._severity = Severity::fromString("error");
|
2009-11-20 19:18:57 +01:00
|
|
|
errmsg._msg = "Error parsing this: " + s;
|
2010-04-09 21:40:37 +02:00
|
|
|
errmsg._id = "preprocessor" + lineStream.str();
|
2009-11-20 19:18:57 +01:00
|
|
|
_errorLogger->reportErr(errmsg);
|
2009-11-09 00:11:52 +01:00
|
|
|
}
|
2009-06-26 13:19:55 +02:00
|
|
|
|
2009-08-29 23:00:54 +02:00
|
|
|
|
2009-06-26 13:19:55 +02:00
|
|
|
const Token *tok = tokenizer.tokens();
|
2009-08-29 23:00:54 +02:00
|
|
|
std::list<std::string> varList;
|
2010-04-02 07:30:58 +02:00
|
|
|
while (tok)
|
2009-06-26 13:19:55 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok, "defined ( %var% )"))
|
2009-06-26 13:19:55 +02:00
|
|
|
{
|
2009-08-29 23:00:54 +02:00
|
|
|
varList.push_back(tok->strAt(2));
|
2009-06-26 13:19:55 +02:00
|
|
|
tok = tok->tokAt(4);
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok && tok->str() == "&&")
|
2009-06-26 13:19:55 +02:00
|
|
|
{
|
|
|
|
tok = tok->next();
|
|
|
|
}
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(tok, "%var% ;"))
|
2009-06-26 13:19:55 +02:00
|
|
|
{
|
2009-08-29 23:00:54 +02:00
|
|
|
varList.push_back(tok->str());
|
2009-06-26 13:19:55 +02:00
|
|
|
tok = tok->tokAt(2);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-29 23:00:54 +02:00
|
|
|
varList.sort();
|
2010-04-15 18:37:51 +02:00
|
|
|
s = join(varList, ';');
|
2009-08-29 23:00:54 +02:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!s.empty())
|
2009-06-26 13:19:55 +02:00
|
|
|
*it = s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-15 18:37:51 +02:00
|
|
|
// Convert configurations into a canonical form: B;C;A or C;A;B => A;B;C
|
|
|
|
for (std::list<std::string>::iterator it = ret.begin(); it != ret.end(); ++it)
|
|
|
|
{
|
|
|
|
// Split the configuration into a list of defines
|
|
|
|
std::list<std::string> defs = split(*it, ';');
|
|
|
|
|
|
|
|
// Re-constitute the configuration after sorting the defines
|
|
|
|
defs.sort();
|
|
|
|
*it = join(defs, ';');
|
|
|
|
}
|
|
|
|
|
2009-08-29 23:00:54 +02:00
|
|
|
// Remove duplicates from the ret list..
|
2009-08-30 10:47:24 +02:00
|
|
|
ret.sort();
|
|
|
|
ret.unique();
|
2009-08-29 23:00:54 +02:00
|
|
|
|
2009-06-26 13:19:55 +02:00
|
|
|
// cleanup unhandled configurations..
|
2010-04-02 07:30:58 +02:00
|
|
|
for (std::list<std::string>::iterator it = ret.begin(); it != ret.end();)
|
2009-06-26 13:19:55 +02:00
|
|
|
{
|
2009-12-13 15:23:44 +01:00
|
|
|
const std::string s(*it + ";");
|
|
|
|
|
|
|
|
bool unhandled = false;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
for (std::string::size_type pos = 0; pos < s.length(); ++pos)
|
2009-12-13 15:23:44 +01:00
|
|
|
{
|
2010-08-06 19:38:21 +02:00
|
|
|
const unsigned char c = static_cast<unsigned char>(s[pos]);
|
2009-12-13 15:23:44 +01:00
|
|
|
|
|
|
|
// ok with ";"
|
2010-04-02 07:30:58 +02:00
|
|
|
if (c == ';')
|
2009-12-13 15:23:44 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// identifier..
|
2010-04-02 07:30:58 +02:00
|
|
|
if (std::isalpha(c) || c == '_')
|
2009-12-13 15:23:44 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
while (std::isalnum(s[pos]) || s[pos] == '_')
|
2009-12-13 15:23:44 +01:00
|
|
|
++pos;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (s[pos] == '=')
|
2009-12-13 15:23:44 +01:00
|
|
|
{
|
|
|
|
++pos;
|
2010-04-02 07:30:58 +02:00
|
|
|
while (std::isdigit(s[pos]))
|
2009-12-13 15:23:44 +01:00
|
|
|
++pos;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (s[pos] != ';')
|
2009-12-13 15:23:44 +01:00
|
|
|
{
|
|
|
|
unhandled = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
--pos;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// not ok..
|
|
|
|
else
|
|
|
|
{
|
|
|
|
unhandled = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (unhandled)
|
2009-06-26 13:19:55 +02:00
|
|
|
{
|
|
|
|
// unhandled ifdef configuration..
|
2010-04-02 07:30:58 +02:00
|
|
|
if (_errorLogger && _settings && _settings->_debug)
|
2009-12-13 15:23:44 +01:00
|
|
|
_errorLogger->reportOut("unhandled configuration: " + *it);
|
2009-06-26 13:19:55 +02:00
|
|
|
|
|
|
|
ret.erase(it++);
|
|
|
|
}
|
|
|
|
else
|
2009-12-13 15:23:44 +01:00
|
|
|
{
|
2009-06-26 13:19:55 +02:00
|
|
|
++it;
|
2009-12-13 15:23:44 +01:00
|
|
|
}
|
2009-06-26 13:19:55 +02:00
|
|
|
}
|
|
|
|
|
2009-01-24 20:28:30 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-13 17:18:27 +01:00
|
|
|
void Preprocessor::simplifyCondition(const std::map<std::string, std::string> &variables, std::string &condition, bool match)
|
2009-11-21 19:53:07 +01:00
|
|
|
{
|
|
|
|
Tokenizer tokenizer;
|
2009-12-11 19:28:37 +01:00
|
|
|
std::istringstream istr(("(" + condition + ")").c_str());
|
2009-11-21 19:53:07 +01:00
|
|
|
tokenizer.tokenize(istr, "");
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tokenizer.tokens(), "( %var% )"))
|
2010-03-04 18:51:25 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (variables.find(tokenizer.tokens()->strAt(1)) != variables.end())
|
2010-03-04 18:51:25 +01:00
|
|
|
condition = "1";
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (match)
|
2010-03-04 18:51:25 +01:00
|
|
|
condition = "0";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tokenizer.tokens(), "( ! %var% )"))
|
2010-03-04 18:51:25 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (variables.find(tokenizer.tokens()->strAt(2)) == variables.end())
|
2010-03-04 18:51:25 +01:00
|
|
|
condition = "1";
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (match)
|
2010-03-04 18:51:25 +01:00
|
|
|
condition = "0";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-11-21 19:53:07 +01:00
|
|
|
// replace variable names with values..
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = const_cast<Token *>(tokenizer.tokens()); tok; tok = tok->next())
|
2009-11-21 19:53:07 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!tok->isName())
|
2009-12-11 19:28:37 +01:00
|
|
|
continue;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok, "defined ( %var% )"))
|
2009-12-11 19:28:37 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (variables.find(tok->strAt(2)) != variables.end())
|
2009-12-13 17:18:27 +01:00
|
|
|
tok->str("1");
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (match)
|
2009-12-11 19:28:37 +01:00
|
|
|
tok->str("0");
|
|
|
|
else
|
2009-12-13 17:18:27 +01:00
|
|
|
continue;
|
2009-12-11 19:28:37 +01:00
|
|
|
tok->deleteNext();
|
|
|
|
tok->deleteNext();
|
|
|
|
tok->deleteNext();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok, "defined %var%"))
|
2009-12-11 19:28:37 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (variables.find(tok->strAt(1)) != variables.end())
|
2009-12-13 17:18:27 +01:00
|
|
|
tok->str("1");
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (match)
|
2009-12-11 19:28:37 +01:00
|
|
|
tok->str("0");
|
|
|
|
else
|
2009-12-13 17:18:27 +01:00
|
|
|
continue;
|
2009-12-11 19:28:37 +01:00
|
|
|
tok->deleteNext();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-11-21 19:53:07 +01:00
|
|
|
const std::map<std::string, std::string>::const_iterator it = variables.find(tok->str());
|
2010-04-02 07:30:58 +02:00
|
|
|
if (it != variables.end())
|
2009-11-28 11:47:44 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (it->second.empty())
|
2009-11-28 11:47:44 +01:00
|
|
|
tok->deleteThis();
|
|
|
|
else
|
|
|
|
tok->str(it->second);
|
|
|
|
}
|
2009-11-21 19:53:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// simplify calculations..
|
2009-12-11 22:32:44 +01:00
|
|
|
bool modified = true;
|
2010-04-02 07:30:58 +02:00
|
|
|
while (modified)
|
2009-12-11 22:32:44 +01:00
|
|
|
{
|
|
|
|
modified = false;
|
|
|
|
tokenizer.simplifyCalculations();
|
2010-04-02 07:30:58 +02:00
|
|
|
for (Token *tok = const_cast<Token *>(tokenizer.tokens()); tok; tok = tok->next())
|
2009-12-11 22:32:44 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok, "! %num%"))
|
2009-12-11 22:32:44 +01:00
|
|
|
{
|
|
|
|
tok->deleteThis();
|
|
|
|
tok->str(tok->str() == "0" ? "1" : "0");
|
|
|
|
modified = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-11-21 19:53:07 +01:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::simpleMatch(tokenizer.tokens(), "( 1 )") ||
|
|
|
|
Token::simpleMatch(tokenizer.tokens(), "( 1 ||"))
|
2009-12-11 19:28:37 +01:00
|
|
|
condition = "1";
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::simpleMatch(tokenizer.tokens(), "( 0 )"))
|
2009-12-11 19:28:37 +01:00
|
|
|
condition = "0";
|
2009-11-21 19:53:07 +01:00
|
|
|
}
|
|
|
|
|
2009-07-30 10:10:34 +02:00
|
|
|
bool Preprocessor::match_cfg_def(const std::map<std::string, std::string> &cfg, std::string def)
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
2009-06-26 13:19:55 +02:00
|
|
|
//std::cout << "cfg: \"" << cfg << "\" ";
|
|
|
|
//std::cout << "def: \"" << def << "\"";
|
|
|
|
|
2009-12-13 17:18:27 +01:00
|
|
|
simplifyCondition(cfg, def, true);
|
2009-11-21 19:53:07 +01:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (cfg.find(def) != cfg.end())
|
2009-07-30 10:10:34 +02:00
|
|
|
return true;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (def == "0")
|
2009-01-24 20:28:30 +01:00
|
|
|
return false;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (def == "1")
|
2009-01-24 20:28:30 +01:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-03 21:09:12 +02:00
|
|
|
std::string Preprocessor::getcode(const std::string &filedata, std::string cfg, const std::string &filename, ErrorLogger *errorLogger)
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
|
|
|
std::ostringstream ret;
|
|
|
|
|
|
|
|
bool match = true;
|
|
|
|
std::list<bool> matching_ifdef;
|
|
|
|
std::list<bool> matched_ifdef;
|
|
|
|
|
2009-07-30 10:10:34 +02:00
|
|
|
// Create a map for the cfg for faster access to defines
|
|
|
|
std::map<std::string, std::string> cfgmap;
|
|
|
|
{
|
|
|
|
std::string::size_type pos = 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
for (;;)
|
2009-07-30 10:10:34 +02:00
|
|
|
{
|
|
|
|
std::string::size_type pos2 = cfg.find_first_of(";=", pos);
|
2010-04-02 07:30:58 +02:00
|
|
|
if (pos2 == std::string::npos)
|
2009-07-30 10:10:34 +02:00
|
|
|
{
|
|
|
|
cfgmap[cfg.substr(pos)] = "";
|
|
|
|
break;
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
if (cfg[pos2] == ';')
|
2009-07-30 10:10:34 +02:00
|
|
|
{
|
|
|
|
cfgmap[cfg.substr(pos, pos2-pos)] = "";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::string::size_type pos3 = pos2;
|
|
|
|
pos2 = cfg.find(";", pos2);
|
2010-04-02 07:30:58 +02:00
|
|
|
if (pos2 == std::string::npos)
|
2009-07-30 10:10:34 +02:00
|
|
|
{
|
|
|
|
cfgmap[cfg.substr(pos, pos3-pos)] = cfg.substr(pos3 + 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cfgmap[cfg.substr(pos, pos3-pos)] = cfg.substr(pos3 + 1, pos2 - pos3 - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pos = pos2 + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-24 20:28:30 +01:00
|
|
|
std::istringstream istr(filedata);
|
|
|
|
std::string line;
|
2010-04-02 07:30:58 +02:00
|
|
|
while (getline(istr, line))
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (line.compare(0, 11, "#pragma asm") == 0)
|
2009-08-10 20:07:55 +02:00
|
|
|
{
|
|
|
|
ret << "\n";
|
|
|
|
bool found_end = false;
|
2010-04-02 07:30:58 +02:00
|
|
|
while (getline(istr, line))
|
2009-08-10 20:07:55 +02:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (line.compare(0, 14, "#pragma endasm") == 0)
|
2009-08-10 20:07:55 +02:00
|
|
|
{
|
|
|
|
found_end = true;
|
|
|
|
break;
|
|
|
|
}
|
2010-02-12 18:15:15 +01:00
|
|
|
|
|
|
|
ret << "\n";
|
2009-08-10 20:07:55 +02:00
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!found_end)
|
2009-08-10 20:07:55 +02:00
|
|
|
break;
|
2010-02-12 18:15:15 +01:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (line.find("=") != std::string::npos)
|
2010-02-12 18:15:15 +01:00
|
|
|
{
|
|
|
|
Tokenizer tokenizer;
|
|
|
|
line.erase(0, sizeof("#pragma endasm"));
|
2010-04-09 21:40:37 +02:00
|
|
|
std::istringstream tempIstr(line.c_str());
|
|
|
|
tokenizer.tokenize(tempIstr, "");
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tokenizer.tokens(), "( %var% = %any% )"))
|
2010-02-12 18:15:15 +01:00
|
|
|
{
|
|
|
|
ret << "asm(" << tokenizer.tokens()->strAt(1) << ");";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret << "\n";
|
|
|
|
|
2009-08-10 20:07:55 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-01-24 20:28:30 +01:00
|
|
|
std::string def = getdef(line, true);
|
|
|
|
std::string ndef = getdef(line, false);
|
|
|
|
|
2010-07-25 15:19:25 +02:00
|
|
|
if (line.compare(0, 8, "#define ") == 0)
|
2009-07-22 18:47:50 +02:00
|
|
|
{
|
2010-07-25 15:19:25 +02:00
|
|
|
std::string::size_type pos = line.find_first_of(" (", 8);
|
2010-04-02 07:30:58 +02:00
|
|
|
if (pos == std::string::npos)
|
2009-07-30 10:10:34 +02:00
|
|
|
cfgmap[line.substr(8)] = "";
|
2010-07-25 15:19:25 +02:00
|
|
|
else if (line[pos] == ' ')
|
2009-07-30 10:10:34 +02:00
|
|
|
cfgmap[line.substr(8, pos - 8)] = line.substr(pos + 1);
|
2010-07-25 15:19:25 +02:00
|
|
|
else
|
|
|
|
cfgmap[line.substr(8, pos - 8)] = "";
|
2009-07-22 18:47:50 +02:00
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (line.find("#elif ") == 0)
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (matched_ifdef.back())
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
|
|
|
matching_ifdef.back() = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (match_cfg_def(cfgmap, def))
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
|
|
|
matching_ifdef.back() = true;
|
|
|
|
matched_ifdef.back() = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (! def.empty())
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
2009-07-30 10:10:34 +02:00
|
|
|
matching_ifdef.push_back(match_cfg_def(cfgmap, def));
|
2009-01-24 20:28:30 +01:00
|
|
|
matched_ifdef.push_back(matching_ifdef.back());
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (! ndef.empty())
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
2009-07-30 10:10:34 +02:00
|
|
|
matching_ifdef.push_back(! match_cfg_def(cfgmap, ndef));
|
2009-01-24 20:28:30 +01:00
|
|
|
matched_ifdef.push_back(matching_ifdef.back());
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (line == "#else")
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (! matched_ifdef.empty())
|
2009-01-24 20:28:30 +01:00
|
|
|
matching_ifdef.back() = ! matched_ifdef.back();
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (line.compare(0, 6, "#endif") == 0)
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (! matched_ifdef.empty())
|
2009-01-24 20:28:30 +01:00
|
|
|
matched_ifdef.pop_back();
|
2010-04-02 07:30:58 +02:00
|
|
|
if (! matching_ifdef.empty())
|
2009-01-24 20:28:30 +01:00
|
|
|
matching_ifdef.pop_back();
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!line.empty() && line[0] == '#')
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
|
|
|
match = true;
|
2010-04-02 07:30:58 +02:00
|
|
|
for (std::list<bool>::const_iterator it = matching_ifdef.begin(); it != matching_ifdef.end(); ++it)
|
2009-01-24 20:28:30 +01:00
|
|
|
match &= bool(*it);
|
|
|
|
}
|
|
|
|
|
2009-11-13 22:12:29 +01:00
|
|
|
// #error => return ""
|
2010-04-02 07:30:58 +02:00
|
|
|
if (match && line.compare(0, 6, "#error") == 0)
|
2009-11-13 22:12:29 +01:00
|
|
|
return "";
|
2009-05-17 18:51:29 +02:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!match && line.compare(0, 8, "#define ") == 0)
|
2009-05-17 18:51:29 +02:00
|
|
|
{
|
|
|
|
// Remove define that is not part of this configuration
|
|
|
|
line = "";
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (line.compare(0, 7, "#file \"") == 0 ||
|
|
|
|
line.compare(0, 8, "#endfile") == 0 ||
|
|
|
|
line.compare(0, 8, "#define ") == 0 ||
|
|
|
|
line.compare(0, 6, "#undef") == 0)
|
2009-03-08 08:45:53 +01:00
|
|
|
{
|
|
|
|
// We must not remove #file tags or line numbers
|
|
|
|
// are corrupted. File tags are removed by the tokenizer.
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (!match || line.compare(0, 1, "#") == 0)
|
2009-03-15 13:23:12 +01:00
|
|
|
{
|
|
|
|
// Remove #if, #else, #pragma etc, leaving only
|
2009-06-14 06:21:20 +02:00
|
|
|
// #define, #undef, #file and #endfile. and also lines
|
2009-03-15 13:23:12 +01:00
|
|
|
// which are not part of this configuration.
|
2009-01-24 20:28:30 +01:00
|
|
|
line = "";
|
2009-03-15 13:23:12 +01:00
|
|
|
}
|
2009-01-24 20:28:30 +01:00
|
|
|
|
|
|
|
ret << line << "\n";
|
|
|
|
}
|
|
|
|
|
2009-04-03 21:09:12 +02:00
|
|
|
return expandMacros(ret.str(), filename, errorLogger);
|
2009-01-24 20:28:30 +01:00
|
|
|
}
|
|
|
|
|
2010-07-21 11:50:04 +02:00
|
|
|
Preprocessor::HeaderTypes Preprocessor::getHeaderFileName(std::string &str)
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
|
|
|
std::string result;
|
2009-05-22 22:59:07 +02:00
|
|
|
std::string::size_type i = str.find_first_of("<\"");
|
2010-04-02 07:30:58 +02:00
|
|
|
if (i == std::string::npos)
|
2009-05-22 23:18:48 +02:00
|
|
|
{
|
|
|
|
str = "";
|
2010-07-21 11:50:04 +02:00
|
|
|
return NoHeader;
|
2009-05-22 23:18:48 +02:00
|
|
|
}
|
2009-01-24 20:28:30 +01:00
|
|
|
|
2010-08-06 19:38:21 +02:00
|
|
|
char c = str[i];
|
2010-04-02 07:30:58 +02:00
|
|
|
if (c == '<')
|
2009-05-22 22:59:07 +02:00
|
|
|
c = '>';
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
for (i = i + 1; i < str.length(); ++i)
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (str[i] == c)
|
2009-01-24 20:28:30 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
result.append(1, str[i]);
|
|
|
|
}
|
|
|
|
|
2010-07-21 13:16:42 +02:00
|
|
|
// Linux can't open include paths with \ separator, so fix them
|
|
|
|
std::replace(result.begin(), result.end(), '\\', '/');
|
|
|
|
|
2009-05-22 23:18:48 +02:00
|
|
|
str = result;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (c == '"')
|
2010-07-21 11:50:04 +02:00
|
|
|
return UserHeader;
|
2009-05-22 23:18:48 +02:00
|
|
|
else
|
2010-07-21 11:50:04 +02:00
|
|
|
return SystemHeader;
|
2009-01-24 20:28:30 +01:00
|
|
|
}
|
|
|
|
|
2010-01-14 22:00:14 +01:00
|
|
|
|
|
|
|
// This wrapper exists because Sun's CC does not allow a static_cast
|
|
|
|
// from extern "C" int(*)(int) to int(*)(int).
|
2010-01-14 22:01:59 +01:00
|
|
|
static int tolowerWrapper(int c)
|
|
|
|
{
|
2010-01-14 22:00:14 +01:00
|
|
|
return std::tolower(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-09 21:40:37 +02:00
|
|
|
void Preprocessor::handleIncludes(std::string &code, const std::string &filePath, const std::list<std::string> &includePaths)
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
2009-04-05 20:14:02 +02:00
|
|
|
std::list<std::string> paths;
|
|
|
|
std::string path;
|
2010-04-09 21:40:37 +02:00
|
|
|
path = filePath;
|
2009-01-24 20:28:30 +01:00
|
|
|
path.erase(1 + path.find_last_of("\\/"));
|
2009-04-05 20:14:02 +02:00
|
|
|
paths.push_back(path);
|
2009-01-24 20:28:30 +01:00
|
|
|
std::string::size_type pos = 0;
|
2009-04-05 20:14:02 +02:00
|
|
|
std::string::size_type endfilePos = 0;
|
2010-01-14 22:04:58 +01:00
|
|
|
std::set<std::string> handledFiles;
|
2009-04-05 20:14:02 +02:00
|
|
|
endfilePos = pos;
|
2010-04-02 07:30:58 +02:00
|
|
|
while ((pos = code.find("#include", pos)) != std::string::npos)
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
|
|
|
// Accept only includes that are at the start of a line
|
2010-04-02 07:30:58 +02:00
|
|
|
if (pos > 0 && code[pos-1] != '\n')
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
|
|
|
pos += 8; // length of "#include"
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-04-05 20:14:02 +02:00
|
|
|
// If endfile is encountered, we have moved to a next file in our stack,
|
|
|
|
// so remove last path in our list.
|
2010-04-02 07:30:58 +02:00
|
|
|
while ((endfilePos = code.find("\n#endfile", endfilePos)) != std::string::npos && endfilePos < pos)
|
2009-04-05 20:14:02 +02:00
|
|
|
{
|
|
|
|
paths.pop_back();
|
2009-10-10 22:23:48 +02:00
|
|
|
endfilePos += 9; // size of #endfile
|
2009-04-05 20:14:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
endfilePos = pos;
|
2009-01-24 20:28:30 +01:00
|
|
|
std::string::size_type end = code.find("\n", pos);
|
|
|
|
std::string filename = code.substr(pos, end - pos);
|
|
|
|
|
|
|
|
// Remove #include clause
|
|
|
|
code.erase(pos, end - pos);
|
|
|
|
|
2010-07-21 11:50:04 +02:00
|
|
|
HeaderTypes headerType = getHeaderFileName(filename);
|
|
|
|
if (headerType == NoHeader)
|
2009-01-24 20:28:30 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// filename contains now a file name e.g. "menu.h"
|
|
|
|
std::string processedFile;
|
2009-10-31 12:46:03 +01:00
|
|
|
bool fileOpened = false;
|
2010-02-09 21:26:15 +01:00
|
|
|
std::ifstream fin;
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
2010-07-27 18:07:30 +02:00
|
|
|
std::list<std::string> includePaths2(includePaths);
|
|
|
|
includePaths2.push_front("");
|
|
|
|
for (std::list<std::string>::const_iterator iter = includePaths2.begin(); iter != includePaths2.end(); ++iter)
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
2010-07-27 18:07:30 +02:00
|
|
|
fin.open((*iter + filename).c_str());
|
|
|
|
if (fin.is_open())
|
|
|
|
{
|
|
|
|
filename = *iter + filename;
|
|
|
|
fileOpened = true;
|
|
|
|
break;
|
|
|
|
}
|
2010-02-23 20:28:40 +01:00
|
|
|
|
2010-07-27 18:07:30 +02:00
|
|
|
fin.clear();
|
|
|
|
}
|
2009-01-24 20:28:30 +01:00
|
|
|
}
|
|
|
|
|
2010-07-21 11:50:04 +02:00
|
|
|
if (headerType == UserHeader && !fileOpened)
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
2009-04-05 20:14:02 +02:00
|
|
|
filename = paths.back() + filename;
|
2010-07-21 13:16:42 +02:00
|
|
|
fin.open(filename.c_str());
|
2010-04-02 07:30:58 +02:00
|
|
|
if (fin.is_open())
|
2009-10-31 12:46:03 +01:00
|
|
|
{
|
|
|
|
fileOpened = true;
|
|
|
|
}
|
2009-01-24 20:28:30 +01:00
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (fileOpened)
|
2010-02-09 21:26:15 +01:00
|
|
|
{
|
2010-03-11 20:58:59 +01:00
|
|
|
std::string tempFile = getFileLister()->simplifyPath(filename.c_str());
|
2010-02-09 21:26:15 +01:00
|
|
|
std::transform(tempFile.begin(), tempFile.end(), tempFile.begin(), tolowerWrapper);
|
2010-04-02 07:30:58 +02:00
|
|
|
if (handledFiles.find(tempFile) != handledFiles.end())
|
2010-02-09 21:26:15 +01:00
|
|
|
{
|
|
|
|
// We have processed this file already once, skip
|
|
|
|
// it this time to avoid ethernal loop.
|
2010-02-14 20:55:21 +01:00
|
|
|
fin.close();
|
2010-02-09 21:26:15 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
handledFiles.insert(tempFile);
|
|
|
|
processedFile = Preprocessor::read(fin, filename, _settings);
|
|
|
|
fin.close();
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (processedFile.length() > 0)
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
|
|
|
// Replace all tabs with spaces..
|
|
|
|
std::replace(processedFile.begin(), processedFile.end(), '\t', ' ');
|
|
|
|
|
|
|
|
// Remove all indentation..
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!processedFile.empty() && processedFile[0] == ' ')
|
2009-01-24 20:28:30 +01:00
|
|
|
processedFile.erase(0, processedFile.find_first_not_of(" "));
|
|
|
|
|
|
|
|
// Remove space characters that are after or before new line character
|
|
|
|
processedFile = removeSpaceNearNL(processedFile);
|
|
|
|
processedFile = "#file \"" + filename + "\"\n" + processedFile + "\n#endfile";
|
|
|
|
code.insert(pos, processedFile);
|
|
|
|
|
2009-04-05 20:14:02 +02:00
|
|
|
path = filename;
|
|
|
|
path.erase(1 + path.find_last_of("\\/"));
|
|
|
|
paths.push_back(path);
|
2009-01-24 20:28:30 +01:00
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (!fileOpened)
|
2009-07-25 21:10:30 +02:00
|
|
|
{
|
2010-07-21 11:50:04 +02:00
|
|
|
if (headerType == UserHeader && _errorLogger && _settings && _settings->_verbose)
|
2009-07-25 21:10:30 +02:00
|
|
|
{
|
2010-07-21 16:00:12 +02:00
|
|
|
std::string fixedpath = Path::toNativeSeparators(filename);
|
|
|
|
_errorLogger->reportOut("Include file: \"" + fixedpath + "\" not found.");
|
2009-07-25 21:10:30 +02:00
|
|
|
}
|
|
|
|
}
|
2009-01-24 20:28:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-19 09:09:47 +02:00
|
|
|
/**
|
|
|
|
* Skip string in line. A string begins and ends with either a " or a '
|
|
|
|
* @param line the string
|
|
|
|
* @param pos in=start position of string, out=end position of string
|
|
|
|
*/
|
|
|
|
static void skipstring(const std::string &line, std::string::size_type &pos)
|
|
|
|
{
|
2010-08-06 19:38:21 +02:00
|
|
|
const char ch = line[pos];
|
2010-06-19 09:09:47 +02:00
|
|
|
|
|
|
|
++pos;
|
|
|
|
while (pos < line.size() && line[pos] != ch)
|
|
|
|
{
|
|
|
|
if (line[pos] == '\\')
|
|
|
|
++pos;
|
|
|
|
++pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief get parameters from code. For example 'foo(1,2)' => '1','2'
|
|
|
|
* @param line in: The code
|
|
|
|
* @param pos in: Position to the '('. out: Position to the ')'
|
|
|
|
* @param params out: The extracted parameters
|
|
|
|
* @param numberOfNewlines out: number of newlines in the macro call
|
|
|
|
* @param endFound out: was the end paranthesis found?
|
|
|
|
*/
|
|
|
|
static void getparams(const std::string &line,
|
2010-06-19 22:24:10 +02:00
|
|
|
std::string::size_type &pos,
|
2010-06-19 09:09:47 +02:00
|
|
|
std::vector<std::string> ¶ms,
|
|
|
|
unsigned int &numberOfNewlines,
|
|
|
|
bool &endFound)
|
|
|
|
{
|
|
|
|
params.clear();
|
|
|
|
numberOfNewlines = 0;
|
|
|
|
endFound = false;
|
|
|
|
|
2010-06-19 12:03:39 +02:00
|
|
|
if (line[pos] == ' ')
|
|
|
|
pos++;
|
2010-06-19 09:09:47 +02:00
|
|
|
|
2010-06-19 12:03:39 +02:00
|
|
|
if (line[pos] != '(')
|
2010-06-19 09:09:47 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
// parantheses level
|
|
|
|
int parlevel = 0;
|
|
|
|
|
2010-06-19 12:03:39 +02:00
|
|
|
// current parameter data
|
2010-06-19 09:09:47 +02:00
|
|
|
std::string par;
|
|
|
|
|
|
|
|
// scan for parameters..
|
2010-06-19 12:03:39 +02:00
|
|
|
for (; pos < line.length(); ++pos)
|
2010-06-19 09:09:47 +02:00
|
|
|
{
|
|
|
|
// increase paranthesis level
|
2010-06-19 12:03:39 +02:00
|
|
|
if (line[pos] == '(')
|
2010-06-19 09:09:47 +02:00
|
|
|
{
|
|
|
|
++parlevel;
|
|
|
|
if (parlevel == 1)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// decrease paranthesis level
|
2010-06-19 12:03:39 +02:00
|
|
|
else if (line[pos] == ')')
|
2010-06-19 09:09:47 +02:00
|
|
|
{
|
|
|
|
--parlevel;
|
|
|
|
if (parlevel <= 0)
|
|
|
|
{
|
|
|
|
endFound = true;
|
|
|
|
params.push_back(par);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// string
|
2010-06-19 12:03:39 +02:00
|
|
|
else if (line[pos] == '\"' || line[pos] == '\'')
|
2010-06-19 09:09:47 +02:00
|
|
|
{
|
2010-06-19 12:03:39 +02:00
|
|
|
const std::string::size_type p = pos;
|
|
|
|
skipstring(line, pos);
|
|
|
|
if (pos == line.length())
|
2010-06-19 09:09:47 +02:00
|
|
|
break;
|
2010-06-19 12:03:39 +02:00
|
|
|
par += line.substr(p, pos + 1 - p);
|
2010-06-19 09:09:47 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// count newlines. the expanded macro must have the same number of newlines
|
2010-06-19 12:03:39 +02:00
|
|
|
else if (line[pos] == '\n')
|
2010-06-19 09:09:47 +02:00
|
|
|
{
|
|
|
|
++numberOfNewlines;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// new parameter
|
2010-06-19 12:03:39 +02:00
|
|
|
if (parlevel == 1 && line[pos] == ',')
|
2010-06-19 09:09:47 +02:00
|
|
|
{
|
|
|
|
params.push_back(par);
|
|
|
|
par = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
// spaces are only added if needed
|
2010-06-19 12:03:39 +02:00
|
|
|
else if (line[pos] == ' ')
|
2010-06-19 09:09:47 +02:00
|
|
|
{
|
|
|
|
// Add space only if it is needed
|
|
|
|
if (par.size() && std::isalnum(par[par.length()-1]))
|
|
|
|
{
|
|
|
|
par += ' ';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// add character to current parameter
|
|
|
|
else if (parlevel >= 1)
|
|
|
|
{
|
2010-06-19 12:03:39 +02:00
|
|
|
par.append(1, line[pos]);
|
2010-06-19 09:09:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-08 17:14:34 +02:00
|
|
|
/** @brief Class that the preprocessor uses when it expands macros. This class represents a preprocessor macro */
|
|
|
|
class PreprocessorMacro
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
|
|
|
private:
|
2010-01-13 21:50:44 +01:00
|
|
|
/** tokens of this macro */
|
2009-01-24 20:28:30 +01:00
|
|
|
Tokenizer tokenizer;
|
2010-01-13 21:50:44 +01:00
|
|
|
|
|
|
|
/** macro parameters */
|
2009-01-24 20:28:30 +01:00
|
|
|
std::vector<std::string> _params;
|
2010-01-13 21:50:44 +01:00
|
|
|
|
|
|
|
/** name of macro */
|
2009-01-24 20:28:30 +01:00
|
|
|
std::string _name;
|
2010-01-13 21:50:44 +01:00
|
|
|
|
|
|
|
/** macro definition in plain text */
|
|
|
|
const std::string _macro;
|
|
|
|
|
|
|
|
/** does this macro take a variable number of parameters? */
|
2009-02-13 14:31:40 +01:00
|
|
|
bool _variadic;
|
2010-01-13 21:50:44 +01:00
|
|
|
|
|
|
|
/** prefix that is used by cppcheck to separate macro parameters. Always "__cppcheck__" */
|
2009-09-19 23:09:05 +02:00
|
|
|
const std::string _prefix;
|
2009-01-24 20:28:30 +01:00
|
|
|
|
2009-06-19 16:42:47 +02:00
|
|
|
/** The macro has parantheses but no parameters.. "AAA()" */
|
|
|
|
bool _nopar;
|
|
|
|
|
2009-10-10 22:01:33 +02:00
|
|
|
/** disabled assignment operator */
|
|
|
|
void operator=(const PreprocessorMacro &);
|
2010-06-19 12:03:39 +02:00
|
|
|
|
|
|
|
/** @brief expand inner macro */
|
|
|
|
std::vector<std::string> expandInnerMacros(const std::vector<std::string> ¶ms1,
|
|
|
|
const std::map<std::string, PreprocessorMacro *> ¯os) const
|
|
|
|
{
|
|
|
|
std::string innerMacroName;
|
|
|
|
|
|
|
|
// Is there an inner macro..
|
|
|
|
{
|
|
|
|
const Token *tok = Token::findmatch(tokens(), ")");
|
|
|
|
if (!Token::Match(tok, ") %var% ("))
|
|
|
|
return params1;
|
|
|
|
innerMacroName = tok->strAt(1);
|
|
|
|
tok = tok->tokAt(3);
|
|
|
|
unsigned int par = 0;
|
|
|
|
while (Token::Match(tok, "%var% ,|)"))
|
|
|
|
{
|
|
|
|
tok = tok->tokAt(2);
|
|
|
|
par++;
|
|
|
|
}
|
|
|
|
if (tok || par != params1.size())
|
|
|
|
return params1;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> params2(params1);
|
|
|
|
|
|
|
|
for (unsigned int ipar = 0; ipar < params1.size(); ++ipar)
|
|
|
|
{
|
|
|
|
const std::string s(innerMacroName + "(");
|
|
|
|
std::string param(params1[ipar]);
|
|
|
|
if (param.compare(0,s.length(),s)==0 && param[param.length()-1]==')')
|
|
|
|
{
|
|
|
|
std::vector<std::string> innerparams;
|
2010-06-19 22:24:10 +02:00
|
|
|
std::string::size_type pos = s.length() - 1;
|
2010-06-19 12:03:39 +02:00
|
|
|
unsigned int num = 0;
|
|
|
|
bool endFound = false;
|
|
|
|
getparams(param, pos, innerparams, num, endFound);
|
|
|
|
if (pos == param.length()-1 && num==0 && endFound && innerparams.size() == params1.size())
|
|
|
|
{
|
|
|
|
// Is inner macro defined?
|
|
|
|
std::map<std::string, PreprocessorMacro *>::const_iterator it = macros.find(innerMacroName);
|
|
|
|
if (it != macros.end())
|
|
|
|
{
|
|
|
|
// expand the inner macro
|
|
|
|
const PreprocessorMacro *innerMacro = it->second;
|
|
|
|
|
|
|
|
std::string innercode;
|
|
|
|
std::map<std::string,PreprocessorMacro *> innermacros = macros;
|
|
|
|
innermacros.erase(innerMacroName);
|
|
|
|
innerMacro->code(innerparams, innermacros, innercode);
|
|
|
|
params2[ipar] = innercode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return params2;
|
|
|
|
}
|
|
|
|
|
2009-01-24 20:28:30 +01:00
|
|
|
public:
|
2009-09-19 23:09:05 +02:00
|
|
|
/**
|
2010-03-14 07:48:44 +01:00
|
|
|
* @brief Constructor for PreprocessorMacro. This is the "setter"
|
|
|
|
* for this class - everything is setup here.
|
|
|
|
* @param macro The code after define, until end of line,
|
2009-09-19 23:09:05 +02:00
|
|
|
* e.g. "A(x) foo(x);"
|
|
|
|
*/
|
2009-07-08 17:14:34 +02:00
|
|
|
PreprocessorMacro(const std::string ¯o)
|
2010-04-15 20:08:51 +02:00
|
|
|
: _macro(macro), _prefix("__cppcheck__")
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
|
|
|
// Tokenize the macro to make it easier to handle
|
|
|
|
std::istringstream istr(macro.c_str());
|
2009-05-09 21:32:29 +02:00
|
|
|
tokenizer.createTokens(istr);
|
2009-01-24 20:28:30 +01:00
|
|
|
|
|
|
|
// macro name..
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tokens() && tokens()->isName())
|
2009-01-24 20:28:30 +01:00
|
|
|
_name = tokens()->str();
|
|
|
|
|
2010-01-13 21:50:44 +01:00
|
|
|
// initialize parameters to default values
|
2009-06-19 16:42:47 +02:00
|
|
|
_variadic = _nopar = false;
|
2009-02-13 14:31:40 +01:00
|
|
|
|
2009-01-24 20:28:30 +01:00
|
|
|
std::string::size_type pos = macro.find_first_of(" (");
|
2010-04-02 07:30:58 +02:00
|
|
|
if (pos != std::string::npos && macro[pos] == '(')
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
|
|
|
// Extract macro parameters
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tokens(), "%var% ( %var%"))
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
for (const Token *tok = tokens()->tokAt(2); tok; tok = tok->next())
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->str() == ")")
|
2009-01-24 20:28:30 +01:00
|
|
|
break;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::simpleMatch(tok, ". . . )"))
|
2009-02-13 14:31:40 +01:00
|
|
|
{
|
|
|
|
_variadic = true;
|
|
|
|
break;
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok->isName())
|
2009-01-24 20:28:30 +01:00
|
|
|
_params.push_back(tok->str());
|
|
|
|
}
|
|
|
|
}
|
2009-06-18 23:09:11 +02:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(tokens(), "%var% ( . . . )"))
|
2009-06-18 23:09:11 +02:00
|
|
|
_variadic = true;
|
2009-06-19 16:42:47 +02:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (Token::Match(tokens(), "%var% ( )"))
|
2009-06-19 16:42:47 +02:00
|
|
|
_nopar = true;
|
2009-01-24 20:28:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-13 21:50:44 +01:00
|
|
|
/** return tokens of this macro */
|
2009-01-24 20:28:30 +01:00
|
|
|
const Token *tokens() const
|
|
|
|
{
|
|
|
|
return tokenizer.tokens();
|
|
|
|
}
|
|
|
|
|
2010-01-13 21:50:44 +01:00
|
|
|
/** read parameters of this macro */
|
2009-12-09 17:13:48 +01:00
|
|
|
const std::vector<std::string> ¶ms() const
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
|
|
|
return _params;
|
|
|
|
}
|
|
|
|
|
2010-01-13 21:50:44 +01:00
|
|
|
/** check if this is macro has a variable number of parameters */
|
2009-02-13 14:31:40 +01:00
|
|
|
bool variadic() const
|
|
|
|
{
|
|
|
|
return _variadic;
|
|
|
|
}
|
|
|
|
|
2010-01-13 21:50:44 +01:00
|
|
|
/** Check if this macro has parantheses but no parameters */
|
2009-06-19 16:42:47 +02:00
|
|
|
bool nopar() const
|
|
|
|
{
|
|
|
|
return _nopar;
|
|
|
|
}
|
|
|
|
|
2010-01-13 21:50:44 +01:00
|
|
|
/** name of macro */
|
2009-01-24 20:28:30 +01:00
|
|
|
const std::string &name() const
|
|
|
|
{
|
|
|
|
return _name;
|
|
|
|
}
|
|
|
|
|
2010-01-13 21:50:44 +01:00
|
|
|
/**
|
|
|
|
* get expanded code for this macro
|
|
|
|
* @param params2 macro parameters
|
2010-06-19 12:03:39 +02:00
|
|
|
* @param macros macro definitions (recursion)
|
2010-01-13 21:50:44 +01:00
|
|
|
* @param macrocode output string
|
|
|
|
* @return true if the expanding was successful
|
|
|
|
*/
|
2010-06-19 12:03:39 +02:00
|
|
|
bool code(const std::vector<std::string> ¶ms2, const std::map<std::string, PreprocessorMacro *> macros, std::string ¯ocode) const
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
2010-06-20 18:41:45 +02:00
|
|
|
if (_nopar || (_params.empty() && _variadic))
|
2009-06-19 16:42:47 +02:00
|
|
|
{
|
|
|
|
macrocode = _macro.substr(1 + _macro.find(")"));
|
2010-04-02 07:30:58 +02:00
|
|
|
if (macrocode.empty())
|
2009-06-19 16:42:47 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
std::string::size_type pos = 0;
|
|
|
|
// Remove leading spaces
|
2010-04-02 07:30:58 +02:00
|
|
|
if ((pos = macrocode.find_first_not_of(" ")) > 0)
|
2009-06-19 16:42:47 +02:00
|
|
|
macrocode.erase(0, pos);
|
|
|
|
// Remove ending newline
|
2010-04-02 07:30:58 +02:00
|
|
|
if ((pos = macrocode.find_first_of("\r\n")) != std::string::npos)
|
2009-06-19 16:42:47 +02:00
|
|
|
macrocode.erase(pos);
|
2009-06-18 23:09:11 +02:00
|
|
|
|
|
|
|
// Replace "__VA_ARGS__" with parameters
|
2010-06-20 18:41:45 +02:00
|
|
|
if (!_nopar)
|
2009-06-18 23:09:11 +02:00
|
|
|
{
|
2010-06-20 18:41:45 +02:00
|
|
|
std::string s;
|
|
|
|
for (unsigned int i = 0; i < params2.size(); ++i)
|
|
|
|
{
|
|
|
|
if (i > 0)
|
|
|
|
s += ",";
|
|
|
|
s += params2[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = 0;
|
|
|
|
while ((pos = macrocode.find("__VA_ARGS__", pos)) != std::string::npos)
|
|
|
|
{
|
|
|
|
macrocode.erase(pos, 11);
|
|
|
|
macrocode.insert(pos, s);
|
|
|
|
pos += s.length();
|
|
|
|
}
|
2009-06-18 23:09:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (_params.empty())
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
|
|
|
std::string::size_type pos = _macro.find(" ");
|
2010-04-02 07:30:58 +02:00
|
|
|
if (pos == std::string::npos)
|
2009-01-24 20:28:30 +01:00
|
|
|
macrocode = "";
|
|
|
|
else
|
|
|
|
{
|
|
|
|
macrocode = _macro.substr(pos + 1);
|
2010-04-02 07:30:58 +02:00
|
|
|
if ((pos = macrocode.find_first_of("\r\n")) != std::string::npos)
|
2009-01-24 20:28:30 +01:00
|
|
|
macrocode.erase(pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
2010-06-19 12:03:39 +02:00
|
|
|
const std::vector<std::string> givenparams = expandInnerMacros(params2, macros);
|
|
|
|
|
2009-01-24 20:28:30 +01:00
|
|
|
const Token *tok = tokens();
|
2010-04-02 07:30:58 +02:00
|
|
|
while (tok && tok->str() != ")")
|
2009-01-24 20:28:30 +01:00
|
|
|
tok = tok->next();
|
2010-04-02 07:30:58 +02:00
|
|
|
if (tok)
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
2009-02-13 14:34:24 +01:00
|
|
|
bool optcomma = false;
|
2010-04-02 07:30:58 +02:00
|
|
|
while ((tok = tok->next()) != NULL)
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
|
|
|
std::string str = tok->str();
|
2010-04-02 07:30:58 +02:00
|
|
|
if (str == "##")
|
2009-01-24 20:28:30 +01:00
|
|
|
continue;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (str[0] == '#' || tok->isName())
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
2009-10-14 20:40:17 +02:00
|
|
|
const bool stringify(str[0] == '#');
|
2010-04-02 07:30:58 +02:00
|
|
|
if (stringify)
|
2009-01-25 14:30:15 +01:00
|
|
|
{
|
|
|
|
str = str.erase(0, 1);
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
for (unsigned int i = 0; i < _params.size(); ++i)
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (str == _params[i])
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (_variadic &&
|
|
|
|
(i == _params.size() - 1 ||
|
2010-06-19 12:03:39 +02:00
|
|
|
(givenparams.size() + 2 == _params.size() && i + 1 == _params.size() - 1)))
|
2009-02-13 14:31:40 +01:00
|
|
|
{
|
|
|
|
str = "";
|
2010-06-19 12:03:39 +02:00
|
|
|
for (unsigned int j = (unsigned int)_params.size() - 1; j < givenparams.size(); ++j)
|
2009-02-13 14:31:40 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (optcomma || j > _params.size() - 1)
|
2009-02-13 14:31:40 +01:00
|
|
|
str += ",";
|
2009-02-13 14:34:24 +01:00
|
|
|
optcomma = false;
|
2010-06-19 12:03:39 +02:00
|
|
|
str += givenparams[j];
|
2009-02-13 14:31:40 +01:00
|
|
|
}
|
|
|
|
}
|
2010-06-19 12:03:39 +02:00
|
|
|
else if (i >= givenparams.size())
|
2009-05-18 22:32:04 +02:00
|
|
|
{
|
|
|
|
// Macro had more parameters than caller used.
|
|
|
|
macrocode = "";
|
|
|
|
return false;
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (stringify)
|
2009-10-14 20:40:17 +02:00
|
|
|
{
|
2010-06-19 12:03:39 +02:00
|
|
|
const std::string &s(givenparams[i]);
|
2009-10-14 20:40:17 +02:00
|
|
|
std::ostringstream ostr;
|
|
|
|
ostr << "\"";
|
2010-04-09 21:40:37 +02:00
|
|
|
for (std::string::size_type j = 0; j < s.size(); ++j)
|
2009-10-14 20:40:17 +02:00
|
|
|
{
|
2010-04-09 21:40:37 +02:00
|
|
|
if (s[j] == '\\' || s[j] == '\"')
|
2009-10-14 20:40:17 +02:00
|
|
|
ostr << '\\';
|
2010-04-09 21:40:37 +02:00
|
|
|
ostr << s[j];
|
2009-10-14 20:40:17 +02:00
|
|
|
}
|
|
|
|
str = ostr.str() + "\"";
|
|
|
|
}
|
2009-01-25 14:30:15 +01:00
|
|
|
else
|
2010-06-19 12:03:39 +02:00
|
|
|
str = givenparams[i];
|
2009-05-18 22:32:04 +02:00
|
|
|
|
2009-01-24 20:28:30 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
if (_variadic && tok->str() == "," && tok->next() && tok->next()->str() == "##")
|
2009-02-13 14:34:24 +01:00
|
|
|
{
|
|
|
|
optcomma = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
optcomma = false;
|
2009-01-24 20:28:30 +01:00
|
|
|
macrocode += str;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (Token::Match(tok, "%var% %var%") ||
|
2010-04-15 22:21:00 +02:00
|
|
|
Token::Match(tok, "%var% %num%") ||
|
|
|
|
Token::Match(tok, "%num% %var%") ||
|
2010-04-02 07:30:58 +02:00
|
|
|
Token::Match(tok, "> >"))
|
2009-01-24 20:28:30 +01:00
|
|
|
macrocode += " ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-18 22:32:04 +02:00
|
|
|
return true;
|
2009-01-24 20:28:30 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-01-13 21:50:44 +01:00
|
|
|
/**
|
|
|
|
* Get data from a input string. This is an extended version of std::getline.
|
|
|
|
* The std::getline only get a single line at a time. It can therefore happen that it
|
|
|
|
* contains a partial statement. This function ensures that the returned data
|
|
|
|
* doesn't end in the middle of a statement. The "getlines" name indicate that
|
|
|
|
* this function will return multiple lines if needed.
|
|
|
|
* @param istr input stream
|
|
|
|
* @param line output data
|
|
|
|
* @return success
|
|
|
|
*/
|
2009-12-09 17:13:48 +01:00
|
|
|
static bool getlines(std::istream &istr, std::string &line)
|
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!istr.good())
|
2009-12-09 17:13:48 +01:00
|
|
|
return false;
|
|
|
|
line = "";
|
2010-01-02 20:54:52 +01:00
|
|
|
int parlevel = 0;
|
2010-08-07 20:33:24 +02:00
|
|
|
for (char ch = (char)istr.get(); istr.good(); ch = (char)istr.get())
|
2009-12-09 17:13:48 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (ch == '\'' || ch == '\"')
|
2009-12-06 20:10:10 +01:00
|
|
|
{
|
2009-12-09 17:13:48 +01:00
|
|
|
line += ch;
|
2010-08-06 19:38:21 +02:00
|
|
|
char c = 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
while (istr.good() && c != ch)
|
2009-11-18 23:34:00 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (c == '\\')
|
2009-12-06 20:10:10 +01:00
|
|
|
{
|
2010-08-07 20:33:24 +02:00
|
|
|
c = (char)istr.get();
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!istr.good())
|
2009-12-09 17:13:48 +01:00
|
|
|
return true;
|
|
|
|
line += c;
|
2009-11-18 23:34:00 +01:00
|
|
|
}
|
2009-12-06 20:10:10 +01:00
|
|
|
|
2010-08-07 20:33:24 +02:00
|
|
|
c = (char)istr.get();
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!istr.good())
|
2009-12-09 17:13:48 +01:00
|
|
|
return true;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (c == '\n' && line.compare(0, 1, "#") == 0)
|
2009-12-09 17:13:48 +01:00
|
|
|
return true;
|
|
|
|
line += c;
|
|
|
|
}
|
2009-12-06 20:10:10 +01:00
|
|
|
continue;
|
2009-01-24 20:28:30 +01:00
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
if (ch == '(')
|
2010-01-02 20:54:52 +01:00
|
|
|
++parlevel;
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (ch == ')')
|
2010-01-02 20:54:52 +01:00
|
|
|
--parlevel;
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (ch == '\n')
|
2009-12-09 17:13:48 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (line.compare(0, 1, "#") == 0)
|
2009-12-09 17:13:48 +01:00
|
|
|
return true;
|
2009-01-24 20:28:30 +01:00
|
|
|
|
2010-08-06 19:38:21 +02:00
|
|
|
if (istr.peek() == '#')
|
2009-12-09 17:13:48 +01:00
|
|
|
{
|
|
|
|
line += ch;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (line.compare(0, 1, "#") != 0 && parlevel <= 0 && ch == ';')
|
2009-12-06 20:10:10 +01:00
|
|
|
{
|
2009-12-09 17:13:48 +01:00
|
|
|
line += ";";
|
|
|
|
return true;
|
2009-12-06 20:10:10 +01:00
|
|
|
}
|
2009-12-06 19:17:59 +01:00
|
|
|
|
2009-12-09 17:13:48 +01:00
|
|
|
line += ch;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2009-01-24 20:28:30 +01:00
|
|
|
|
2009-12-09 17:13:48 +01:00
|
|
|
std::string Preprocessor::expandMacros(const std::string &code, std::string filename, ErrorLogger *errorLogger)
|
|
|
|
{
|
|
|
|
// Search for macros and expand them..
|
2010-01-13 21:50:44 +01:00
|
|
|
// --------------------------------------------
|
2009-12-06 19:17:59 +01:00
|
|
|
|
2010-01-13 21:50:44 +01:00
|
|
|
// Available macros (key=macroname, value=macro).
|
2009-12-09 17:13:48 +01:00
|
|
|
std::map<std::string, PreprocessorMacro *> macros;
|
2009-01-24 20:28:30 +01:00
|
|
|
|
2010-01-13 21:50:44 +01:00
|
|
|
// Current line number
|
2009-12-09 17:13:48 +01:00
|
|
|
unsigned int linenr = 1;
|
2009-01-24 20:28:30 +01:00
|
|
|
|
2009-12-09 17:13:48 +01:00
|
|
|
// linenr, filename
|
|
|
|
std::stack< std::pair<unsigned int, std::string> > fileinfo;
|
2009-12-06 19:17:59 +01:00
|
|
|
|
2010-01-13 21:50:44 +01:00
|
|
|
// output stream
|
2009-12-09 17:13:48 +01:00
|
|
|
std::ostringstream ostr;
|
2010-01-13 21:50:44 +01:00
|
|
|
|
|
|
|
// read code..
|
2009-12-09 17:13:48 +01:00
|
|
|
std::istringstream istr(code.c_str());
|
|
|
|
std::string line;
|
2010-04-02 07:30:58 +02:00
|
|
|
while (getlines(istr, line))
|
2009-12-09 17:13:48 +01:00
|
|
|
{
|
2010-01-13 21:50:44 +01:00
|
|
|
// defining a macro..
|
2010-04-02 07:30:58 +02:00
|
|
|
if (line.compare(0, 8, "#define ") == 0)
|
2009-12-09 17:13:48 +01:00
|
|
|
{
|
|
|
|
PreprocessorMacro *macro = new PreprocessorMacro(line.substr(8));
|
2010-04-02 07:30:58 +02:00
|
|
|
if (macro->name().empty())
|
2009-12-09 17:13:48 +01:00
|
|
|
delete macro;
|
|
|
|
else
|
2009-12-28 19:48:30 +01:00
|
|
|
{
|
|
|
|
std::map<std::string, PreprocessorMacro *>::iterator it;
|
|
|
|
it = macros.find(macro->name());
|
2010-04-02 07:30:58 +02:00
|
|
|
if (it != macros.end())
|
2009-12-28 19:48:30 +01:00
|
|
|
delete it->second;
|
2009-12-09 17:13:48 +01:00
|
|
|
macros[macro->name()] = macro;
|
2009-12-28 19:48:30 +01:00
|
|
|
}
|
2009-12-09 17:13:48 +01:00
|
|
|
line = "\n";
|
|
|
|
}
|
2009-01-24 20:28:30 +01:00
|
|
|
|
2010-01-13 21:50:44 +01:00
|
|
|
// undefining a macro..
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (line.compare(0, 7, "#undef ") == 0)
|
2009-12-09 17:13:48 +01:00
|
|
|
{
|
|
|
|
std::map<std::string, PreprocessorMacro *>::iterator it;
|
|
|
|
it = macros.find(line.substr(7));
|
2010-04-02 07:30:58 +02:00
|
|
|
if (it != macros.end())
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
2009-12-09 17:13:48 +01:00
|
|
|
delete it->second;
|
|
|
|
macros.erase(it);
|
2009-12-06 20:10:10 +01:00
|
|
|
}
|
2009-12-09 17:13:48 +01:00
|
|
|
line = "\n";
|
|
|
|
}
|
2009-01-24 20:28:30 +01:00
|
|
|
|
2010-01-13 21:50:44 +01:00
|
|
|
// entering a file, update position..
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (line.compare(0, 7, "#file \"") == 0)
|
2009-12-09 17:13:48 +01:00
|
|
|
{
|
|
|
|
fileinfo.push(std::pair<unsigned int, std::string>(linenr, filename));
|
|
|
|
filename = line.substr(7, line.length() - 8);
|
|
|
|
linenr = 0;
|
|
|
|
line += "\n";
|
|
|
|
}
|
2009-09-19 23:09:05 +02:00
|
|
|
|
2010-01-13 21:50:44 +01:00
|
|
|
// leaving a file, update position..
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (line == "#endfile")
|
2009-12-09 17:13:48 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!fileinfo.empty())
|
2009-12-06 20:10:10 +01:00
|
|
|
{
|
2009-12-09 17:13:48 +01:00
|
|
|
linenr = fileinfo.top().first;
|
|
|
|
filename = fileinfo.top().second;
|
|
|
|
fileinfo.pop();
|
2009-12-06 20:10:10 +01:00
|
|
|
}
|
2009-12-09 17:13:48 +01:00
|
|
|
line += "\n";
|
|
|
|
}
|
2009-11-18 23:34:00 +01:00
|
|
|
|
2010-01-13 21:50:44 +01:00
|
|
|
// all other preprocessor directives are just replaced with a newline
|
2010-04-02 07:30:58 +02:00
|
|
|
else if (line.compare(0, 1, "#") == 0)
|
2009-12-09 17:13:48 +01:00
|
|
|
{
|
|
|
|
line += "\n";
|
|
|
|
}
|
2009-09-19 23:09:05 +02:00
|
|
|
|
2009-12-09 17:13:48 +01:00
|
|
|
// expand macros..
|
|
|
|
else
|
|
|
|
{
|
2009-12-31 12:45:17 +01:00
|
|
|
// Limit for each macro.
|
|
|
|
// The limit specify a position in the "line" variable.
|
|
|
|
// For a "recursive macro" where the expanded text contains
|
|
|
|
// the macro again, the macro should not be expanded again.
|
|
|
|
// The limits are used to prevent recursive expanding.
|
|
|
|
// * When a macro is expanded its limit position is set to
|
|
|
|
// the last expanded character.
|
|
|
|
// * macros are only allowed to be expanded when the
|
|
|
|
// the position is beyond the limit.
|
|
|
|
// * The limit is relative to the end of the "line"
|
|
|
|
// variable. Inserting and deleting text before the limit
|
|
|
|
// without updating the limit is safe.
|
|
|
|
// * when pos goes beyond a limit the limit needs to be
|
|
|
|
// deleted because it is unsafe to insert/delete text
|
2010-01-13 21:50:44 +01:00
|
|
|
// after the limit otherwise
|
2009-12-09 17:13:48 +01:00
|
|
|
std::map<const PreprocessorMacro *, unsigned int> limits;
|
2009-03-09 20:29:25 +01:00
|
|
|
|
2009-12-31 12:45:17 +01:00
|
|
|
// pos is the current position in line
|
2009-12-09 17:13:48 +01:00
|
|
|
std::string::size_type pos = 0;
|
2010-01-13 21:50:44 +01:00
|
|
|
|
|
|
|
// scan line to see if there are any macros to expand..
|
2010-02-26 22:11:23 +01:00
|
|
|
unsigned int tmpLinenr = 0;
|
2010-04-02 07:30:58 +02:00
|
|
|
while (pos < line.size())
|
2009-12-06 20:10:10 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (line[pos] == '\n')
|
2010-02-26 22:11:23 +01:00
|
|
|
++tmpLinenr;
|
2009-12-09 17:13:48 +01:00
|
|
|
|
|
|
|
// skip strings..
|
2010-04-02 07:30:58 +02:00
|
|
|
if (line[pos] == '\"' || line[pos] == '\'')
|
2009-12-06 20:10:10 +01:00
|
|
|
{
|
2009-12-09 17:13:48 +01:00
|
|
|
const char ch = line[pos];
|
|
|
|
|
|
|
|
skipstring(line, pos);
|
|
|
|
++pos;
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (pos >= line.size())
|
2009-12-09 17:13:48 +01:00
|
|
|
{
|
|
|
|
writeError(filename,
|
2010-02-26 22:11:23 +01:00
|
|
|
linenr + tmpLinenr,
|
2009-12-09 17:13:48 +01:00
|
|
|
errorLogger,
|
|
|
|
"noQuoteCharPair",
|
|
|
|
std::string("No pair for character (") + ch + "). Can't process file. File is either invalid or unicode, which is currently not supported.");
|
2009-12-28 19:48:30 +01:00
|
|
|
|
|
|
|
std::map<std::string, PreprocessorMacro *>::iterator it;
|
2010-04-02 07:30:58 +02:00
|
|
|
for (it = macros.begin(); it != macros.end(); ++it)
|
2009-12-28 19:48:30 +01:00
|
|
|
delete it->second;
|
|
|
|
|
2009-12-09 17:13:48 +01:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2009-12-06 20:10:10 +01:00
|
|
|
continue;
|
|
|
|
}
|
2009-05-24 22:57:12 +02:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!std::isalpha(line[pos]) && line[pos] != '_')
|
2009-12-09 17:13:48 +01:00
|
|
|
++pos;
|
2009-01-24 20:28:30 +01:00
|
|
|
|
2009-12-09 17:13:48 +01:00
|
|
|
// found an identifier..
|
2010-01-13 21:50:44 +01:00
|
|
|
// the "while" is used in case the expanded macro will immediately call another macro
|
2010-04-02 07:30:58 +02:00
|
|
|
while (pos < line.length() && (std::isalpha(line[pos]) || line[pos] == '_'))
|
2009-12-09 17:13:48 +01:00
|
|
|
{
|
2010-01-13 21:50:44 +01:00
|
|
|
// pos1 = start position of macro
|
2009-12-09 17:13:48 +01:00
|
|
|
const std::string::size_type pos1 = pos++;
|
2010-01-13 21:50:44 +01:00
|
|
|
|
|
|
|
// find the end of the identifier
|
2010-04-02 07:30:58 +02:00
|
|
|
while (pos < line.size() && (std::isalnum(line[pos]) || line[pos] == '_'))
|
2009-12-09 17:13:48 +01:00
|
|
|
++pos;
|
2009-12-06 19:17:59 +01:00
|
|
|
|
2010-01-13 21:50:44 +01:00
|
|
|
// get identifier
|
2009-12-09 17:13:48 +01:00
|
|
|
const std::string id = line.substr(pos1, pos - pos1);
|
2009-12-06 19:17:59 +01:00
|
|
|
|
2009-12-09 17:13:48 +01:00
|
|
|
// is there a macro with this name?
|
|
|
|
std::map<std::string, PreprocessorMacro *>::const_iterator it;
|
|
|
|
it = macros.find(id);
|
2010-04-02 07:30:58 +02:00
|
|
|
if (it == macros.end())
|
2010-01-13 21:50:44 +01:00
|
|
|
break; // no macro with this name exist
|
2009-12-09 17:13:48 +01:00
|
|
|
|
|
|
|
const PreprocessorMacro * const macro = it->second;
|
2009-12-06 20:10:10 +01:00
|
|
|
|
2010-01-13 21:50:44 +01:00
|
|
|
// check that pos is within allowed limits for this
|
2009-12-31 12:45:17 +01:00
|
|
|
// macro
|
2009-12-06 20:10:10 +01:00
|
|
|
{
|
2009-12-09 17:13:48 +01:00
|
|
|
const std::map<const PreprocessorMacro *, unsigned int>::const_iterator it2 = limits.find(macro);
|
2010-04-02 07:30:58 +02:00
|
|
|
if (it2 != limits.end() && pos <= line.length() - it2->second)
|
2009-12-06 20:10:10 +01:00
|
|
|
break;
|
|
|
|
}
|
2009-12-09 17:13:48 +01:00
|
|
|
|
2010-01-13 21:50:44 +01:00
|
|
|
// get parameters from line..
|
2009-12-09 17:13:48 +01:00
|
|
|
std::vector<std::string> params;
|
|
|
|
std::string::size_type pos2 = pos;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (macro->params().size() && pos2 >= line.length())
|
2009-12-09 17:13:48 +01:00
|
|
|
break;
|
|
|
|
|
2010-01-13 21:50:44 +01:00
|
|
|
// number of newlines within macro use
|
2009-12-09 17:13:48 +01:00
|
|
|
unsigned int numberOfNewlines = 0;
|
|
|
|
|
2010-01-13 21:50:44 +01:00
|
|
|
// if the macro has parantheses, get parameters
|
2010-04-02 07:30:58 +02:00
|
|
|
if (macro->variadic() || macro->nopar() || macro->params().size())
|
2009-12-06 20:10:10 +01:00
|
|
|
{
|
2010-01-13 21:50:44 +01:00
|
|
|
// is the end paranthesis found?
|
2009-12-09 17:13:48 +01:00
|
|
|
bool endFound = false;
|
2010-01-13 21:50:44 +01:00
|
|
|
|
2010-06-19 09:09:47 +02:00
|
|
|
getparams(line,pos2,params,numberOfNewlines,endFound);
|
2010-01-13 21:50:44 +01:00
|
|
|
|
2009-12-09 17:13:48 +01:00
|
|
|
|
2010-01-13 21:50:44 +01:00
|
|
|
// something went wrong so bail out
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!endFound)
|
2009-01-24 20:28:30 +01:00
|
|
|
break;
|
|
|
|
}
|
2009-11-18 23:34:00 +01:00
|
|
|
|
2010-01-13 21:50:44 +01:00
|
|
|
// Just an empty parameter => clear
|
2010-04-02 07:30:58 +02:00
|
|
|
if (params.size() == 1 && params[0] == "")
|
2009-12-09 17:13:48 +01:00
|
|
|
params.clear();
|
2009-11-18 23:34:00 +01:00
|
|
|
|
2010-01-13 21:50:44 +01:00
|
|
|
// Check that it's the same number of parameters..
|
2010-04-02 07:30:58 +02:00
|
|
|
if (!macro->variadic() && params.size() != macro->params().size())
|
2009-12-09 17:13:48 +01:00
|
|
|
break;
|
2009-01-24 20:28:30 +01:00
|
|
|
|
2009-12-09 17:13:48 +01:00
|
|
|
// Create macro code..
|
|
|
|
std::string tempMacro;
|
2010-06-19 12:03:39 +02:00
|
|
|
if (!macro->code(params, macros, tempMacro))
|
2009-03-16 22:20:55 +01:00
|
|
|
{
|
2009-12-09 17:13:48 +01:00
|
|
|
// Syntax error in code
|
|
|
|
writeError(filename,
|
2010-02-26 22:11:23 +01:00
|
|
|
linenr + tmpLinenr,
|
2009-12-09 17:13:48 +01:00
|
|
|
errorLogger,
|
|
|
|
"syntaxError",
|
|
|
|
std::string("Syntax error. Not enough parameters for macro '") + macro->name() + "'.");
|
2009-12-28 19:48:30 +01:00
|
|
|
|
2010-04-09 21:40:37 +02:00
|
|
|
std::map<std::string, PreprocessorMacro *>::iterator iter;
|
|
|
|
for (iter = macros.begin(); iter != macros.end(); ++iter)
|
|
|
|
delete iter->second;
|
2009-05-18 22:32:04 +02:00
|
|
|
|
2009-12-09 17:13:48 +01:00
|
|
|
return "";
|
|
|
|
}
|
2009-12-06 20:10:10 +01:00
|
|
|
|
2010-01-13 21:50:44 +01:00
|
|
|
// make sure number of newlines remain the same..
|
2010-04-15 22:21:00 +02:00
|
|
|
std::string macrocode(std::string(numberOfNewlines, '\n') + tempMacro);
|
2009-12-06 20:10:10 +01:00
|
|
|
|
2009-12-09 17:13:48 +01:00
|
|
|
// Insert macro code..
|
2010-04-02 07:30:58 +02:00
|
|
|
if (macro->variadic() || macro->nopar() || !macro->params().empty())
|
2009-12-09 17:13:48 +01:00
|
|
|
++pos2;
|
2009-12-06 20:10:10 +01:00
|
|
|
|
2009-12-31 12:45:17 +01:00
|
|
|
// Remove old limits
|
2010-04-02 07:30:58 +02:00
|
|
|
for (std::map<const PreprocessorMacro *, unsigned int>::iterator iter = limits.begin();
|
|
|
|
iter != limits.end();)
|
2009-12-14 23:06:05 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if ((line.length() - pos1) < iter->second)
|
2009-12-14 23:06:05 +01:00
|
|
|
{
|
|
|
|
// We have gone past this limit, so just delete it
|
|
|
|
limits.erase(iter++);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
}
|
2009-12-06 20:10:10 +01:00
|
|
|
|
2009-12-31 12:45:17 +01:00
|
|
|
// don't allow this macro to be expanded again before pos2
|
|
|
|
limits[macro] = line.length() - pos2;
|
|
|
|
|
|
|
|
// erase macro
|
2009-12-09 17:13:48 +01:00
|
|
|
line.erase(pos1, pos2 - pos1);
|
2009-12-31 12:45:17 +01:00
|
|
|
|
2010-04-15 22:21:00 +02:00
|
|
|
// Don't glue this macro into variable or number after it
|
|
|
|
if (std::isalnum(line[pos1]) || line[pos1] == '_')
|
|
|
|
macrocode.append(1,' ');
|
|
|
|
|
2009-12-31 12:45:17 +01:00
|
|
|
// insert expanded macro code
|
2009-12-09 17:13:48 +01:00
|
|
|
line.insert(pos1, macrocode);
|
2009-12-31 12:45:17 +01:00
|
|
|
|
|
|
|
// position = start position.
|
2009-12-09 17:13:48 +01:00
|
|
|
pos = pos1;
|
|
|
|
}
|
2009-05-18 22:32:04 +02:00
|
|
|
}
|
2009-12-09 17:13:48 +01:00
|
|
|
}
|
2009-05-18 22:32:04 +02:00
|
|
|
|
2010-01-13 21:50:44 +01:00
|
|
|
// the line has been processed in various ways. Now add it to the output stream
|
2009-12-09 17:13:48 +01:00
|
|
|
ostr << line;
|
2010-01-13 21:50:44 +01:00
|
|
|
|
|
|
|
// update linenr
|
2010-04-02 07:30:58 +02:00
|
|
|
for (std::string::size_type p = 0; p < line.length(); ++p)
|
2009-12-09 17:13:48 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (line[p] == '\n')
|
2009-12-09 17:13:48 +01:00
|
|
|
++linenr;
|
2009-01-24 20:28:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2009-12-09 17:13:48 +01:00
|
|
|
std::map<std::string, PreprocessorMacro *>::iterator it;
|
2010-04-02 07:30:58 +02:00
|
|
|
for (it = macros.begin(); it != macros.end(); ++it)
|
2009-12-09 17:13:48 +01:00
|
|
|
delete it->second;
|
2009-01-24 20:28:30 +01:00
|
|
|
}
|
|
|
|
|
2009-12-09 17:13:48 +01:00
|
|
|
return ostr.str();
|
2009-01-24 20:28:30 +01:00
|
|
|
}
|
|
|
|
|