2016-07-20 12:21:00 +02:00
|
|
|
/*
|
|
|
|
* simplecpp - A simple and high-fidelity C/C++ preprocessor library
|
|
|
|
* Copyright (C) 2016 Daniel Marjamäki.
|
|
|
|
*
|
|
|
|
* This library is free software: you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation, either
|
|
|
|
* version 3 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef simplecppH
|
|
|
|
#define simplecppH
|
|
|
|
|
|
|
|
#include <cctype>
|
|
|
|
#include <istream>
|
|
|
|
#include <list>
|
|
|
|
#include <map>
|
|
|
|
#include <set>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2016-07-21 20:38:58 +02:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
# ifdef SIMPLECPP_EXPORT
|
|
|
|
# define SIMPLECPP_LIB __declspec(dllexport)
|
|
|
|
# elif defined(SIMPLECPP_IMPORT)
|
|
|
|
# define SIMPLECPP_LIB __declspec(dllimport)
|
|
|
|
# else
|
|
|
|
# define SIMPLECPP_LIB
|
|
|
|
# endif
|
|
|
|
#else
|
|
|
|
# define SIMPLECPP_LIB
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2016-07-20 12:21:00 +02:00
|
|
|
namespace simplecpp {
|
|
|
|
|
|
|
|
typedef std::string TokenString;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Location in source code
|
|
|
|
*/
|
2016-07-21 20:38:58 +02:00
|
|
|
class SIMPLECPP_LIB Location {
|
2016-07-20 12:21:00 +02:00
|
|
|
public:
|
2016-11-06 12:29:13 +01:00
|
|
|
explicit Location(const std::vector<std::string> &f) : files(f), fileIndex(0), line(1U), col(0U) {}
|
2016-07-20 12:21:00 +02:00
|
|
|
|
|
|
|
Location &operator=(const Location &other) {
|
|
|
|
if (this != &other) {
|
|
|
|
fileIndex = other.fileIndex;
|
|
|
|
line = other.line;
|
|
|
|
col = other.col;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** increment this location by string */
|
|
|
|
void adjust(const std::string &str);
|
|
|
|
|
|
|
|
bool operator<(const Location &rhs) const {
|
|
|
|
if (fileIndex != rhs.fileIndex)
|
|
|
|
return fileIndex < rhs.fileIndex;
|
|
|
|
if (line != rhs.line)
|
|
|
|
return line < rhs.line;
|
|
|
|
return col < rhs.col;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool sameline(const Location &other) const {
|
|
|
|
return fileIndex == other.fileIndex && line == other.line;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string file() const {
|
|
|
|
return fileIndex < files.size() ? files[fileIndex] : std::string("");
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::vector<std::string> &files;
|
|
|
|
unsigned int fileIndex;
|
|
|
|
unsigned int line;
|
|
|
|
unsigned int col;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* token class.
|
|
|
|
* @todo don't use std::string representation - for both memory and performance reasons
|
|
|
|
*/
|
2016-07-21 20:38:58 +02:00
|
|
|
class SIMPLECPP_LIB Token {
|
2016-07-20 12:21:00 +02:00
|
|
|
public:
|
|
|
|
Token(const TokenString &s, const Location &loc) :
|
2016-07-21 15:05:29 +02:00
|
|
|
str(string), location(loc), previous(NULL), next(NULL), string(s)
|
2016-07-20 12:21:00 +02:00
|
|
|
{
|
|
|
|
flags();
|
|
|
|
}
|
|
|
|
|
|
|
|
Token(const Token &tok) :
|
2016-07-21 15:05:29 +02:00
|
|
|
str(string), macro(tok.macro), location(tok.location), previous(NULL), next(NULL), string(tok.str)
|
2016-07-20 12:21:00 +02:00
|
|
|
{
|
|
|
|
flags();
|
|
|
|
}
|
|
|
|
|
|
|
|
void flags() {
|
2016-07-29 13:05:35 +02:00
|
|
|
name = (std::isalpha((unsigned char)str[0]) || str[0] == '_' || str[0] == '$');
|
2016-07-20 12:21:00 +02:00
|
|
|
comment = (str.compare(0, 2, "//") == 0 || str.compare(0, 2, "/*") == 0);
|
2016-07-29 13:05:35 +02:00
|
|
|
number = std::isdigit((unsigned char)str[0]) || (str.size() > 1U && str[0] == '-' && std::isdigit((unsigned char)str[1]));
|
2016-07-20 12:21:00 +02:00
|
|
|
op = (str.size() == 1U) ? str[0] : '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
void setstr(const std::string &s) {
|
|
|
|
string = s;
|
|
|
|
flags();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isOneOf(const char ops[]) const;
|
|
|
|
bool startsWithOneOf(const char c[]) const;
|
|
|
|
bool endsWithOneOf(const char c[]) const;
|
|
|
|
|
|
|
|
const TokenString &str;
|
|
|
|
TokenString macro;
|
2016-07-29 13:05:35 +02:00
|
|
|
char op;
|
2016-07-20 12:21:00 +02:00
|
|
|
bool comment;
|
|
|
|
bool name;
|
|
|
|
bool number;
|
|
|
|
Location location;
|
|
|
|
Token *previous;
|
|
|
|
Token *next;
|
2016-07-21 18:58:11 +02:00
|
|
|
|
|
|
|
const Token *previousSkipComments() const {
|
|
|
|
const Token *tok = this->previous;
|
|
|
|
while (tok && tok->comment)
|
|
|
|
tok = tok->previous;
|
|
|
|
return tok;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Token *nextSkipComments() const {
|
|
|
|
const Token *tok = this->next;
|
|
|
|
while (tok && tok->comment)
|
|
|
|
tok = tok->next;
|
|
|
|
return tok;
|
|
|
|
}
|
2016-07-25 09:13:59 +02:00
|
|
|
|
|
|
|
void printAll() const;
|
|
|
|
void printOut() const;
|
2016-07-20 12:21:00 +02:00
|
|
|
private:
|
|
|
|
TokenString string;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Output from preprocessor */
|
2016-07-21 20:38:58 +02:00
|
|
|
struct SIMPLECPP_LIB Output {
|
2016-11-06 12:29:13 +01:00
|
|
|
explicit Output(const std::vector<std::string> &files) : type(ERROR), location(files) {}
|
2016-07-20 12:21:00 +02:00
|
|
|
enum Type {
|
2016-07-21 12:47:00 +02:00
|
|
|
ERROR, /* #error */
|
|
|
|
WARNING, /* #warning */
|
2016-07-31 12:10:30 +02:00
|
|
|
MISSING_HEADER,
|
|
|
|
INCLUDE_NESTED_TOO_DEEPLY,
|
2016-09-03 12:29:45 +02:00
|
|
|
SYNTAX_ERROR,
|
|
|
|
PORTABILITY_BACKSLASH
|
2016-07-20 12:21:00 +02:00
|
|
|
} type;
|
|
|
|
Location location;
|
|
|
|
std::string msg;
|
|
|
|
};
|
|
|
|
|
2016-07-29 13:05:35 +02:00
|
|
|
typedef std::list<Output> OutputList;
|
2016-07-20 12:21:00 +02:00
|
|
|
|
|
|
|
/** List of tokens. */
|
2016-07-21 20:38:58 +02:00
|
|
|
class SIMPLECPP_LIB TokenList {
|
2016-07-20 12:21:00 +02:00
|
|
|
public:
|
2016-11-06 12:29:13 +01:00
|
|
|
explicit TokenList(std::vector<std::string> &filenames);
|
2016-07-20 12:21:00 +02:00
|
|
|
TokenList(std::istream &istr, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = 0);
|
|
|
|
TokenList(const TokenList &other);
|
|
|
|
~TokenList();
|
|
|
|
void operator=(const TokenList &other);
|
|
|
|
|
|
|
|
void clear();
|
|
|
|
bool empty() const {
|
2016-07-29 08:46:38 +02:00
|
|
|
return !frontToken;
|
2016-07-20 12:21:00 +02:00
|
|
|
}
|
|
|
|
void push_back(Token *token);
|
|
|
|
|
|
|
|
void dump() const;
|
|
|
|
std::string stringify() const;
|
|
|
|
|
|
|
|
void readfile(std::istream &istr, const std::string &filename=std::string(), OutputList *outputList = 0);
|
|
|
|
void constFold();
|
|
|
|
|
|
|
|
void removeComments();
|
|
|
|
|
2016-07-29 08:46:38 +02:00
|
|
|
Token *front() {
|
|
|
|
return frontToken;
|
2016-07-20 12:21:00 +02:00
|
|
|
}
|
|
|
|
|
2016-07-29 08:46:38 +02:00
|
|
|
const Token *cfront() const {
|
|
|
|
return frontToken;
|
2016-07-20 12:21:00 +02:00
|
|
|
}
|
|
|
|
|
2016-07-29 08:46:38 +02:00
|
|
|
Token *back() {
|
|
|
|
return backToken;
|
2016-07-20 12:21:00 +02:00
|
|
|
}
|
|
|
|
|
2016-07-29 08:46:38 +02:00
|
|
|
const Token *cback() const {
|
|
|
|
return backToken;
|
2016-07-20 12:21:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void deleteToken(Token *tok) {
|
|
|
|
if (!tok)
|
|
|
|
return;
|
|
|
|
Token *prev = tok->previous;
|
|
|
|
Token *next = tok->next;
|
|
|
|
if (prev)
|
|
|
|
prev->next = next;
|
|
|
|
if (next)
|
|
|
|
next->previous = prev;
|
2016-07-29 08:46:38 +02:00
|
|
|
if (frontToken == tok)
|
|
|
|
frontToken = next;
|
|
|
|
if (backToken == tok)
|
|
|
|
backToken = prev;
|
2016-07-20 12:21:00 +02:00
|
|
|
delete tok;
|
|
|
|
}
|
|
|
|
|
2016-07-23 09:26:06 +02:00
|
|
|
void takeTokens(TokenList &other) {
|
2016-07-29 08:46:38 +02:00
|
|
|
if (!other.frontToken)
|
2016-07-23 09:26:06 +02:00
|
|
|
return;
|
2016-07-29 08:46:38 +02:00
|
|
|
if (!frontToken) {
|
|
|
|
frontToken = other.frontToken;
|
2016-07-23 09:26:06 +02:00
|
|
|
} else {
|
2016-07-29 08:46:38 +02:00
|
|
|
backToken->next = other.frontToken;
|
|
|
|
other.frontToken->previous = backToken;
|
2016-07-23 09:26:06 +02:00
|
|
|
}
|
2016-07-29 08:46:38 +02:00
|
|
|
backToken = other.backToken;
|
|
|
|
other.frontToken = other.backToken = NULL;
|
2016-07-23 09:26:06 +02:00
|
|
|
}
|
|
|
|
|
2016-07-20 15:04:03 +02:00
|
|
|
/** sizeof(T) */
|
|
|
|
std::map<std::string, std::size_t> sizeOfType;
|
|
|
|
|
2016-07-20 12:21:00 +02:00
|
|
|
private:
|
|
|
|
void combineOperators();
|
|
|
|
|
|
|
|
void constFoldUnaryNotPosNeg(Token *tok);
|
|
|
|
void constFoldMulDivRem(Token *tok);
|
|
|
|
void constFoldAddSub(Token *tok);
|
|
|
|
void constFoldComparison(Token *tok);
|
|
|
|
void constFoldBitwise(Token *tok);
|
|
|
|
void constFoldLogicalOp(Token *tok);
|
|
|
|
void constFoldQuestionOp(Token **tok);
|
|
|
|
|
|
|
|
std::string readUntil(std::istream &istr, const Location &location, const char start, const char end, OutputList *outputList);
|
|
|
|
|
2016-07-28 09:26:23 +02:00
|
|
|
std::string lastLine(int maxsize=10) const;
|
2016-07-20 12:21:00 +02:00
|
|
|
|
|
|
|
unsigned int fileIndex(const std::string &filename);
|
|
|
|
|
2016-07-29 08:46:38 +02:00
|
|
|
Token *frontToken;
|
|
|
|
Token *backToken;
|
2016-07-20 12:21:00 +02:00
|
|
|
std::vector<std::string> &files;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Tracking how macros are used */
|
2016-07-21 20:38:58 +02:00
|
|
|
struct SIMPLECPP_LIB MacroUsage {
|
2016-11-06 12:29:13 +01:00
|
|
|
explicit MacroUsage(const std::vector<std::string> &f) : macroLocation(f), useLocation(f) {}
|
2016-07-20 12:21:00 +02:00
|
|
|
std::string macroName;
|
|
|
|
Location macroLocation;
|
|
|
|
Location useLocation;
|
|
|
|
};
|
|
|
|
|
2016-07-21 20:38:58 +02:00
|
|
|
struct SIMPLECPP_LIB DUI {
|
2016-07-20 12:21:00 +02:00
|
|
|
std::list<std::string> defines;
|
|
|
|
std::set<std::string> undefined;
|
|
|
|
std::list<std::string> includePaths;
|
2016-08-06 08:41:19 +02:00
|
|
|
std::list<std::string> includes;
|
2016-07-20 12:21:00 +02:00
|
|
|
};
|
|
|
|
|
2016-07-29 13:05:35 +02:00
|
|
|
SIMPLECPP_LIB std::map<std::string, TokenList*> load(const TokenList &rawtokens, std::vector<std::string> &filenames, const DUI &dui, OutputList *outputList = 0);
|
2016-07-20 12:21:00 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Preprocess
|
|
|
|
* @todo simplify interface
|
2016-07-31 20:48:55 +02:00
|
|
|
* @param output TokenList that receives the preprocessing output
|
|
|
|
* @param rawtokens Raw tokenlist for top sourcefile
|
|
|
|
* @param files internal data of simplecpp
|
|
|
|
* @param filedata output from simplecpp::load()
|
|
|
|
* @param dui defines, undefs, and include paths
|
|
|
|
* @param outputList output: list that will receive output messages
|
|
|
|
* @param macroUsage output: macro usage
|
2016-07-20 12:21:00 +02:00
|
|
|
*/
|
2016-07-29 13:05:35 +02:00
|
|
|
SIMPLECPP_LIB void preprocess(TokenList &output, const TokenList &rawtokens, std::vector<std::string> &files, const std::map<std::string, TokenList*> &filedata, const DUI &dui, OutputList *outputList = 0, std::list<MacroUsage> *macroUsage = 0);
|
2016-07-31 20:48:55 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Deallocate data
|
|
|
|
*/
|
|
|
|
SIMPLECPP_LIB void cleanup(std::map<std::string, TokenList*> &filedata);
|
2016-07-20 12:21:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|