Skip to content

Fix #449 (Update c++ standard to c++11) #450

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/clang-tidy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
- name: Prepare CMake
run: |
cmake -S . -B cmake.output -G "Unix Makefiles" -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DDISABLE_CPP03_SYNTAX_CHECK=ON
cmake -S . -B cmake.output -G "Unix Makefiles" -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
env:
CXX: clang-20

Expand Down
21 changes: 2 additions & 19 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
cmake_minimum_required (VERSION 3.10)
project (simplecpp LANGUAGES CXX)

option(DISABLE_CPP03_SYNTAX_CHECK "Disable the C++03 syntax check." OFF)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

include(CheckCXXCompilerFlag)

Expand Down Expand Up @@ -70,25 +71,7 @@ endif()
add_library(simplecpp_obj OBJECT simplecpp.cpp)

add_executable(simplecpp $<TARGET_OBJECTS:simplecpp_obj> main.cpp)
set_property(TARGET simplecpp PROPERTY CXX_STANDARD 11)

if (NOT DISABLE_CPP03_SYNTAX_CHECK)
# it is not possible to set a standard older than C++14 with Visual Studio
if (CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
# we need to create a dummy library as -fsyntax-only will not produce any output files causing the build to fail
add_library(simplecpp-03-syntax OBJECT simplecpp.cpp)
target_compile_options(simplecpp-03-syntax PRIVATE -std=c++03)
if (CMAKE_CXX_COMPILER_ID MATCHES "GNU")
target_compile_options(simplecpp-03-syntax PRIVATE -Wno-long-long)
elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
target_compile_options(simplecpp-03-syntax PRIVATE -Wno-c++11-long-long -Wno-c++11-compat)
endif()
add_dependencies(simplecpp simplecpp-03-syntax)
endif()
endif()

add_executable(testrunner $<TARGET_OBJECTS:simplecpp_obj> test.cpp)
set_property(TARGET testrunner PROPERTY CXX_STANDARD 11)

enable_testing()
add_test(NAME testrunner COMMAND testrunner)
4 changes: 1 addition & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
all: testrunner simplecpp

CXXFLAGS = -Wall -Wextra -pedantic -Wcast-qual -Wfloat-equal -Wmissing-declarations -Wmissing-format-attribute -Wredundant-decls -Wundef -Wno-multichar -Wold-style-cast -std=c++0x -g
CXXFLAGS = -Wall -Wextra -pedantic -Wcast-qual -Wfloat-equal -Wmissing-declarations -Wmissing-format-attribute -Wredundant-decls -Wundef -Wno-multichar -Wold-style-cast -std=c++11 -g
LDFLAGS = -g

%.o: %.cpp simplecpp.h
Expand All @@ -11,8 +11,6 @@ testrunner: test.o simplecpp.o
$(CXX) $(LDFLAGS) simplecpp.o test.o -o testrunner

test: testrunner simplecpp
# The -std=c++03 makes sure that simplecpp.cpp is C++03 conformant. We don't require a C++11 compiler
g++ -std=c++03 -fsyntax-only simplecpp.cpp
./testrunner
python3 run-tests.py

Expand Down
99 changes: 15 additions & 84 deletions simplecpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,10 @@
#include <stack>
#include <stdexcept>
#include <string>
#if __cplusplus >= 201103L
#ifdef SIMPLECPP_WINDOWS
#include <mutex>
#endif
#include <unordered_map>
#endif
#include <utility>
#include <vector>

Expand All @@ -51,18 +49,6 @@
#undef ERROR
#endif

#if __cplusplus >= 201103L
#define OVERRIDE override
#define EXPLICIT explicit
#else
#define OVERRIDE
#define EXPLICIT
#endif

#if (__cplusplus < 201103L) && !defined(__APPLE__)
#define nullptr NULL
#endif

static bool isHex(const std::string &s)
{
return s.size()>2 && (s.compare(0,2,"0x")==0 || s.compare(0,2,"0X")==0);
Expand Down Expand Up @@ -368,22 +354,22 @@ class simplecpp::TokenList::Stream {
class StdIStream : public simplecpp::TokenList::Stream {
public:
// cppcheck-suppress uninitDerivedMemberVar - we call Stream::init() to initialize the private members
EXPLICIT StdIStream(std::istream &istr)
explicit StdIStream(std::istream &istr)
: istr(istr) {
assert(istr.good());
init();
}

virtual int get() OVERRIDE {
virtual int get() override {
return istr.get();
}
virtual int peek() OVERRIDE {
virtual int peek() override {
return istr.peek();
}
virtual void unget() OVERRIDE {
virtual void unget() override {
istr.unget();
}
virtual bool good() OVERRIDE {
virtual bool good() override {
return istr.good();
}

Expand All @@ -402,20 +388,20 @@ class StdCharBufStream : public simplecpp::TokenList::Stream {
init();
}

virtual int get() OVERRIDE {
virtual int get() override {
if (pos >= size)
return lastStatus = EOF;
return str[pos++];
}
virtual int peek() OVERRIDE {
virtual int peek() override {
if (pos >= size)
return lastStatus = EOF;
return str[pos];
}
virtual void unget() OVERRIDE {
virtual void unget() override {
--pos;
}
virtual bool good() OVERRIDE {
virtual bool good() override {
return lastStatus != EOF;
}

Expand All @@ -429,7 +415,7 @@ class StdCharBufStream : public simplecpp::TokenList::Stream {
class FileStream : public simplecpp::TokenList::Stream {
public:
// cppcheck-suppress uninitDerivedMemberVar - we call Stream::init() to initialize the private members
EXPLICIT FileStream(const std::string &filename, std::vector<std::string> &files)
explicit FileStream(const std::string &filename, std::vector<std::string> &files)
: file(fopen(filename.c_str(), "rb"))
, lastCh(0)
, lastStatus(0) {
Expand All @@ -440,25 +426,25 @@ class FileStream : public simplecpp::TokenList::Stream {
init();
}

~FileStream() OVERRIDE {
~FileStream() override {
fclose(file);
file = nullptr;
}

virtual int get() OVERRIDE {
virtual int get() override {
lastStatus = lastCh = fgetc(file);
return lastCh;
}
virtual int peek() OVERRIDE{
virtual int peek() override{
// keep lastCh intact
const int ch = fgetc(file);
unget_internal(ch);
return ch;
}
virtual void unget() OVERRIDE {
virtual void unget() override {
unget_internal(lastCh);
}
virtual bool good() OVERRIDE {
virtual bool good() override {
return lastStatus != EOF;
}

Expand Down Expand Up @@ -519,12 +505,10 @@ simplecpp::TokenList::TokenList(const TokenList &other) : frontToken(nullptr), b
*this = other;
}

#if __cplusplus >= 201103L
simplecpp::TokenList::TokenList(TokenList &&other) : frontToken(nullptr), backToken(nullptr), files(other.files)
{
*this = std::move(other);
}
#endif

simplecpp::TokenList::~TokenList()
{
Expand All @@ -543,7 +527,6 @@ simplecpp::TokenList &simplecpp::TokenList::operator=(const TokenList &other)
return *this;
}

#if __cplusplus >= 201103L
simplecpp::TokenList &simplecpp::TokenList::operator=(TokenList &&other)
{
if (this != &other) {
Expand All @@ -557,7 +540,6 @@ simplecpp::TokenList &simplecpp::TokenList::operator=(TokenList &&other)
}
return *this;
}
#endif

void simplecpp::TokenList::clear()
{
Expand Down Expand Up @@ -1477,11 +1459,7 @@ unsigned int simplecpp::TokenList::fileIndex(const std::string &filename)

namespace simplecpp {
class Macro;
#if __cplusplus >= 201103L
using MacroMap = std::unordered_map<TokenString,Macro>;
#else
typedef std::map<TokenString,Macro> MacroMap;
#endif

class Macro {
public:
Expand Down Expand Up @@ -1791,13 +1769,8 @@ namespace simplecpp {
tok = tok->next;
}
}
#if __cplusplus >= 201103L
optExpandValue = new TokenList(std::move(expandValue));
optNoExpandValue = new TokenList(std::move(noExpandValue));
#else
optExpandValue = new TokenList(expandValue);
optNoExpandValue = new TokenList(noExpandValue);
#endif
}

return true;
Expand Down Expand Up @@ -2437,47 +2410,9 @@ namespace simplecpp {

#ifdef SIMPLECPP_WINDOWS

#if __cplusplus >= 201103L
using MyMutex = std::mutex;
template<class T>
using MyLock = std::lock_guard<T>;
#else
class MyMutex {
public:
MyMutex() {
InitializeCriticalSection(&m_criticalSection);
}

~MyMutex() {
DeleteCriticalSection(&m_criticalSection);
}

CRITICAL_SECTION* lock() {
return &m_criticalSection;
}
private:
CRITICAL_SECTION m_criticalSection;
};

template<typename T>
class MyLock {
public:
explicit MyLock(T& m)
: m_mutex(m) {
EnterCriticalSection(m_mutex.lock());
}

~MyLock() {
LeaveCriticalSection(m_mutex.lock());
}

private:
MyLock& operator=(const MyLock&);
MyLock(const MyLock&);

T& m_mutex;
};
#endif

class RealFileNameMap {
public:
Expand Down Expand Up @@ -4099,7 +4034,3 @@ std::string simplecpp::getCppStdString(const std::string &std)
{
return getCppStdString(getCppStd(std));
}

#if (__cplusplus < 201103L) && !defined(__APPLE__)
#undef nullptr
#endif
12 changes: 0 additions & 12 deletions simplecpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@
# define SIMPLECPP_LIB
#endif

#if (__cplusplus < 201103L) && !defined(__APPLE__)
#define nullptr NULL
#endif

#if defined(_MSC_VER)
# pragma warning(push)
// suppress warnings about "conversion from 'type1' to 'type2', possible loss of data"
Expand Down Expand Up @@ -214,14 +210,10 @@ namespace simplecpp {
/** generates a token list from the given filename parameter */
TokenList(const std::string &filename, std::vector<std::string> &filenames, OutputList *outputList = nullptr);
TokenList(const TokenList &other);
#if __cplusplus >= 201103L
TokenList(TokenList &&other);
#endif
~TokenList();
TokenList &operator=(const TokenList &other);
#if __cplusplus >= 201103L
TokenList &operator=(TokenList &&other);
#endif

void clear();
bool empty() const {
Expand Down Expand Up @@ -395,8 +387,4 @@ namespace simplecpp {
# pragma warning(pop)
#endif

#if (__cplusplus < 201103L) && !defined(__APPLE__)
#undef nullptr
#endif

#endif
Loading