Skip to content

Commit

Permalink
TokenList: avoid Path::identify() being called with empty filename …
Browse files Browse the repository at this point in the history
…from `determineCppC()`
  • Loading branch information
firewave committed Apr 22, 2024
1 parent ea2e716 commit 550e285
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
19 changes: 11 additions & 8 deletions externals/simplecpp/simplecpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3268,6 +3268,9 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
sizeOfType.insert(std::make_pair("double *", sizeof(double *)));
sizeOfType.insert(std::make_pair("long double *", sizeof(long double *)));

// use a dummy vector for the macros because as this is not part of the file and would add an empty entry - e.g. /usr/include/poll.h
std::vector<std::string> dummy;

const bool hasInclude = (dui.std.size() == 5 && dui.std.compare(0,3,"c++") == 0 && dui.std >= "c++17");
MacroMap macros;
for (std::list<std::string>::const_iterator it = dui.defines.begin(); it != dui.defines.end(); ++it) {
Expand All @@ -3279,26 +3282,26 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
continue;
const std::string lhs(macrostr.substr(0,eq));
const std::string rhs(eq==std::string::npos ? std::string("1") : macrostr.substr(eq+1));
const Macro macro(lhs, rhs, files);
const Macro macro(lhs, rhs, dummy);
macros.insert(std::pair<TokenString,Macro>(macro.name(), macro));
}

macros.insert(std::make_pair("__FILE__", Macro("__FILE__", "__FILE__", files)));
macros.insert(std::make_pair("__LINE__", Macro("__LINE__", "__LINE__", files)));
macros.insert(std::make_pair("__COUNTER__", Macro("__COUNTER__", "__COUNTER__", files)));
macros.insert(std::make_pair("__FILE__", Macro("__FILE__", "__FILE__", dummy)));
macros.insert(std::make_pair("__LINE__", Macro("__LINE__", "__LINE__", dummy)));
macros.insert(std::make_pair("__COUNTER__", Macro("__COUNTER__", "__COUNTER__", dummy)));
struct tm ltime = {};
getLocaltime(ltime);
macros.insert(std::make_pair("__DATE__", Macro("__DATE__", getDateDefine(&ltime), files)));
macros.insert(std::make_pair("__TIME__", Macro("__TIME__", getTimeDefine(&ltime), files)));
macros.insert(std::make_pair("__DATE__", Macro("__DATE__", getDateDefine(&ltime), dummy)));
macros.insert(std::make_pair("__TIME__", Macro("__TIME__", getTimeDefine(&ltime), dummy)));

if (!dui.std.empty()) {
std::string std_def = simplecpp::getCStdString(dui.std);
if (!std_def.empty()) {
macros.insert(std::make_pair("__STDC_VERSION__", Macro("__STDC_VERSION__", std_def, files)));
macros.insert(std::make_pair("__STDC_VERSION__", Macro("__STDC_VERSION__", std_def, dummy)));
} else {
std_def = simplecpp::getCppStdString(dui.std);
if (!std_def.empty())
macros.insert(std::make_pair("__cplusplus", Macro("__cplusplus", std_def, files)));
macros.insert(std::make_pair("__cplusplus", Macro("__cplusplus", std_def, dummy)));
}
}

Expand Down
4 changes: 4 additions & 0 deletions externals/simplecpp/simplecpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,10 @@ namespace simplecpp {
/** sizeof(T) */
std::map<std::string, std::size_t> sizeOfType;

const std::vector<std::string>& getFiles() const {
return files;
}

private:
void combineOperators();

Expand Down
7 changes: 4 additions & 3 deletions lib/tokenlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ void TokenList::determineCppC()
{
// only try to determine if it wasn't enforced
if (mLang == Standards::Language::None) {
ASSERT_LANG(!getSourceFilePath().empty());
mLang = Path::identify(getSourceFilePath());
// TODO: cannot enable assert as this might occur for unknown extensions
//ASSERT_LANG(mLang != Standards::Language::None);
Expand Down Expand Up @@ -372,11 +373,11 @@ bool TokenList::createTokensInternal(std::istream &code, const std::string& file
// NOLINTNEXTLINE(cppcoreguidelines-rvalue-reference-param-not-moved)
void TokenList::createTokens(simplecpp::TokenList&& tokenList)
{
if (tokenList.cfront()) {
// tokenList.cfront() might be NULL if the file contained nothing to tokenize so we need to check the files instead
if (!tokenList.getFiles().empty()) {
// this is a copy
// TODO: the same as TokenList.files - move that instead
// TODO: this points to mFiles when called from createTokens(std::istream &, const std::string&)
mOrigFiles = mFiles = tokenList.cfront()->location.files;
mOrigFiles = mFiles = tokenList.getFiles();
}
else
mFiles.clear();
Expand Down

0 comments on commit 550e285

Please sign in to comment.