Skip to content

Commit 629ea10

Browse files
committed
fixed #27 - handling of -include
1 parent 5f2b20b commit 629ea10

4 files changed

Lines changed: 71 additions & 9 deletions

File tree

main.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
#include <fstream>
55
#include <iostream>
6-
6+
#include <cstring>
77

88
int main(int argc, char **argv) {
99
const char *filename = NULL;
@@ -14,19 +14,23 @@ int main(int argc, char **argv) {
1414
const char *arg = argv[i];
1515
if (*arg == '-') {
1616
char c = arg[1];
17-
if (c != 'D' && c != 'U' && c != 'I')
17+
if (c != 'D' && c != 'U' && c != 'I' && c != 'i')
1818
continue; // Ignored
1919
const char *value = arg[2] ? (argv[i] + 2) : argv[++i];
2020
switch (c) {
21-
case 'D':
21+
case 'D': // define symbol
2222
dui.defines.push_back(value);
2323
break;
24-
case 'U':
24+
case 'U': // undefine symbol
2525
dui.undefined.insert(value);
2626
break;
27-
case 'I':
27+
case 'I': // include path
2828
dui.includePaths.push_back(value);
2929
break;
30+
case 'i':
31+
if (std::strncmp(arg, "-include=",9)==0)
32+
dui.includes.push_back(arg+9);
33+
break;
3034
};
3135
} else {
3236
filename = arg;
@@ -36,9 +40,10 @@ int main(int argc, char **argv) {
3640
if (!filename) {
3741
std::cout << "Syntax:" << std::endl;
3842
std::cout << "simplecpp [options] filename" << std::endl;
39-
std::cout << " -D Define NAME." << std::endl;
40-
std::cout << " -I Include path." << std::endl;
41-
std::cout << " -U Undefine NAME." << std::endl;
43+
std::cout << " -DNAME Define NAME." << std::endl;
44+
std::cout << " -IPATH Include path." << std::endl;
45+
std::cout << " -include=FILE Include FILE." << std::endl;
46+
std::cout << " -UNAME Undefine NAME." << std::endl;
4247
std::exit(0);
4348
}
4449

simplecpp.cpp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1615,6 +1615,25 @@ std::map<std::string, simplecpp::TokenList*> simplecpp::load(const simplecpp::To
16151615

16161616
std::list<const Token *> filelist;
16171617

1618+
// -include files
1619+
for (std::list<std::string>::const_iterator it = dui.includes.begin(); it != dui.includes.end(); ++it) {
1620+
if (ret.find(*it) != ret.end())
1621+
continue;
1622+
1623+
std::ifstream fin(it->c_str());
1624+
if (!fin.is_open())
1625+
continue;
1626+
1627+
TokenList *tokenlist = new TokenList(fin, fileNumbers, *it, outputList);
1628+
if (!tokenlist->front()) {
1629+
delete tokenlist;
1630+
continue;
1631+
}
1632+
1633+
ret[*it] = tokenlist;
1634+
filelist.push_back(tokenlist->front());
1635+
}
1636+
16181637
for (const Token *rawtok = rawtokens.cfront(); rawtok || !filelist.empty(); rawtok = rawtok ? rawtok->next : NULL) {
16191638
if (rawtok == NULL) {
16201639
rawtok = filelist.back();
@@ -1710,7 +1729,14 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
17101729

17111730
std::set<std::string> pragmaOnce;
17121731

1713-
for (const Token *rawtok = rawtokens.cfront(); rawtok || !includetokenstack.empty();) {
1732+
includetokenstack.push(rawtokens.cfront());
1733+
for (std::list<std::string>::const_iterator it = dui.includes.begin(); it != dui.includes.end(); ++it) {
1734+
const std::map<std::string, TokenList*>::const_iterator f = filedata.find(*it);
1735+
if (f != filedata.end())
1736+
includetokenstack.push(f->second->cfront());
1737+
}
1738+
1739+
for (const Token *rawtok = NULL; rawtok || !includetokenstack.empty();) {
17141740
if (rawtok == NULL) {
17151741
rawtok = includetokenstack.top();
17161742
includetokenstack.pop();

simplecpp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ struct SIMPLECPP_LIB DUI {
271271
std::list<std::string> defines;
272272
std::set<std::string> undefined;
273273
std::list<std::string> includePaths;
274+
std::list<std::string> includes;
274275
};
275276

276277
SIMPLECPP_LIB std::map<std::string, TokenList*> load(const TokenList &rawtokens, std::vector<std::string> &filenames, const DUI &dui, OutputList *outputList = 0);

test.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,35 @@ void include3() { // #16 - crash when expanding macro from header
671671
}
672672

673673

674+
void include4() { // #27 - -include
675+
const char code_c[] = "X\n" ;
676+
const char code_h[] = "#define X 123\n";
677+
678+
std::vector<std::string> files;
679+
680+
std::istringstream istr_c(code_c);
681+
simplecpp::TokenList rawtokens_c(istr_c, files, "27.c");
682+
683+
std::istringstream istr_h(code_h);
684+
simplecpp::TokenList rawtokens_h(istr_h, files, "27.h");
685+
686+
ASSERT_EQUALS(2U, files.size());
687+
ASSERT_EQUALS("27.c", files[0]);
688+
ASSERT_EQUALS("27.h", files[1]);
689+
690+
std::map<std::string, simplecpp::TokenList *> filedata;
691+
filedata["27.c"] = &rawtokens_c;
692+
filedata["27.h"] = &rawtokens_h;
693+
694+
simplecpp::TokenList out(files);
695+
simplecpp::DUI dui;
696+
dui.includes.push_back("27.h");
697+
simplecpp::preprocess(out, rawtokens_c, files, filedata, dui);
698+
699+
ASSERT_EQUALS("123", out.stringify());
700+
}
701+
702+
674703
void readfile_string() {
675704
const char code[] = "A = \"abc\'def\"";
676705
ASSERT_EQUALS("A = \"abc\'def\"", readfile(code));
@@ -858,6 +887,7 @@ int main(int argc, char **argv) {
858887
TEST_CASE(include1);
859888
TEST_CASE(include2);
860889
TEST_CASE(include3);
890+
TEST_CASE(include4); // -include
861891

862892
TEST_CASE(readfile_string);
863893
TEST_CASE(readfile_rawstring);

0 commit comments

Comments
 (0)