Skip to content

Commit 9f7cb3f

Browse files
committed
Add test
1 parent c3dff6a commit 9f7cb3f

3 files changed

Lines changed: 86 additions & 47 deletions

File tree

simplecpp.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3109,15 +3109,13 @@ bool simplecpp::FileDataCache::getFileId(const std::string &path, FileID &id)
31093109
#endif
31103110
}
31113111

3112-
simplecpp::FileDataCache simplecpp::load(const simplecpp::TokenList &rawtokens, std::vector<std::string> &filenames, const simplecpp::DUI &dui, simplecpp::OutputList *outputList)
3112+
simplecpp::FileDataCache simplecpp::load(const simplecpp::TokenList &rawtokens, std::vector<std::string> &filenames, const simplecpp::DUI &dui, simplecpp::OutputList *outputList, FileDataCache cache)
31133113
{
31143114
#ifdef SIMPLECPP_WINDOWS
31153115
if (dui.clearIncludeCache)
31163116
nonExistingFilesCache.clear();
31173117
#endif
31183118

3119-
FileDataCache cache;
3120-
31213119
std::list<const Token *> filelist;
31223120

31233121
// -include files

simplecpp.h

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -353,49 +353,6 @@ namespace simplecpp {
353353
bool removeComments; /** remove comment tokens from included files */
354354
};
355355

356-
SIMPLECPP_LIB long long characterLiteralToLL(const std::string& str);
357-
358-
SIMPLECPP_LIB FileDataCache load(const TokenList &rawtokens, std::vector<std::string> &filenames, const DUI &dui, OutputList *outputList = nullptr);
359-
360-
/**
361-
* Preprocess
362-
* @todo simplify interface
363-
* @param output TokenList that receives the preprocessing output
364-
* @param rawtokens Raw tokenlist for top sourcefile
365-
* @param files internal data of simplecpp
366-
* @param cache output from simplecpp::load()
367-
* @param dui defines, undefs, and include paths
368-
* @param outputList output: list that will receive output messages
369-
* @param macroUsage output: macro usage
370-
* @param ifCond output: #if/#elif expressions
371-
*/
372-
SIMPLECPP_LIB void preprocess(TokenList &output, const TokenList &rawtokens, std::vector<std::string> &files, FileDataCache &cache, const DUI &dui, OutputList *outputList = nullptr, std::list<MacroUsage> *macroUsage = nullptr, std::list<IfCond> *ifCond = nullptr);
373-
374-
/**
375-
* Deallocate data
376-
*/
377-
SIMPLECPP_LIB void cleanup(FileDataCache &cache);
378-
379-
/** Simplify path */
380-
SIMPLECPP_LIB std::string simplifyPath(std::string path);
381-
382-
/** Convert Cygwin path to Windows path */
383-
SIMPLECPP_LIB std::string convertCygwinToWindowsPath(const std::string &cygwinPath);
384-
385-
/** Returns the C version a given standard */
386-
SIMPLECPP_LIB cstd_t getCStd(const std::string &std);
387-
388-
/** Returns the C++ version a given standard */
389-
SIMPLECPP_LIB cppstd_t getCppStd(const std::string &std);
390-
391-
/** Returns the __STDC_VERSION__ value for a given standard */
392-
SIMPLECPP_LIB std::string getCStdString(const std::string &std);
393-
SIMPLECPP_LIB std::string getCStdString(cstd_t std);
394-
395-
/** Returns the __cplusplus value for a given standard */
396-
SIMPLECPP_LIB std::string getCppStdString(const std::string &std);
397-
SIMPLECPP_LIB std::string getCppStdString(cppstd_t std);
398-
399356
struct SIMPLECPP_LIB FileData {
400357
/** The canonical filename associated with this data */
401358
std::string filename;
@@ -503,8 +460,50 @@ namespace simplecpp {
503460
container_type mData;
504461
name_map_type mNameMap;
505462
id_map_type mIdMap;
506-
507463
};
464+
465+
SIMPLECPP_LIB long long characterLiteralToLL(const std::string& str);
466+
467+
SIMPLECPP_LIB FileDataCache load(const TokenList &rawtokens, std::vector<std::string> &filenames, const DUI &dui, OutputList *outputList = nullptr, FileDataCache cache = {});
468+
469+
/**
470+
* Preprocess
471+
* @todo simplify interface
472+
* @param output TokenList that receives the preprocessing output
473+
* @param rawtokens Raw tokenlist for top sourcefile
474+
* @param files internal data of simplecpp
475+
* @param cache output from simplecpp::load()
476+
* @param dui defines, undefs, and include paths
477+
* @param outputList output: list that will receive output messages
478+
* @param macroUsage output: macro usage
479+
* @param ifCond output: #if/#elif expressions
480+
*/
481+
SIMPLECPP_LIB void preprocess(TokenList &output, const TokenList &rawtokens, std::vector<std::string> &files, FileDataCache &cache, const DUI &dui, OutputList *outputList = nullptr, std::list<MacroUsage> *macroUsage = nullptr, std::list<IfCond> *ifCond = nullptr);
482+
483+
/**
484+
* Deallocate data
485+
*/
486+
SIMPLECPP_LIB void cleanup(FileDataCache &cache);
487+
488+
/** Simplify path */
489+
SIMPLECPP_LIB std::string simplifyPath(std::string path);
490+
491+
/** Convert Cygwin path to Windows path */
492+
SIMPLECPP_LIB std::string convertCygwinToWindowsPath(const std::string &cygwinPath);
493+
494+
/** Returns the C version a given standard */
495+
SIMPLECPP_LIB cstd_t getCStd(const std::string &std);
496+
497+
/** Returns the C++ version a given standard */
498+
SIMPLECPP_LIB cppstd_t getCppStd(const std::string &std);
499+
500+
/** Returns the __STDC_VERSION__ value for a given standard */
501+
SIMPLECPP_LIB std::string getCStdString(const std::string &std);
502+
SIMPLECPP_LIB std::string getCStdString(cstd_t std);
503+
504+
/** Returns the __cplusplus value for a given standard */
505+
SIMPLECPP_LIB std::string getCppStdString(const std::string &std);
506+
SIMPLECPP_LIB std::string getCppStdString(cppstd_t std);
508507
}
509508

510509
#if defined(_MSC_VER)

test.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2087,6 +2087,47 @@ static void systemInclude()
20872087
ASSERT_EQUALS("", toString(outputList));
20882088
}
20892089

