2009-01-24 20:28:30 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2009-05-30 07:48:12 +02:00
|
|
|
* Copyright (C) 2007-2009 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
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "preprocessor.h"
|
|
|
|
#include "tokenize.h"
|
|
|
|
#include "token.h"
|
2009-05-19 21:19:15 +02:00
|
|
|
#include "filelister.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 <iostream>
|
|
|
|
#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-01-24 20:28:30 +01:00
|
|
|
|
2009-07-25 21:10:30 +02:00
|
|
|
Preprocessor::Preprocessor(const Settings *settings, ErrorLogger *errorLogger) : _settings(settings), _errorLogger(errorLogger)
|
2009-06-26 13:19:55 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-05-19 21:19:15 +02:00
|
|
|
void Preprocessor::writeError(const std::string &fileName, const std::string &code, size_t endPos, ErrorLogger *errorLogger, const std::string &errorType, const std::string &errorText)
|
|
|
|
{
|
|
|
|
if (!errorLogger)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// line number in parsed code
|
|
|
|
unsigned int lineno = 1;
|
|
|
|
|
|
|
|
// The current token being parsed
|
|
|
|
std::string CurrentToken;
|
|
|
|
|
|
|
|
// lineNumbers holds line numbers for files in fileIndexes
|
|
|
|
// every time an include file is complitely parsed, last item in the vector
|
|
|
|
// is removed and lineno is set to point to that value.
|
|
|
|
std::vector<unsigned int> lineNumbers;
|
|
|
|
|
|
|
|
// fileIndexes holds index for _files vector about currently parsed files
|
|
|
|
// every time an include file is complitely parsed, last item in the vector
|
|
|
|
// is removed and FileIndex is set to point to that value.
|
|
|
|
std::vector<std::string> fileIndexes;
|
|
|
|
|
|
|
|
// FileIndex. What file in the _files vector is read now?
|
|
|
|
std::string FileIndex = fileName;
|
|
|
|
|
|
|
|
// Read one byte at a time from code and create tokens
|
|
|
|
if (endPos > code.length())
|
|
|
|
endPos = code.length();
|
|
|
|
|
|
|
|
for (size_t codePos = 0; codePos < endPos; ++codePos)
|
|
|
|
{
|
|
|
|
|
|
|
|
char ch = code[codePos];
|
|
|
|
|
2009-05-30 14:24:55 +02:00
|
|
|
// UTF / extended ASCII => The output from the preprocessor should only be standard ASCII
|
2009-05-19 21:19:15 +02:00
|
|
|
if (ch < 0)
|
2009-05-30 14:24:55 +02:00
|
|
|
{
|
2009-07-14 08:17:12 +02:00
|
|
|
/**
|
|
|
|
* @todo handle utf better:
|
|
|
|
* - remove characters that are redundant
|
|
|
|
* - convert needed characters to standard ASCII
|
|
|
|
*/
|
2009-07-16 07:20:29 +02:00
|
|
|
|
2009-05-30 14:24:55 +02:00
|
|
|
// Not sure how to handle this character. Bailing out.
|
|
|
|
if (ch < 0)
|
|
|
|
continue;
|
|
|
|
}
|
2009-05-19 21:19:15 +02:00
|
|
|
|
|
|
|
// char/string..
|
|
|
|
if (ch == '\'' || ch == '\"')
|
|
|
|
{
|
|
|
|
std::string line;
|
|
|
|
|
|
|
|
// read char
|
|
|
|
bool special = false;
|
|
|
|
char c = ch;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
// Append token..
|
|
|
|
line += c;
|
|
|
|
|
|
|
|
if (c == '\n')
|
|
|
|
++lineno;
|
|
|
|
|
|
|
|
// Special sequence '\.'
|
|
|
|
if (special)
|
|
|
|
special = false;
|
|
|
|
else
|
|
|
|
special = (c == '\\');
|
|
|
|
|
|
|
|
// Get next character
|
|
|
|
++codePos;
|
|
|
|
c = code[codePos];
|
|
|
|
}
|
|
|
|
while (codePos < endPos && (special || c != ch));
|
|
|
|
line += ch;
|
|
|
|
|
|
|
|
// Handle #file "file.h"
|
|
|
|
if (CurrentToken == "#file")
|
|
|
|
{
|
|
|
|
// Extract the filename
|
|
|
|
line = line.substr(1, line.length() - 2);
|
|
|
|
|
|
|
|
// Has this file been tokenized already?
|
|
|
|
++lineno;
|
|
|
|
fileIndexes.push_back(FileIndex);
|
|
|
|
FileIndex = FileLister::simplifyPath(line.c_str());
|
|
|
|
lineNumbers.push_back(lineno);
|
|
|
|
lineno = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
CurrentToken.clear();
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strchr("+-*/%&|^?!=<>[](){};:,.~\n ", ch))
|
|
|
|
{
|
|
|
|
if (ch == '.' &&
|
|
|
|
CurrentToken.length() > 0 &&
|
|
|
|
std::isdigit(CurrentToken[0]))
|
|
|
|
{
|
|
|
|
// Don't separate doubles "5.4"
|
|
|
|
}
|
|
|
|
else if (strchr("+-", ch) &&
|
|
|
|
CurrentToken.length() > 0 &&
|
|
|
|
std::isdigit(CurrentToken[0]) &&
|
|
|
|
CurrentToken[CurrentToken.length()-1] == 'e')
|
|
|
|
{
|
|
|
|
// Don't separate doubles "4.2e+10"
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (CurrentToken == "#file")
|
|
|
|
{
|
|
|
|
// Handle this where strings are handled
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (CurrentToken == "#endfile")
|
|
|
|
{
|
|
|
|
if (lineNumbers.empty() || fileIndexes.empty())
|
|
|
|
{
|
|
|
|
std::cerr << "####### Preprocessor bug! #######\n";
|
|
|
|
std::exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
lineno = lineNumbers.back();
|
|
|
|
lineNumbers.pop_back();
|
|
|
|
FileIndex = fileIndexes.back();
|
|
|
|
fileIndexes.pop_back();
|
|
|
|
CurrentToken.clear();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If token contains # characters, split it up
|
|
|
|
std::string temp;
|
|
|
|
for (std::string::size_type i = 0; i < CurrentToken.length(); ++i)
|
|
|
|
{
|
|
|
|
if (CurrentToken[i] == '#' && CurrentToken.length() + 1 > i && CurrentToken[i+1] == '#')
|
|
|
|
{
|
|
|
|
temp.clear();
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
temp += CurrentToken[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
CurrentToken.clear();
|
|
|
|
|
|
|
|
if (ch == '\n')
|
|
|
|
{
|
|
|
|
++lineno;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (ch == ' ')
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
CurrentToken += ch;
|
|
|
|
// Add "++", "--" or ">>" token
|
|
|
|
if ((ch == '+' || ch == '-' || ch == '>') && (code[codePos+1] == ch))
|
|
|
|
{
|
|
|
|
++codePos;
|
|
|
|
CurrentToken += code[codePos];
|
|
|
|
}
|
|
|
|
CurrentToken.clear();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CurrentToken += ch;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::list<ErrorLogger::ErrorMessage::FileLocation> locationList;
|
|
|
|
ErrorLogger::ErrorMessage::FileLocation loc;
|
|
|
|
loc.line = lineno;
|
|
|
|
loc.file = FileIndex;
|
|
|
|
locationList.push_back(loc);
|
|
|
|
errorLogger->reportErr(
|
|
|
|
ErrorLogger::ErrorMessage(locationList,
|
|
|
|
"error",
|
|
|
|
errorText,
|
|
|
|
errorType));
|
|
|
|
}
|
|
|
|
|
2009-01-24 20:28:30 +01:00
|
|
|
static char readChar(std::istream &istr)
|
|
|
|
{
|
|
|
|
char ch = (char)istr.get();
|
|
|
|
|
|
|
|
// Handling of newlines..
|
|
|
|
if (ch == '\r')
|
|
|
|
{
|
|
|
|
ch = '\n';
|
|
|
|
if ((char)istr.peek() == '\n')
|
|
|
|
(void)istr.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ch;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Just read the code into a string. Perform simple cleanup of the code */
|
|
|
|
std::string Preprocessor::read(std::istream &istr)
|
|
|
|
{
|
|
|
|
// Get filedata from stream..
|
|
|
|
bool ignoreSpace = true;
|
|
|
|
|
|
|
|
// need space.. #if( => #if (
|
|
|
|
bool needSpace = false;
|
|
|
|
|
|
|
|
// For the error report
|
|
|
|
int lineno = 1;
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
for (char ch = readChar(istr); istr.good(); ch = readChar(istr))
|
|
|
|
{
|
|
|
|
if (ch == '\n')
|
|
|
|
++lineno;
|
|
|
|
|
|
|
|
// Replace assorted special chars with spaces..
|
2009-06-19 15:43:46 +02:00
|
|
|
if ((ch > 0) && (ch != '\n') && (std::isspace(ch) || std::iscntrl(ch)))
|
2009-01-24 20:28:30 +01:00
|
|
|
ch = ' ';
|
|
|
|
|
|
|
|
// Skip spaces after ' ' and after '#'
|
|
|
|
if (ch == ' ' && ignoreSpace)
|
|
|
|
continue;
|
2009-05-14 21:53:49 +02:00
|
|
|
ignoreSpace = bool(ch == ' ' || ch == '#' || ch == '\n');
|
2009-01-24 20:28:30 +01:00
|
|
|
|
|
|
|
if (needSpace)
|
|
|
|
{
|
|
|
|
if (ch == '(')
|
|
|
|
code << " ";
|
2009-06-19 15:43:46 +02:00
|
|
|
else if ((ch > 0) && ! std::isalpha(ch))
|
2009-01-24 20:28:30 +01:00
|
|
|
needSpace = false;
|
|
|
|
}
|
|
|
|
if (ch == '#')
|
|
|
|
needSpace = true;
|
|
|
|
|
2009-05-13 21:18:02 +02:00
|
|
|
// <backspace><newline>..
|
|
|
|
if (ch == '\\')
|
|
|
|
{
|
|
|
|
char chNext = 0;
|
2009-07-20 22:24:23 +02:00
|
|
|
for (;;)
|
2009-05-13 21:18:02 +02:00
|
|
|
{
|
|
|
|
chNext = (char)istr.peek();
|
2009-06-19 15:43:46 +02:00
|
|
|
if (chNext != '\n' && chNext != '\r' && (chNext > 0) &&
|
2009-05-13 21:18:02 +02:00
|
|
|
(std::isspace(chNext) || std::iscntrl(chNext)))
|
|
|
|
{
|
|
|
|
// Skip whitespace between <backspace> and <newline>
|
|
|
|
(void)readChar(istr);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (chNext == '\n' || chNext == '\r')
|
|
|
|
{
|
|
|
|
++newlines;
|
|
|
|
(void)readChar(istr); // Skip the "<backspace><newline>"
|
|
|
|
}
|
|
|
|
else
|
|
|
|
code << "\\";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Just some code..
|
|
|
|
else
|
|
|
|
{
|
|
|
|
code << std::string(1, ch);
|
|
|
|
|
|
|
|
// if there has been <backspace><newline> sequences, add extra newlines..
|
|
|
|
if (ch == '\n' && newlines > 0)
|
|
|
|
{
|
|
|
|
code << std::string(newlines, '\n');
|
|
|
|
newlines = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return removeComments(code.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::string Preprocessor::removeComments(const std::string &str)
|
|
|
|
{
|
|
|
|
// For the error report
|
|
|
|
int lineno = 1;
|
|
|
|
|
|
|
|
// 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-05-14 21:53:49 +02:00
|
|
|
char previous = 0;
|
2009-05-13 21:18:02 +02:00
|
|
|
for (std::string::size_type i = 0; i < str.length(); ++i)
|
|
|
|
{
|
|
|
|
char ch = str[i];
|
|
|
|
if (ch < 0)
|
|
|
|
throw std::runtime_error("The code contains characters that are unhandled");
|
|
|
|
|
2009-01-24 20:28:30 +01:00
|
|
|
// Remove comments..
|
2009-05-14 21:53:49 +02:00
|
|
|
if (str.compare(i, 2, "//", 0, 2) == 0)
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
2009-05-14 21:53:49 +02:00
|
|
|
i = str.find('\n', i);
|
|
|
|
if (i == std::string::npos)
|
|
|
|
break;
|
2009-01-24 20:28:30 +01:00
|
|
|
|
2009-05-14 21:53:49 +02:00
|
|
|
code << "\n";
|
|
|
|
previous = '\n';
|
|
|
|
++lineno;
|
|
|
|
}
|
|
|
|
else if (str.compare(i, 2, "/*", 0, 2) == 0)
|
|
|
|
{
|
|
|
|
char chPrev = 0;
|
|
|
|
++i;
|
|
|
|
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;
|
|
|
|
ch = str[i];
|
|
|
|
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..
|
|
|
|
else if (ch == '\"' || ch == '\'')
|
|
|
|
{
|
|
|
|
code << std::string(1, ch);
|
|
|
|
char chNext;
|
|
|
|
do
|
|
|
|
{
|
2009-05-13 21:18:02 +02:00
|
|
|
++i;
|
|
|
|
chNext = str[i];
|
2009-01-24 20:28:30 +01:00
|
|
|
if (chNext == '\\')
|
|
|
|
{
|
2009-05-13 21:18:02 +02:00
|
|
|
++i;
|
|
|
|
char chSeq = str[i];
|
2009-01-24 20:28:30 +01:00
|
|
|
if (chSeq == '\n')
|
|
|
|
++newlines;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
code << std::string(1, chNext);
|
|
|
|
code << std::string(1, chSeq);
|
2009-05-14 21:53:49 +02:00
|
|
|
previous = chSeq;
|
2009-01-24 20:28:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2009-05-14 21:53:49 +02:00
|
|
|
{
|
2009-01-24 20:28:30 +01:00
|
|
|
code << std::string(1, chNext);
|
2009-05-14 21:53:49 +02:00
|
|
|
previous = chNext;
|
|
|
|
}
|
2009-01-24 20:28:30 +01:00
|
|
|
}
|
2009-08-13 23:22:51 +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
|
|
|
|
{
|
2009-05-14 21:53:49 +02:00
|
|
|
if (ch == ' ' && previous == ' ')
|
|
|
|
{
|
|
|
|
// Skip double white space
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
code << std::string(1, ch);
|
|
|
|
previous = ch;
|
|
|
|
}
|
|
|
|
|
2009-01-24 20:28:30 +01:00
|
|
|
|
|
|
|
// if there has been <backspace><newline> sequences, add extra newlines..
|
|
|
|
if (ch == '\n' && newlines > 0)
|
|
|
|
{
|
|
|
|
code << std::string(newlines, '\n');
|
|
|
|
newlines = 0;
|
2009-05-14 21:53:49 +02:00
|
|
|
previous = '\n';
|
2009-01-24 20:28:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return code.str();
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
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;
|
|
|
|
int prev = -1;
|
|
|
|
for (unsigned int i = 0; i < str.size(); i++)
|
|
|
|
{
|
|
|
|
if (str[i] == ' ' &&
|
|
|
|
((i > 0 && tmp[prev] == '\n') ||
|
|
|
|
(i + 1 < str.size() && str[i+1] == '\n')
|
|
|
|
)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
// Ignore space that has new line in either side of it
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tmp.append(1, str[i]);
|
|
|
|
++prev;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2009-01-24 20:28:30 +01:00
|
|
|
while ((pos = ret.find("#if defined(", pos)) != std::string::npos)
|
|
|
|
{
|
|
|
|
std::string::size_type pos2 = ret.find(")", pos + 9);
|
|
|
|
if (pos2 > ret.length() - 1)
|
|
|
|
break;
|
|
|
|
if (ret[pos2+1] == '\n')
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
while ((pos = ret.find("#if !defined(", pos)) != std::string::npos)
|
|
|
|
{
|
|
|
|
std::string::size_type pos2 = ret.find(")", pos + 9);
|
|
|
|
if (pos2 > ret.length() - 1)
|
|
|
|
break;
|
|
|
|
if (ret[pos2+1] == '\n')
|
|
|
|
{
|
|
|
|
ret.erase(pos2, 1);
|
|
|
|
ret.erase(pos + 3, 10);
|
|
|
|
ret.insert(pos + 3, "ndef ");
|
|
|
|
}
|
|
|
|
++pos;
|
|
|
|
}
|
|
|
|
|
2009-01-24 20:28:30 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Preprocessor::preprocess(std::istream &istr, std::string &processedFile, std::list<std::string> &resultConfigurations, const std::string &filename, const std::list<std::string> &includePaths)
|
|
|
|
{
|
|
|
|
processedFile = read(istr);
|
|
|
|
|
|
|
|
// Replace all tabs with spaces..
|
|
|
|
std::replace(processedFile.begin(), processedFile.end(), '\t', ' ');
|
|
|
|
|
|
|
|
// Remove all indentation..
|
|
|
|
if (!processedFile.empty() && processedFile[0] == ' ')
|
|
|
|
processedFile.erase(0, processedFile.find_first_not_of(" "));
|
|
|
|
|
|
|
|
// Remove space characters that are after or before new line character
|
|
|
|
processedFile = removeSpaceNearNL(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;
|
|
|
|
while (std::getline(istr, line))
|
|
|
|
{
|
|
|
|
if (line.substr(0, 4) == "#if " || line.substr(0, 6) == "#elif ")
|
|
|
|
{
|
|
|
|
std::string::size_type pos = 0;
|
|
|
|
while ((pos = line.find(" defined ")) != std::string::npos)
|
|
|
|
{
|
|
|
|
line[pos+8] = '(';
|
|
|
|
pos = line.find_first_of(" |&", pos + 8);
|
|
|
|
if (pos == std::string::npos)
|
|
|
|
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..
|
|
|
|
resultConfigurations = getcfgs(processedFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 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"
|
2009-06-14 22:37:18 +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"
|
|
|
|
if (!def && line.find("#ifndef ") != 0)
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the "#ifdef" or "#ifndef"
|
2009-06-14 22:37:18 +02:00
|
|
|
if (line.find("#if defined ") == 0)
|
|
|
|
line.erase(0, 11);
|
|
|
|
else
|
|
|
|
line.erase(0, line.find(" "));
|
2009-01-24 20:28:30 +01:00
|
|
|
|
|
|
|
// Remove all spaces.
|
|
|
|
while (line.find(" ") != std::string::npos)
|
|
|
|
line.erase(line.find(" "), 1);
|
|
|
|
|
|
|
|
// The remaining string is our result.
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::list<std::string> Preprocessor::getcfgs(const std::string &filedata)
|
|
|
|
{
|
|
|
|
std::list<std::string> ret;
|
|
|
|
ret.push_back("");
|
|
|
|
|
|
|
|
std::list<std::string> deflist;
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
std::istringstream istr(filedata);
|
|
|
|
std::string line;
|
|
|
|
while (getline(istr, line))
|
|
|
|
{
|
|
|
|
if (line.substr(0, 6) == "#file ")
|
|
|
|
{
|
|
|
|
++filelevel;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (line == "#endfile")
|
|
|
|
{
|
|
|
|
if (filelevel > 0)
|
|
|
|
--filelevel;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-07-25 13:58:34 +02:00
|
|
|
else if (line.substr(0, 8) == "#define " && line.find("(", 8) == std::string::npos)
|
2009-07-22 18:47:50 +02:00
|
|
|
{
|
2009-07-25 13:58:34 +02:00
|
|
|
if (line.find(" ", 8) == std::string::npos)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2009-01-24 20:28:30 +01:00
|
|
|
if (filelevel > 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
std::string def = getdef(line, true) + getdef(line, false);
|
|
|
|
if (!def.empty())
|
|
|
|
{
|
|
|
|
if (! deflist.empty() && line.find("#elif ") == 0)
|
|
|
|
deflist.pop_back();
|
|
|
|
deflist.push_back(def);
|
|
|
|
def = "";
|
|
|
|
for (std::list<std::string>::const_iterator it = deflist.begin(); it != deflist.end(); ++it)
|
|
|
|
{
|
|
|
|
if (*it == "0")
|
|
|
|
break;
|
|
|
|
if (*it == "1")
|
|
|
|
continue;
|
|
|
|
if (! def.empty())
|
|
|
|
def += ";";
|
|
|
|
def += *it;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (std::find(ret.begin(), ret.end(), def) == ret.end())
|
|
|
|
ret.push_back(def);
|
|
|
|
}
|
|
|
|
|
2009-07-22 18:47:50 +02:00
|
|
|
else if (line.find("#else") == 0 && ! deflist.empty())
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
|
|
|
std::string def((deflist.back() == "1") ? "0" : "1");
|
|
|
|
deflist.pop_back();
|
|
|
|
deflist.push_back(def);
|
|
|
|
}
|
|
|
|
|
2009-07-22 18:47:50 +02:00
|
|
|
else if (line.find("#endif") == 0 && ! deflist.empty())
|
2009-01-24 20:28:30 +01:00
|
|
|
deflist.pop_back();
|
|
|
|
}
|
|
|
|
|
2009-07-22 18:47:50 +02:00
|
|
|
// Remove defined constants from ifdef configurations..
|
|
|
|
for (std::list<std::string>::iterator it = ret.begin(); it != ret.end(); ++it)
|
|
|
|
{
|
2009-07-25 13:58:34 +02:00
|
|
|
std::string cfg(*it);
|
2009-07-22 18:47:50 +02:00
|
|
|
for (std::set<std::string>::const_iterator it2 = defines.begin(); it2 != defines.end(); ++it2)
|
|
|
|
{
|
|
|
|
std::string::size_type pos = 0;
|
2009-07-25 13:58:34 +02:00
|
|
|
|
|
|
|
// Get name of define
|
|
|
|
std::string defineName(*it2);
|
|
|
|
if (defineName.find("=") != std::string::npos)
|
|
|
|
defineName.erase(defineName.find("="));
|
|
|
|
|
|
|
|
// Remove ifdef configurations that match the defineName
|
|
|
|
while ((pos = cfg.find(defineName, pos)) != std::string::npos)
|
2009-07-22 18:47:50 +02:00
|
|
|
{
|
|
|
|
std::string::size_type pos1 = pos;
|
|
|
|
++pos;
|
2009-07-25 13:58:34 +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();
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2009-07-25 13:58:34 +02:00
|
|
|
if (cfg.length() != it->length())
|
2009-07-22 18:47:50 +02:00
|
|
|
{
|
2009-07-25 13:58:34 +02:00
|
|
|
while (cfg.length() > 0 && cfg[0] == ';')
|
|
|
|
cfg.erase(0, 1);
|
2009-07-22 18:47:50 +02:00
|
|
|
|
2009-07-25 13:58:34 +02:00
|
|
|
while (cfg.length() > 0 && cfg[cfg.length()-1] == ';')
|
|
|
|
cfg.erase(cfg.length() - 1);
|
2009-07-22 18:47:50 +02:00
|
|
|
|
|
|
|
std::string::size_type pos = 0;
|
2009-07-25 13:58:34 +02:00
|
|
|
while ((pos = cfg.find(";;", pos)) != std::string::npos)
|
|
|
|
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"
|
|
|
|
for (std::list<std::string>::iterator it = ret.begin(); it != ret.end(); ++it)
|
|
|
|
{
|
|
|
|
std::string s(*it);
|
|
|
|
|
|
|
|
if (s.find("&&") != std::string::npos)
|
|
|
|
{
|
|
|
|
Tokenizer tokenizer;
|
|
|
|
std::istringstream istr(s.c_str());
|
|
|
|
tokenizer.tokenize(istr, "");
|
|
|
|
|
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;
|
2009-06-26 13:19:55 +02:00
|
|
|
while (tok)
|
|
|
|
{
|
|
|
|
if (Token::Match(tok, "defined ( %var% )"))
|
|
|
|
{
|
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);
|
|
|
|
if (tok && tok->str() == "&&")
|
|
|
|
{
|
|
|
|
tok = tok->next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (Token::Match(tok, "%var% ;"))
|
|
|
|
{
|
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();
|
|
|
|
s = "";
|
|
|
|
for (std::list<std::string>::iterator varIter = varList.begin(); varIter != varList.end(); ++varIter)
|
|
|
|
{
|
|
|
|
if (!s.empty())
|
|
|
|
s += ";";
|
|
|
|
|
|
|
|
s += *varIter;
|
|
|
|
}
|
|
|
|
|
2009-06-26 13:19:55 +02:00
|
|
|
if (!s.empty())
|
|
|
|
*it = s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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..
|
|
|
|
for (std::list<std::string>::iterator it = ret.begin(); it != ret.end();)
|
|
|
|
{
|
|
|
|
const std::string &s(*it);
|
|
|
|
if (s.find("&&") != std::string::npos || s.find("||") != std::string::npos)
|
|
|
|
{
|
|
|
|
// unhandled ifdef configuration..
|
2009-07-25 23:16:18 +02:00
|
|
|
if (_errorLogger && _settings && _settings->_debug)
|
|
|
|
_errorLogger->reportOut("unhandled configuration: " + s);
|
2009-06-26 13:19:55 +02:00
|
|
|
|
|
|
|
ret.erase(it++);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
|
2009-01-24 20:28:30 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
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-07-30 10:10:34 +02:00
|
|
|
if (cfg.find(def) != cfg.end())
|
|
|
|
return true;
|
|
|
|
|
2009-06-26 13:19:55 +02:00
|
|
|
for (std::string::size_type pos = def.find("defined("); pos != std::string::npos; pos = def.find("defined(", pos + 1))
|
|
|
|
{
|
|
|
|
// The character before "defined" must not be '_' or alphanumeric
|
|
|
|
unsigned char chPrev = (pos > 0) ? def[pos-1] : ' ';
|
|
|
|
if (chPrev == '_' || std::isalnum(chPrev))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Extract the parameter..
|
|
|
|
std::string::size_type pos2 = def.find(")", pos);
|
|
|
|
if (pos2 == std::string::npos)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
std::string::size_type pos1 = pos + 8;
|
|
|
|
const std::string par(def.substr(pos1, pos2 - pos1));
|
2009-07-30 10:10:34 +02:00
|
|
|
const bool isdefined(cfg.find(par) != cfg.end());
|
2009-06-26 13:19:55 +02:00
|
|
|
|
|
|
|
def.erase(pos, pos2 + 1 - pos);
|
|
|
|
def.insert(pos, isdefined ? "1" : "0");
|
|
|
|
}
|
|
|
|
|
2009-07-22 20:11:27 +02:00
|
|
|
if (def.find("1||") != std::string::npos || def.find("||1") != std::string::npos)
|
|
|
|
return true;
|
|
|
|
|
2009-06-26 13:19:55 +02:00
|
|
|
while (def.find("1&&") != std::string::npos)
|
|
|
|
{
|
|
|
|
def.erase(def.find("1&&"), 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
//std::cout << " => \"" << def << "\"" << std::endl;
|
|
|
|
|
2009-01-24 20:28:30 +01:00
|
|
|
if (def == "0")
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (def == "1")
|
|
|
|
return true;
|
|
|
|
|
2009-07-30 10:10:34 +02:00
|
|
|
/*
|
|
|
|
if (cfg.empty())
|
|
|
|
return false;
|
2009-01-24 20:28:30 +01:00
|
|
|
|
2009-07-30 10:10:34 +02:00
|
|
|
// remove the define values
|
|
|
|
while (cfg.find("=") != std::string::npos)
|
|
|
|
{
|
|
|
|
std::string::size_type pos1 = cfg.find("=");
|
|
|
|
std::string::size_type pos2 = cfg.find(";", pos1);
|
|
|
|
if (pos2 == std::string::npos)
|
|
|
|
cfg.erase(pos1);
|
|
|
|
else
|
|
|
|
cfg.erase(pos1, pos2 - pos1);
|
|
|
|
}
|
2009-07-25 13:58:34 +02:00
|
|
|
|
2009-07-30 10:10:34 +02:00
|
|
|
while (! cfg.empty())
|
|
|
|
{
|
|
|
|
if (cfg.find(";") == std::string::npos)
|
|
|
|
return bool(cfg == def);
|
|
|
|
std::string _cfg = cfg.substr(0, cfg.find(";"));
|
|
|
|
if (_cfg == def)
|
|
|
|
return true;
|
|
|
|
cfg.erase(0, cfg.find(";") + 1);
|
|
|
|
}
|
|
|
|
*/
|
2009-01-24 20:28:30 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
std::string::size_type pos2 = cfg.find_first_of(";=", pos);
|
|
|
|
if (pos2 == std::string::npos)
|
|
|
|
{
|
|
|
|
cfgmap[cfg.substr(pos)] = "";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (cfg[pos2] == ';')
|
|
|
|
{
|
|
|
|
cfgmap[cfg.substr(pos, pos2-pos)] = "";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::string::size_type pos3 = pos2;
|
|
|
|
pos2 = cfg.find(";", pos2);
|
|
|
|
if (pos2 == std::string::npos)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
while (getline(istr, line))
|
|
|
|
{
|
2009-08-26 21:54:43 +02:00
|
|
|
if (line.substr(0, 11) == "#pragma asm")
|
2009-08-10 20:07:55 +02:00
|
|
|
{
|
|
|
|
ret << "\n";
|
|
|
|
bool found_end = false;
|
|
|
|
while (getline(istr, line))
|
|
|
|
{
|
|
|
|
ret << "\n";
|
2009-08-26 21:54:43 +02:00
|
|
|
if (line.substr(0, 14) == "#pragma endasm")
|
2009-08-10 20:07:55 +02:00
|
|
|
{
|
|
|
|
found_end = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found_end)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-24 20:28:30 +01:00
|
|
|
std::string def = getdef(line, true);
|
|
|
|
std::string ndef = getdef(line, false);
|
|
|
|
|
2009-07-25 13:58:34 +02:00
|
|
|
if (line.substr(0, 8) == "#define " && line.find("(", 8) == std::string::npos)
|
2009-07-22 18:47:50 +02:00
|
|
|
{
|
2009-07-25 13:58:34 +02:00
|
|
|
std::string::size_type pos = line.find(" ", 8);
|
|
|
|
if (pos == std::string::npos)
|
2009-07-30 10:10:34 +02:00
|
|
|
cfgmap[line.substr(8)] = "";
|
2009-07-25 13:58:34 +02:00
|
|
|
else
|
2009-07-30 10:10:34 +02:00
|
|
|
cfgmap[line.substr(8, pos - 8)] = line.substr(pos + 1);
|
2009-07-22 18:47:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
else if (line.find("#elif ") == 0)
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
|
|
|
if (matched_ifdef.back())
|
|
|
|
{
|
|
|
|
matching_ifdef.back() = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-07-30 10:10:34 +02:00
|
|
|
if (match_cfg_def(cfgmap, def))
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
|
|
|
matching_ifdef.back() = true;
|
|
|
|
matched_ifdef.back() = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (! def.empty())
|
|
|
|
{
|
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());
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (! ndef.empty())
|
|
|
|
{
|
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());
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (line == "#else")
|
|
|
|
{
|
|
|
|
if (! matched_ifdef.empty())
|
|
|
|
matching_ifdef.back() = ! matched_ifdef.back();
|
|
|
|
}
|
|
|
|
|
2009-03-18 00:10:26 +01:00
|
|
|
else if (line.compare(0, 6, "#endif") == 0)
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
|
|
|
if (! matched_ifdef.empty())
|
|
|
|
matched_ifdef.pop_back();
|
|
|
|
if (! matching_ifdef.empty())
|
|
|
|
matching_ifdef.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!line.empty() && line[0] == '#')
|
|
|
|
{
|
|
|
|
match = true;
|
|
|
|
for (std::list<bool>::const_iterator it = matching_ifdef.begin(); it != matching_ifdef.end(); ++it)
|
|
|
|
match &= bool(*it);
|
|
|
|
}
|
|
|
|
|
2009-05-17 18:51:29 +02:00
|
|
|
|
|
|
|
if (!match && line.find("#define") == 0)
|
|
|
|
{
|
|
|
|
// Remove define that is not part of this configuration
|
|
|
|
line = "";
|
|
|
|
}
|
|
|
|
else if (line.find("#file \"") == 0 ||
|
|
|
|
line.find("#endfile") == 0 ||
|
2009-06-14 06:21:20 +02:00
|
|
|
line.find("#define") == 0 ||
|
|
|
|
line.find("#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.
|
|
|
|
}
|
|
|
|
else if (!match ||
|
2009-03-15 13:23:12 +01:00
|
|
|
line[0] == '#')
|
|
|
|
{
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2009-05-22 23:18:48 +02:00
|
|
|
int 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("<\"");
|
2009-01-24 20:28:30 +01:00
|
|
|
if (i == std::string::npos)
|
2009-05-22 23:18:48 +02:00
|
|
|
{
|
|
|
|
str = "";
|
|
|
|
return 0;
|
|
|
|
}
|
2009-01-24 20:28:30 +01:00
|
|
|
|
2009-05-22 22:59:07 +02:00
|
|
|
char c = str[i];
|
|
|
|
if (c == '<')
|
|
|
|
c = '>';
|
|
|
|
|
2009-01-24 20:28:30 +01:00
|
|
|
for (i = i + 1; i < str.length(); ++i)
|
|
|
|
{
|
2009-05-22 22:59:07 +02:00
|
|
|
if (str[i] == c)
|
2009-01-24 20:28:30 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
result.append(1, str[i]);
|
|
|
|
}
|
|
|
|
|
2009-05-22 23:18:48 +02:00
|
|
|
str = result;
|
|
|
|
if (c == '"')
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 2;
|
2009-01-24 20:28:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Preprocessor::handleIncludes(std::string &code, const std::string &filename, const std::list<std::string> &includePaths)
|
|
|
|
{
|
2009-04-05 20:14:02 +02:00
|
|
|
std::list<std::string> paths;
|
|
|
|
std::string path;
|
|
|
|
path = filename;
|
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;
|
2009-01-24 20:28:30 +01:00
|
|
|
std::map<std::string, bool> handledFiles;
|
2009-04-05 20:14:02 +02:00
|
|
|
endfilePos = pos;
|
2009-01-24 20:28:30 +01:00
|
|
|
while ((pos = code.find("#include", pos)) != std::string::npos)
|
|
|
|
{
|
2009-04-05 20:14:02 +02:00
|
|
|
|
2009-01-24 20:28:30 +01:00
|
|
|
// Accept only includes that are at the start of a line
|
|
|
|
if (pos > 0 && code[pos-1] != '\n')
|
|
|
|
{
|
|
|
|
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.
|
|
|
|
while ((endfilePos = code.find("#endfile", endfilePos)) != std::string::npos && endfilePos < pos)
|
|
|
|
{
|
|
|
|
paths.pop_back();
|
|
|
|
endfilePos += 8; // size of #endfile
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2009-05-22 23:18:48 +02:00
|
|
|
int headerType = getHeaderFileName(filename);
|
|
|
|
if (headerType == 0)
|
2009-01-24 20:28:30 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
std::string tempFile = filename;
|
|
|
|
std::transform(tempFile.begin(), tempFile.end(), tempFile.begin(), static_cast < int(*)(int) > (std::tolower));
|
|
|
|
if (handledFiles.find(tempFile) != handledFiles.end())
|
|
|
|
{
|
|
|
|
// We have processed this file already once, skip
|
|
|
|
// it this time to avoid ethernal loop.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
handledFiles[ tempFile ] = true;
|
|
|
|
|
|
|
|
// filename contains now a file name e.g. "menu.h"
|
|
|
|
std::string processedFile;
|
|
|
|
for (std::list<std::string>::const_iterator iter = includePaths.begin(); iter != includePaths.end(); ++iter)
|
|
|
|
{
|
|
|
|
std::ifstream fin;
|
|
|
|
fin.open((*iter + filename).c_str());
|
|
|
|
if (fin.is_open())
|
|
|
|
{
|
|
|
|
filename = *iter + filename;
|
|
|
|
processedFile = Preprocessor::read(fin);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-22 23:18:48 +02:00
|
|
|
if (headerType == 1 && processedFile.length() == 0)
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
2009-04-05 20:14:02 +02:00
|
|
|
filename = paths.back() + filename;
|
2009-01-24 20:28:30 +01:00
|
|
|
std::ifstream fin(filename.c_str());
|
2009-04-05 20:14:02 +02:00
|
|
|
if (fin.is_open())
|
|
|
|
processedFile = Preprocessor::read(fin);
|
2009-01-24 20:28:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (processedFile.length() > 0)
|
|
|
|
{
|
|
|
|
// Replace all tabs with spaces..
|
|
|
|
std::replace(processedFile.begin(), processedFile.end(), '\t', ' ');
|
|
|
|
|
|
|
|
// Remove all indentation..
|
|
|
|
if (!processedFile.empty() && processedFile[0] == ' ')
|
|
|
|
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
|
|
|
}
|
2009-07-25 21:10:30 +02:00
|
|
|
else
|
|
|
|
{
|
2009-07-26 22:15:29 +02:00
|
|
|
if (headerType == 1 && _errorLogger && _settings && _settings->_verbose)
|
2009-07-25 21:10:30 +02:00
|
|
|
{
|
2009-07-26 22:15:29 +02:00
|
|
|
_errorLogger->reportOut("Include file: \"" + filename + "\" not found.");
|
2009-07-25 21:10:30 +02:00
|
|
|
}
|
|
|
|
}
|
2009-01-24 20:28:30 +01: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:
|
|
|
|
Tokenizer tokenizer;
|
|
|
|
std::vector<std::string> _params;
|
|
|
|
std::string _name;
|
|
|
|
std::string _macro;
|
2009-02-13 14:31:40 +01:00
|
|
|
bool _variadic;
|
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-01-24 20:28:30 +01:00
|
|
|
public:
|
2009-07-08 17:14:34 +02:00
|
|
|
PreprocessorMacro(const std::string ¯o)
|
2009-01-24 20:28:30 +01:00
|
|
|
: _macro(macro)
|
|
|
|
{
|
|
|
|
// 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..
|
|
|
|
if (tokens() && tokens()->isName())
|
|
|
|
_name = tokens()->str();
|
|
|
|
|
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(" (");
|
|
|
|
if (pos != std::string::npos && macro[pos] == '(')
|
|
|
|
{
|
|
|
|
// Extract macro parameters
|
|
|
|
if (Token::Match(tokens(), "%var% ( %var%"))
|
|
|
|
{
|
|
|
|
for (const Token *tok = tokens()->tokAt(2); tok; tok = tok->next())
|
|
|
|
{
|
|
|
|
if (tok->str() == ")")
|
|
|
|
break;
|
2009-05-21 17:55:52 +02:00
|
|
|
if (Token::simpleMatch(tok, ". . . )"))
|
2009-02-13 14:31:40 +01:00
|
|
|
{
|
|
|
|
_variadic = true;
|
|
|
|
break;
|
|
|
|
}
|
2009-01-24 20:28:30 +01:00
|
|
|
if (tok->isName())
|
|
|
|
_params.push_back(tok->str());
|
|
|
|
}
|
|
|
|
}
|
2009-06-18 23:09:11 +02:00
|
|
|
|
|
|
|
else if (Token::Match(tokens(), "%var% ( . . . )"))
|
|
|
|
_variadic = true;
|
2009-06-19 16:42:47 +02:00
|
|
|
|
|
|
|
else if (Token::Match(tokens(), "%var% ( )"))
|
|
|
|
_nopar = true;
|
2009-01-24 20:28:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const Token *tokens() const
|
|
|
|
{
|
|
|
|
return tokenizer.tokens();
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::vector<std::string> params() const
|
|
|
|
{
|
|
|
|
return _params;
|
|
|
|
}
|
|
|
|
|
2009-02-13 14:31:40 +01:00
|
|
|
bool variadic() const
|
|
|
|
{
|
|
|
|
return _variadic;
|
|
|
|
}
|
|
|
|
|
2009-06-19 16:42:47 +02:00
|
|
|
bool nopar() const
|
|
|
|
{
|
|
|
|
return _nopar;
|
|
|
|
}
|
|
|
|
|
2009-01-24 20:28:30 +01:00
|
|
|
const std::string &name() const
|
|
|
|
{
|
|
|
|
return _name;
|
|
|
|
}
|
|
|
|
|
2009-05-18 22:32:04 +02:00
|
|
|
bool code(const std::vector<std::string> ¶ms2, std::string ¯ocode) const
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
2009-06-19 16:42:47 +02:00
|
|
|
if (_nopar)
|
|
|
|
{
|
|
|
|
macrocode = _macro.substr(1 + _macro.find(")"));
|
|
|
|
if (macrocode.empty())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
std::string::size_type pos = 0;
|
|
|
|
// Remove leading spaces
|
|
|
|
if ((pos = macrocode.find_first_not_of(" ")) > 0)
|
|
|
|
macrocode.erase(0, pos);
|
|
|
|
// Remove ending newline
|
|
|
|
if ((pos = macrocode.find_first_of("\r\n")) != std::string::npos)
|
|
|
|
macrocode.erase(pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (_params.empty() && _variadic)
|
2009-06-18 23:09:11 +02:00
|
|
|
{
|
|
|
|
std::string s;
|
|
|
|
for (unsigned int i = 0; i < params2.size(); ++i)
|
|
|
|
{
|
|
|
|
if (i > 0)
|
|
|
|
s += ",";
|
|
|
|
s += params2[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
macrocode = _macro.substr(1 + _macro.find(")"));
|
|
|
|
if (macrocode.empty())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
std::string::size_type pos = 0;
|
|
|
|
// Remove leading spaces
|
|
|
|
if ((pos = macrocode.find_first_not_of(" ")) > 0)
|
2009-06-19 15:47:40 +02:00
|
|
|
macrocode.erase(0, pos);
|
2009-06-18 23:09:11 +02:00
|
|
|
// Remove ending newline
|
|
|
|
if ((pos = macrocode.find_first_of("\r\n")) != std::string::npos)
|
|
|
|
macrocode.erase(pos);
|
|
|
|
// Replace "__VA_ARGS__" with parameters
|
2009-07-20 15:42:40 +02:00
|
|
|
pos = 0;
|
|
|
|
while ((pos = macrocode.find("__VA_ARGS__", pos)) != std::string::npos)
|
2009-06-18 23:09:11 +02:00
|
|
|
{
|
|
|
|
macrocode.erase(pos, 11);
|
|
|
|
macrocode.insert(pos, s);
|
2009-07-20 15:42:40 +02:00
|
|
|
pos += s.length();
|
2009-06-18 23:09:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (_params.empty())
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
|
|
|
std::string::size_type pos = _macro.find(" ");
|
|
|
|
if (pos == std::string::npos)
|
|
|
|
macrocode = "";
|
|
|
|
else
|
|
|
|
{
|
|
|
|
macrocode = _macro.substr(pos + 1);
|
|
|
|
if ((pos = macrocode.find_first_of("\r\n")) != std::string::npos)
|
|
|
|
macrocode.erase(pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const Token *tok = tokens();
|
|
|
|
while (tok && tok->str() != ")")
|
|
|
|
tok = tok->next();
|
|
|
|
if (tok)
|
|
|
|
{
|
2009-02-13 14:34:24 +01:00
|
|
|
bool optcomma = false;
|
2009-01-24 20:28:30 +01:00
|
|
|
while ((tok = tok->next()) != NULL)
|
|
|
|
{
|
|
|
|
std::string str = tok->str();
|
|
|
|
if (str == "##")
|
|
|
|
continue;
|
2009-01-25 14:30:15 +01:00
|
|
|
if (str[0] == '#' || tok->isName())
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
2009-01-25 14:30:15 +01:00
|
|
|
bool stringify = false;
|
|
|
|
if (str[0] == '#')
|
|
|
|
{
|
|
|
|
str = str.erase(0, 1);
|
|
|
|
stringify = true;
|
|
|
|
}
|
2009-01-24 20:28:30 +01:00
|
|
|
for (unsigned int i = 0; i < _params.size(); ++i)
|
|
|
|
{
|
|
|
|
if (str == _params[i])
|
|
|
|
{
|
2009-02-13 14:31:40 +01:00
|
|
|
if (_variadic && i == _params.size() - 1)
|
|
|
|
{
|
|
|
|
str = "";
|
|
|
|
for (unsigned int j = _params.size() - 1; j < params2.size(); ++j)
|
|
|
|
{
|
2009-02-13 14:34:24 +01: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;
|
2009-02-13 14:31:40 +01:00
|
|
|
str += params2[j];
|
|
|
|
}
|
|
|
|
}
|
2009-05-18 22:32:04 +02:00
|
|
|
else if (i >= params2.size())
|
|
|
|
{
|
|
|
|
// Macro had more parameters than caller used.
|
|
|
|
macrocode = "";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if (stringify)
|
2009-01-25 14:30:15 +01:00
|
|
|
str = "\"" + params2[i] + "\"";
|
|
|
|
else
|
|
|
|
str = params2[i];
|
2009-05-18 22:32:04 +02:00
|
|
|
|
2009-01-24 20:28:30 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-07-27 11:24:24 +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;
|
|
|
|
if (Token::Match(tok, "%type% %var%"))
|
|
|
|
macrocode += " ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-18 22:32:04 +02:00
|
|
|
return true;
|
2009-01-24 20:28:30 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-04-03 21:09:12 +02:00
|
|
|
std::string Preprocessor::expandMacros(std::string code, const std::string &filename, ErrorLogger *errorLogger)
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
|
|
|
// Search for macros and expand them..
|
2009-08-22 13:03:52 +02:00
|
|
|
std::string::size_type defpos = std::string::npos;
|
|
|
|
while ((defpos > 0) && ((defpos = code.rfind("#define ", defpos)) != std::string::npos))
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
|
|
|
if (defpos > 0 && code[defpos-1] != '\n')
|
|
|
|
{
|
2009-08-22 13:03:52 +02:00
|
|
|
defpos--;
|
2009-01-24 20:28:30 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get macro..
|
|
|
|
std::string::size_type endpos = code.find("\n", defpos + 6);
|
|
|
|
if (endpos == std::string::npos)
|
|
|
|
{
|
|
|
|
code.erase(defpos);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract the whole macro into a separate variable "macro" and then erase it from "code"
|
2009-07-08 17:14:34 +02:00
|
|
|
const PreprocessorMacro macro(code.substr(defpos + 8, endpos - defpos - 7));
|
2009-01-24 20:28:30 +01:00
|
|
|
code.erase(defpos, endpos - defpos);
|
|
|
|
|
|
|
|
// No macro name => continue
|
|
|
|
if (macro.name() == "")
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Expand all macros in the code..
|
|
|
|
char pattern[5] = "\"'# ";
|
|
|
|
pattern[3] = macro.name().at(0);
|
|
|
|
std::string::size_type pos1 = defpos;
|
|
|
|
while ((pos1 = code.find_first_of(pattern, pos1 + 1)) != std::string::npos)
|
|
|
|
{
|
|
|
|
char ch = code[pos1];
|
|
|
|
|
|
|
|
// #undef => break
|
|
|
|
if (code[pos1] == '#')
|
|
|
|
{
|
|
|
|
// Are we at a #undef or #define?
|
|
|
|
if (code.substr(pos1, 7) == "#undef ")
|
|
|
|
pos1 += 7;
|
|
|
|
else if (code.substr(pos1, 8) == "#define ")
|
|
|
|
pos1 += 8;
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Compare the macroname with the macroname we're currently parsing (macro.name())
|
|
|
|
// If it's the same macroname.. break.
|
|
|
|
std::string::size_type pos = pos1 + macro.name().length();
|
|
|
|
if (pos < code.length()
|
|
|
|
&& code.substr(pos1, macro.name().length()) == macro.name()
|
|
|
|
&& !std::isalnum(code[pos]) && code[pos] != '_')
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// String or char..
|
|
|
|
if (code[pos1] == '\"' || code[pos1] == '\'')
|
|
|
|
{
|
2009-04-26 11:05:32 +02:00
|
|
|
// Find the end of the string/char..
|
2009-01-24 20:28:30 +01:00
|
|
|
++pos1;
|
2009-04-26 11:05:32 +02:00
|
|
|
while (pos1 < code.size() && code[pos1] != ch && code[pos1] != '\n')
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
|
|
|
if (code[pos1] == '\\')
|
|
|
|
++pos1;
|
|
|
|
++pos1;
|
2009-04-26 11:05:32 +02:00
|
|
|
}
|
2009-01-24 20:28:30 +01:00
|
|
|
|
2009-04-26 11:05:32 +02:00
|
|
|
// End of line/file was reached without finding pair
|
|
|
|
if (pos1 >= code.size() || code[pos1] == '\n')
|
|
|
|
{
|
2009-04-26 21:19:08 +02:00
|
|
|
std::string::size_type lineStart = code.rfind('\n', pos1 - 1);
|
|
|
|
if (lineStart != std::string::npos)
|
|
|
|
{
|
|
|
|
if (code.substr(lineStart + 1, 7) == "#define")
|
|
|
|
{
|
|
|
|
// There is nothing wrong #define containing quote without
|
|
|
|
// a pair.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-19 21:19:15 +02:00
|
|
|
writeError(filename,
|
|
|
|
code,
|
|
|
|
pos1,
|
|
|
|
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-04-26 11:05:32 +02:00
|
|
|
|
|
|
|
return "";
|
2009-01-24 20:28:30 +01:00
|
|
|
}
|
2009-04-26 11:05:32 +02:00
|
|
|
|
2009-01-24 20:28:30 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Matching the macroname?
|
|
|
|
const std::string substr(code.substr(pos1, macro.name().length()));
|
|
|
|
if (code.substr(pos1, macro.name().length()) != macro.name())
|
|
|
|
continue;
|
|
|
|
|
2009-06-18 23:09:11 +02:00
|
|
|
// Previous char must not be alphanumeric nor '_'
|
2009-01-24 20:28:30 +01:00
|
|
|
if (pos1 != 0 && (std::isalnum(code[pos1-1]) || code[pos1-1] == '_'))
|
|
|
|
continue;
|
|
|
|
|
2009-06-18 23:09:11 +02:00
|
|
|
// The char after the macroname must not be alphanumeric nor '_'
|
2009-01-24 20:28:30 +01:00
|
|
|
if (pos1 + macro.name().length() < code.length())
|
|
|
|
{
|
|
|
|
std::string::size_type pos2 = pos1 + macro.name().length();
|
|
|
|
if (std::isalnum(code[pos2]) || code[pos2] == '_')
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> params;
|
|
|
|
std::string::size_type pos2 = pos1 + macro.name().length();
|
|
|
|
if (macro.params().size() && pos2 >= code.length())
|
|
|
|
continue;
|
2009-03-09 20:29:25 +01:00
|
|
|
|
|
|
|
unsigned int numberOfNewlines = 0;
|
|
|
|
|
2009-06-19 16:42:47 +02:00
|
|
|
if (macro.variadic() || macro.nopar() || macro.params().size())
|
2009-01-24 20:28:30 +01:00
|
|
|
{
|
2009-05-24 22:57:12 +02:00
|
|
|
if (code[pos2] == ' ')
|
|
|
|
pos2++;
|
|
|
|
|
2009-01-24 20:28:30 +01:00
|
|
|
if (code[pos2] != '(')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
int parlevel = 0;
|
|
|
|
std::string par;
|
|
|
|
for (; pos2 < code.length(); ++pos2)
|
|
|
|
{
|
|
|
|
if (code[pos2] == '(')
|
|
|
|
{
|
|
|
|
++parlevel;
|
|
|
|
if (parlevel == 1)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (code[pos2] == ')')
|
|
|
|
{
|
|
|
|
--parlevel;
|
|
|
|
if (parlevel <= 0)
|
|
|
|
{
|
|
|
|
params.push_back(par);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (code[pos2] == '\"' || code[pos2] == '\'')
|
|
|
|
{
|
|
|
|
par += code[pos2];
|
|
|
|
char ch = code[pos2];
|
|
|
|
++pos2;
|
|
|
|
while (pos2 < code.length() && code[pos2] != ch)
|
|
|
|
{
|
2009-06-05 22:45:31 +02:00
|
|
|
if (code[pos2] == '\\')
|
|
|
|
{
|
|
|
|
par += code[pos2];
|
|
|
|
++pos2;
|
|
|
|
}
|
|
|
|
|
2009-01-24 20:28:30 +01:00
|
|
|
par += code[pos2];
|
|
|
|
++pos2;
|
|
|
|
}
|
|
|
|
if (pos2 == code.length())
|
|
|
|
break;
|
|
|
|
par += code[pos2];
|
|
|
|
continue;
|
|
|
|
}
|
2009-03-09 20:29:25 +01:00
|
|
|
else if (code[pos2] == '\n')
|
|
|
|
{
|
|
|
|
++numberOfNewlines;
|
2009-05-05 17:19:06 +02:00
|
|
|
continue;
|
2009-03-09 20:29:25 +01:00
|
|
|
}
|
2009-01-24 20:28:30 +01:00
|
|
|
|
|
|
|
if (parlevel == 1 && code[pos2] == ',')
|
|
|
|
{
|
|
|
|
params.push_back(par);
|
|
|
|
par = "";
|
2009-03-16 22:20:55 +01:00
|
|
|
}
|
|
|
|
else if (code[pos2] == ' ')
|
|
|
|
{
|
2009-08-01 14:55:45 +02:00
|
|
|
// Add space only if it is needed
|
|
|
|
if (par.size() && std::isalnum(par[par.length()-1]))
|
|
|
|
{
|
|
|
|
par += ' ';
|
|
|
|
}
|
2009-01-24 20:28:30 +01:00
|
|
|
}
|
|
|
|
else if (parlevel >= 1)
|
|
|
|
{
|
2009-03-16 22:20:55 +01:00
|
|
|
par.append(1, code[pos2]);
|
2009-01-24 20:28:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-19 16:42:47 +02:00
|
|
|
if (params.size() == 1 && params[0] == "")
|
|
|
|
params.clear();
|
|
|
|
|
2009-01-24 20:28:30 +01:00
|
|
|
// Same number of parameters..
|
2009-02-13 14:31:40 +01:00
|
|
|
if (!macro.variadic() && params.size() != macro.params().size())
|
2009-01-24 20:28:30 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// Create macro code..
|
2009-05-18 22:32:04 +02:00
|
|
|
std::string tempMacro;
|
|
|
|
if (!macro.code(params, tempMacro))
|
|
|
|
{
|
|
|
|
// Syntax error in code
|
|
|
|
|
|
|
|
|
2009-05-19 21:19:15 +02:00
|
|
|
writeError(filename,
|
|
|
|
code,
|
|
|
|
pos1,
|
|
|
|
errorLogger,
|
|
|
|
"syntaxError",
|
|
|
|
std::string("Syntax error. Not enough parameters for macro '") + macro.name() + "'.");
|
2009-05-18 22:32:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string macrocode(std::string(numberOfNewlines, '\n') + tempMacro);
|
2009-01-24 20:28:30 +01:00
|
|
|
|
|
|
|
// Insert macro code..
|
2009-06-19 16:42:47 +02:00
|
|
|
if (macro.variadic() || macro.nopar() || !macro.params().empty())
|
2009-01-24 20:28:30 +01:00
|
|
|
++pos2;
|
|
|
|
|
|
|
|
code.erase(pos1, pos2 - pos1);
|
|
|
|
code.insert(pos1, macrocode);
|
|
|
|
pos1 += macrocode.length() - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove all #undef..
|
|
|
|
defpos = 0;
|
|
|
|
while ((defpos = code.find("\n#undef ", defpos)) != std::string::npos)
|
|
|
|
{
|
|
|
|
++defpos;
|
|
|
|
std::string::size_type pos2 = code.find("\n", defpos);
|
|
|
|
code.erase(defpos, pos2 - defpos);
|
|
|
|
}
|
|
|
|
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|