Skip to content

Commit 20ed366

Browse files
committed
1
1 parent 09a8163 commit 20ed366

3 files changed

Lines changed: 88 additions & 6 deletions

File tree

main.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ int main(int argc, char **argv)
1717
{
1818
bool error = false;
1919
const char *filename = nullptr;
20+
bool precompile = false;
2021
bool use_istream = false;
2122

2223
// Settings..
@@ -66,6 +67,12 @@ int main(int argc, char **argv)
6667
quiet = true;
6768
found = true;
6869
break;
70+
case 'p':
71+
if (std::strcmp(arg, "-pc")==0) {
72+
precompile = true;
73+
found = true;
74+
}
75+
break;
6976
case 'e':
7077
error_only = true;
7178
found = true;
@@ -102,6 +109,7 @@ int main(int argc, char **argv)
102109
std::cout << " -q Quiet mode (no output)." << std::endl;
103110
std::cout << " -is Use std::istream interface." << std::endl;
104111
std::cout << " -e Output errors only." << std::endl;
112+
std::cout << " -pc Precompile header." << std::endl;
105113
std::exit(0);
106114
}
107115

@@ -123,15 +131,20 @@ int main(int argc, char **argv)
123131
}
124132
rawtokens->removeComments();
125133
simplecpp::TokenList outputTokens(files);
126-
std::map<std::string, simplecpp::TokenList*> filedata;
127-
simplecpp::preprocess(outputTokens, *rawtokens, files, filedata, dui, &outputList);
128-
simplecpp::cleanup(filedata);
134+
if (precompile) {
135+
std::ofstream f(filename + std::string(".pc"));
136+
f << simplecpp::precompileHeader(*rawtokens, files, dui, &outputList);
137+
} else {
138+
std::map<std::string, simplecpp::TokenList*> filedata;
139+
simplecpp::preprocess(outputTokens, *rawtokens, files, filedata, dui, &outputList);
140+
simplecpp::cleanup(filedata);
141+
}
129142
delete rawtokens;
130143
rawtokens = nullptr;
131144

132145
// Output
133146
if (!quiet) {
134-
if (!error_only)
147+
if (!error_only && !precompile)
135148
std::cout << outputTokens.stringify() << std::endl;
136149

137150
for (const simplecpp::Output &output : outputList) {

simplecpp.cpp

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3314,8 +3314,10 @@ static std::string getTimeDefine(const struct tm *timep)
33143314
return std::string("\"").append(buf).append("\"");
33153315
}
33163316

3317-
void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenList &rawtokens, std::vector<std::string> &files, std::map<std::string, simplecpp::TokenList *> &filedata, const simplecpp::DUI &dui, simplecpp::OutputList *outputList, std::list<simplecpp::MacroUsage> *macroUsage, std::list<simplecpp::IfCond> *ifCond)
3317+
static void simplecppPreprocess(simplecpp::TokenList &output, const simplecpp::TokenList &rawtokens, std::vector<std::string> &files, std::map<std::string, simplecpp::TokenList *> &filedata, const simplecpp::DUI &dui, simplecpp::OutputList *outputList, std::list<simplecpp::MacroUsage> *macroUsage, std::list<simplecpp::IfCond> *ifCond, simplecpp::MacroMap& macros)
33183318
{
3319+
using namespace simplecpp;
3320+
33193321
#ifdef SIMPLECPP_WINDOWS
33203322
if (dui.clearIncludeCache)
33213323
nonExistingFilesCache.clear();
@@ -3347,7 +3349,6 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
33473349
std::vector<std::string> dummy;
33483350

33493351
const bool hasInclude = isCpp17OrLater(dui);
3350-
MacroMap macros;
33513352
for (std::list<std::string>::const_iterator it = dui.defines.begin(); it != dui.defines.end(); ++it) {
33523353
const std::string &macrostr = *it;
33533354
const std::string::size_type eq = macrostr.find('=');
@@ -3792,6 +3793,64 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
37923793
}
37933794
}
37943795

3796+
void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenList &rawtokens, std::vector<std::string> &files, std::map<std::string, simplecpp::TokenList *> &filedata, const simplecpp::DUI &dui, simplecpp::OutputList *outputList, std::list<simplecpp::MacroUsage> *macroUsage, std::list<simplecpp::IfCond> *ifCond)
3797+
{
3798+
MacroMap macroMap;
3799+
simplecppPreprocess(output,
3800+
rawtokens,
3801+
files,
3802+
filedata,
3803+
dui,
3804+
outputList,
3805+
macroUsage,
3806+
ifCond,
3807+
macroMap);
3808+
}
3809+
3810+
3811+
std::string simplecpp::precompileHeader(const TokenList &rawtokens, std::vector<std::string> &files, const DUI &dui, OutputList *outputList)
3812+
{
3813+
std::map<std::string, TokenList*> filedata;
3814+
simplecpp::TokenList output(files);
3815+
std::list<simplecpp::MacroUsage> macroUsage;
3816+
std::list<simplecpp::IfCond> ifCond;
3817+
simplecpp::MacroMap macroMap;
3818+
simplecppPreprocess(output,
3819+
rawtokens,
3820+
files,
3821+
filedata,
3822+
dui,
3823+
outputList,
3824+
&macroUsage,
3825+
&ifCond,
3826+
macroMap);
3827+
3828+
std::string ret;
3829+
unsigned int fileIndex = 0;
3830+
unsigned int line = 0;
3831+
unsigned int col = 0;
3832+
for (const simplecpp::Token *tok = output.cfront(); tok; tok = tok->next) {
3833+
ret += "\n";
3834+
if (tok->location.fileIndex != fileIndex) {
3835+
fileIndex = tok->location.fileIndex;
3836+
ret += "f" + toString(fileIndex);
3837+
}
3838+
if (tok->location.line != line) {
3839+
line = tok->location.line;
3840+
ret += "l" + toString(line);
3841+
}
3842+
if (tok->location.col != col) {
3843+
col = tok->location.col;
3844+
ret += "c" + toString(col);
3845+
}
3846+
ret += ":" + tok->str();
3847+
}
3848+
for (simplecpp::MacroMap::const_iterator it = macroMap.begin(); it != macroMap.end(); ++it) {
3849+
ret += "\nM" + it->second.name();
3850+
}
3851+
return ret;
3852+
}
3853+
37953854
void simplecpp::cleanup(std::map<std::string, TokenList*> &filedata)
37963855
{
37973856
for (std::map<std::string, TokenList*>::iterator it = filedata.begin(); it != filedata.end(); ++it)

simplecpp.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,16 @@ namespace simplecpp {
365365
*/
366366
SIMPLECPP_LIB void preprocess(TokenList &output, const TokenList &rawtokens, std::vector<std::string> &files, std::map<std::string, TokenList*> &filedata, const DUI &dui, OutputList *outputList = nullptr, std::list<MacroUsage> *macroUsage = nullptr, std::list<IfCond> *ifCond = nullptr);
367367

368+
/**
369+
* Precompile header
370+
* @param rawtokens Raw tokenlist for top sourcefile
371+
* @param files internal data of simplecpp
372+
* @param dui defines, undefs, and include paths
373+
* @param outputList output: list that will receive output messages
374+
* @return precompiled header data
375+
*/
376+
SIMPLECPP_LIB std::string precompileHeader(const TokenList &rawtokens, std::vector<std::string> &files, const DUI &dui, OutputList *outputList = nullptr);
377+
368378
/**
369379
* Deallocate data
370380
*/

0 commit comments

Comments
 (0)