2090+
static void circularInclude()
2091+
{
2092+
std::vector<std::string> files;
2093+
simplecpp::FileDataCache cache;
2094+
2095+
{
2096+
const char *path = "test.h";
2097+
const char code[] =
2098+
"#ifndef TEST_H\n"
2099+
"#define TEST_H\n"
2100+
"#include \"a/a.h\"\n"
2101+
"#endif\n"
2102+
;
2103+
cache.insert({path, makeTokenList(code, files, path)});
2104+
}
2105+
2106+
{
2107+
const char *path = "a/a.h";
2108+
const char code[] =
2109+
"#ifndef A_H\n"
2110+
"#define A_H\n"
2111+
"#include \"../test.h\"\n"
2112+
"#endif\n"
2113+
;
2114+
cache.insert({path, makeTokenList(code, files, path)});
2115+
}
2116+
2117+
simplecpp::OutputList outputList;
2118+
{
2119+
std::vector<std::string> filenames;
2120+
simplecpp::DUI dui;
2121+
2122+
const char code[] = "#include \"test.h\"\n";
2123+
const simplecpp::TokenList rawtokens = makeTokenList(code, files, "test.cpp");
2124+
2125+
simplecpp::load(rawtokens, filenames, dui, &outputList, std::move(cache));
2126+
}
2127+
2128+
ASSERT_EQUALS("", toString(outputList));
2129+
}
2130+
20902131
static void multiline1()
20912132
{
20922133
const char code[] = "#define A \\\n"
@@ -3314,6 +3355,7 @@ int main(int argc, char **argv)
33143355
TEST_CASE(missingHeader4);
33153356
TEST_CASE(nestedInclude);
33163357
TEST_CASE(systemInclude);
3358+
TEST_CASE(circularInclude);
33173359

33183360
TEST_CASE(nullDirective1);
33193361
TEST_CASE(nullDirective2);

0 commit comments

Comments
 (0)