From 7b8c77894123c7babf4888fd0877c8cc48446b79 Mon Sep 17 00:00:00 2001 From: firewave Date: Sun, 21 Sep 2025 22:15:05 +0200 Subject: [PATCH 1/3] moved part of `FileDataCache` implementation into source file --- simplecpp.cpp | 101 +++++++++++++++++++++++++++++++++++++++++--------- simplecpp.h | 57 ++++------------------------ 2 files changed, 90 insertions(+), 68 deletions(-) diff --git a/simplecpp.cpp b/simplecpp.cpp index a7ced05a..ee98eec9 100644 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -3,20 +3,6 @@ * Copyright (C) 2016-2023 simplecpp team */ -#if defined(_WIN32) -# ifndef _WIN32_WINNT -# define _WIN32_WINNT 0x0602 -# endif -# ifndef NOMINMAX -# define NOMINMAX -# endif -# ifndef WIN32_LEAN_AND_MEAN -# define WIN32_LEAN_AND_MEAN -# endif -# include -# undef ERROR -#endif - #include "simplecpp.h" #if defined(_WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) @@ -55,6 +41,21 @@ # include #else # include +# include +#endif + +#if defined(_WIN32) +# ifndef _WIN32_WINNT +# define _WIN32_WINNT 0x0602 +# endif +# ifndef NOMINMAX +# define NOMINMAX +# endif +# ifndef WIN32_LEAN_AND_MEAN +# define WIN32_LEAN_AND_MEAN +# endif +# include +# undef ERROR #endif static bool isHex(const std::string &s) @@ -3075,6 +3076,63 @@ static std::string openHeader(std::ifstream &f, const simplecpp::DUI &dui, const return ""; } +struct FileID { +#ifdef _WIN32 + struct { + std::uint64_t VolumeSerialNumber; + struct { + std::uint64_t IdentifierHi; + std::uint64_t IdentifierLo; + } FileId; + } fileIdInfo; + + bool operator==(const FileID &that) const noexcept { + return fileIdInfo.VolumeSerialNumber == that.fileIdInfo.VolumeSerialNumber && + fileIdInfo.FileId.IdentifierHi == that.fileIdInfo.FileId.IdentifierHi && + fileIdInfo.FileId.IdentifierLo == that.fileIdInfo.FileId.IdentifierLo; + } +#else + dev_t dev; + ino_t ino; + + bool operator==(const FileID& that) const noexcept { + return dev == that.dev && ino == that.ino; + } +#endif + struct Hasher { + std::size_t operator()(const FileID &id) const { +#ifdef _WIN32 + return static_cast(id.fileIdInfo.FileId.IdentifierHi ^ id.fileIdInfo.FileId.IdentifierLo ^ + id.fileIdInfo.VolumeSerialNumber); +#else + return static_cast(id.dev) ^ static_cast(id.ino); +#endif + } + }; +}; + +struct simplecpp::FileDataCache::Impl +{ + void clear() + { + mIdMap.clear(); + } + + using id_map_type = std::unordered_map; + + id_map_type mIdMap; +}; + +simplecpp::FileDataCache::FileDataCache() + : mImpl(new Impl) +{} + +simplecpp::FileDataCache::~FileDataCache() = default; +simplecpp::FileDataCache::FileDataCache(FileDataCache &&) noexcept = default; +simplecpp::FileDataCache &simplecpp::FileDataCache::operator=(simplecpp::FileDataCache &&) noexcept = default; + +static bool getFileId(const std::string &path, FileID &id); + std::pair simplecpp::FileDataCache::tryload(FileDataCache::name_map_type::iterator &name_it, const simplecpp::DUI &dui, std::vector &filenames, simplecpp::OutputList *outputList) { const std::string &path = name_it->first; @@ -3083,8 +3141,8 @@ std::pair simplecpp::FileDataCache::tryload(FileDat if (!getFileId(path, fileId)) return {nullptr, false}; - const auto id_it = mIdMap.find(fileId); - if (id_it != mIdMap.end()) { + const auto id_it = mImpl->mIdMap.find(fileId); + if (id_it != mImpl->mIdMap.end()) { name_it->second = id_it->second; return {id_it->second, false}; } @@ -3095,7 +3153,7 @@ std::pair simplecpp::FileDataCache::tryload(FileDat data->tokens.removeComments(); name_it->second = data; - mIdMap.emplace(fileId, data); + mImpl->mIdMap.emplace(fileId, data); mData.emplace_back(data); return {data, true}; @@ -3147,7 +3205,14 @@ std::pair simplecpp::FileDataCache::get(const std:: return {nullptr, false}; } -bool simplecpp::FileDataCache::getFileId(const std::string &path, FileID &id) +void simplecpp::FileDataCache::clear() +{ + mImpl->clear(); + mNameMap.clear(); + mData.clear(); +} + +bool getFileId(const std::string &path, FileID &id) { #ifdef _WIN32 HANDLE hFile = CreateFileA(path.c_str(), 0, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr); diff --git a/simplecpp.h b/simplecpp.h index f29166ff..de20ae2e 100644 --- a/simplecpp.h +++ b/simplecpp.h @@ -41,10 +41,6 @@ # define SIMPLECPP_LIB #endif -#ifndef _WIN32 -# include -#endif - #if defined(_MSC_VER) # pragma warning(push) // suppress warnings about "conversion from 'type1' to 'type2', possible loss of data" @@ -452,13 +448,14 @@ namespace simplecpp { class SIMPLECPP_LIB FileDataCache { public: - FileDataCache() = default; + FileDataCache(); + ~FileDataCache(); FileDataCache(const FileDataCache &) = delete; - FileDataCache(FileDataCache &&) = default; + FileDataCache(FileDataCache &&) noexcept; FileDataCache &operator=(const FileDataCache &) = delete; - FileDataCache &operator=(FileDataCache &&) = default; + FileDataCache &operator=(FileDataCache &&) noexcept; /** Get the cached data for a file, or load and then return it if it isn't cached. * returns the file data and true if the file was loaded, false if it was cached. */ @@ -472,11 +469,7 @@ namespace simplecpp { mNameMap.emplace(newdata->filename, newdata); } - void clear() { - mNameMap.clear(); - mIdMap.clear(); - mData.clear(); - } + void clear(); using container_type = std::vector>; using iterator = container_type::iterator; @@ -506,51 +499,15 @@ namespace simplecpp { } private: - struct FileID { -#ifdef _WIN32 - struct { - std::uint64_t VolumeSerialNumber; - struct { - std::uint64_t IdentifierHi; - std::uint64_t IdentifierLo; - } FileId; - } fileIdInfo; - - bool operator==(const FileID &that) const noexcept { - return fileIdInfo.VolumeSerialNumber == that.fileIdInfo.VolumeSerialNumber && - fileIdInfo.FileId.IdentifierHi == that.fileIdInfo.FileId.IdentifierHi && - fileIdInfo.FileId.IdentifierLo == that.fileIdInfo.FileId.IdentifierLo; - } -#else - dev_t dev; - ino_t ino; - - bool operator==(const FileID& that) const noexcept { - return dev == that.dev && ino == that.ino; - } -#endif - struct Hasher { - std::size_t operator()(const FileID &id) const { -#ifdef _WIN32 - return static_cast(id.fileIdInfo.FileId.IdentifierHi ^ id.fileIdInfo.FileId.IdentifierLo ^ - id.fileIdInfo.VolumeSerialNumber); -#else - return static_cast(id.dev) ^ static_cast(id.ino); -#endif - } - }; - }; + struct Impl; + std::unique_ptr mImpl; using name_map_type = std::unordered_map; - using id_map_type = std::unordered_map; - - static bool getFileId(const std::string &path, FileID &id); std::pair tryload(name_map_type::iterator &name_it, const DUI &dui, std::vector &filenames, OutputList *outputList); container_type mData; name_map_type mNameMap; - id_map_type mIdMap; }; /** Converts character literal (including prefix, but not ud-suffix) to long long value. From 34ae6cb97db37719080594944c249c5a0935b2aa Mon Sep 17 00:00:00 2001 From: firewave Date: Tue, 28 Apr 2026 07:54:05 +0200 Subject: [PATCH 2/3] simplecpp.cpp: fixed `misc-use-internal-linkage` clang-tidy warning --- simplecpp.cpp | 52 ++++++++++++++++++++++++++------------------------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/simplecpp.cpp b/simplecpp.cpp index ee98eec9..3927ed0e 100644 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -3076,40 +3076,42 @@ static std::string openHeader(std::ifstream &f, const simplecpp::DUI &dui, const return ""; } -struct FileID { +namespace { + struct FileID { #ifdef _WIN32 - struct { - std::uint64_t VolumeSerialNumber; struct { - std::uint64_t IdentifierHi; - std::uint64_t IdentifierLo; - } FileId; - } fileIdInfo; - - bool operator==(const FileID &that) const noexcept { - return fileIdInfo.VolumeSerialNumber == that.fileIdInfo.VolumeSerialNumber && - fileIdInfo.FileId.IdentifierHi == that.fileIdInfo.FileId.IdentifierHi && - fileIdInfo.FileId.IdentifierLo == that.fileIdInfo.FileId.IdentifierLo; - } + std::uint64_t VolumeSerialNumber; + struct { + std::uint64_t IdentifierHi; + std::uint64_t IdentifierLo; + } FileId; + } fileIdInfo; + + bool operator==(const FileID &that) const noexcept { + return fileIdInfo.VolumeSerialNumber == that.fileIdInfo.VolumeSerialNumber && + fileIdInfo.FileId.IdentifierHi == that.fileIdInfo.FileId.IdentifierHi && + fileIdInfo.FileId.IdentifierLo == that.fileIdInfo.FileId.IdentifierLo; + } #else - dev_t dev; - ino_t ino; + dev_t dev; + ino_t ino; - bool operator==(const FileID& that) const noexcept { - return dev == that.dev && ino == that.ino; - } + bool operator==(const FileID& that) const noexcept { + return dev == that.dev && ino == that.ino; + } #endif - struct Hasher { - std::size_t operator()(const FileID &id) const { + struct Hasher { + std::size_t operator()(const FileID &id) const { #ifdef _WIN32 - return static_cast(id.fileIdInfo.FileId.IdentifierHi ^ id.fileIdInfo.FileId.IdentifierLo ^ - id.fileIdInfo.VolumeSerialNumber); + return static_cast(id.fileIdInfo.FileId.IdentifierHi ^ id.fileIdInfo.FileId.IdentifierLo ^ + id.fileIdInfo.VolumeSerialNumber); #else - return static_cast(id.dev) ^ static_cast(id.ino); + return static_cast(id.dev) ^ static_cast(id.ino); #endif - } + } + }; }; -}; +} struct simplecpp::FileDataCache::Impl { From 1596948a15fbeed527cbc61fe94ebe08aa1eef01 Mon Sep 17 00:00:00 2001 From: firewave Date: Tue, 28 Apr 2026 08:03:58 +0200 Subject: [PATCH 3/3] s --- simplecpp.cpp | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/simplecpp.cpp b/simplecpp.cpp index 3927ed0e..d3511792 100644 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -37,13 +37,6 @@ #include #include -#ifdef _WIN32 -# include -#else -# include -# include -#endif - #if defined(_WIN32) # ifndef _WIN32_WINNT # define _WIN32_WINNT 0x0602 @@ -56,6 +49,10 @@ # endif # include # undef ERROR +# include +#else +# include +# include #endif static bool isHex(const std::string &s)