Skip to content
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
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ cmake_minimum_required (VERSION 3.5)
project (simplecpp LANGUAGES CXX)

if (CMAKE_CXX_COMPILER_ID MATCHES "GNU")
add_compile_options(-Wall -Wextra -pedantic -Wcast-qual -Wfloat-equal -Wmissing-declarations -Wmissing-format-attribute -Wredundant-decls -Wshadow -Wundef)
add_compile_options(-Wall -Wextra -pedantic -Wcast-qual -Wfloat-equal -Wmissing-declarations -Wmissing-format-attribute -Wredundant-decls -Wshadow -Wundef -Wold-style-cast)
endif()

if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
Expand All @@ -12,7 +12,7 @@ if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
# these are not really fixable
add_compile_options(-Wno-exit-time-destructors -Wno-global-constructors)
# TODO: fix these?
add_compile_options(-Wno-padded -Wno-sign-conversion -Wno-conversion -Wno-old-style-cast)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't want to get conversion warnings. I am not sure what the Wpadded is about.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will address that in another PR.

The one about padding is related the the order of member in a data types causing additional padding and thus increased memory usage. In general it's also something we might not be interested in.

add_compile_options(-Wno-padded -Wno-sign-conversion -Wno-conversion)
endif()

add_executable(simplecpp simplecpp.cpp main.cpp)
Expand Down
2 changes: 1 addition & 1 deletion 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 -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++0x -g
LDFLAGS = -g

%.o: %.cpp simplecpp.h
Expand Down
20 changes: 10 additions & 10 deletions simplecpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,20 +320,20 @@ std::string simplecpp::TokenList::stringify() const

static unsigned char readChar(std::istream &istr, unsigned int bom)
{
unsigned char ch = (unsigned char)istr.get();
unsigned char ch = static_cast<unsigned char>(istr.get());

// For UTF-16 encoded files the BOM is 0xfeff/0xfffe. If the
// character is non-ASCII character then replace it with 0xff
if (bom == 0xfeff || bom == 0xfffe) {
const unsigned char ch2 = (unsigned char)istr.get();
const unsigned char ch2 = static_cast<unsigned char>(istr.get());
const int ch16 = (bom == 0xfeff) ? (ch<<8 | ch2) : (ch2<<8 | ch);
ch = (unsigned char)((ch16 >= 0x80) ? 0xff : ch16);
ch = static_cast<unsigned char>(((ch16 >= 0x80) ? 0xff : ch16));
}

// Handling of newlines..
if (ch == '\r') {
ch = '\n';
if (bom == 0 && (char)istr.peek() == '\n')
if (bom == 0 && static_cast<char>(istr.peek()) == '\n')
(void)istr.get();
else if (bom == 0xfeff || bom == 0xfffe) {
int c1 = istr.get();
Expand All @@ -351,16 +351,16 @@ static unsigned char readChar(std::istream &istr, unsigned int bom)

static unsigned char peekChar(std::istream &istr, unsigned int bom)
{
unsigned char ch = (unsigned char)istr.peek();
unsigned char ch = static_cast<unsigned char>(istr.peek());

// For UTF-16 encoded files the BOM is 0xfeff/0xfffe. If the
// character is non-ASCII character then replace it with 0xff
if (bom == 0xfeff || bom == 0xfffe) {
(void)istr.get();
const unsigned char ch2 = (unsigned char)istr.peek();
const unsigned char ch2 = static_cast<unsigned char>(istr.peek());
istr.unget();
const int ch16 = (bom == 0xfeff) ? (ch<<8 | ch2) : (ch2<<8 | ch);
ch = (unsigned char)((ch16 >= 0x80) ? 0xff : ch16);
ch = static_cast<unsigned char>(((ch16 >= 0x80) ? 0xff : ch16));
}

// Handling of newlines..
Expand All @@ -383,9 +383,9 @@ static unsigned short getAndSkipBOM(std::istream &istr)

// The UTF-16 BOM is 0xfffe or 0xfeff.
if (ch1 >= 0xfe) {
unsigned short bom = ((unsigned char)istr.get() << 8);
unsigned short bom = (static_cast<unsigned char>(istr.get()) << 8);
if (istr.peek() >= 0xfe)
return bom | (unsigned char)istr.get();
return bom | static_cast<unsigned char>(istr.get());
istr.unget();
return 0;
}
Expand Down Expand Up @@ -486,7 +486,7 @@ void simplecpp::TokenList::readfile(std::istream &istr, const std::string &filen
err.type = simplecpp::Output::UNHANDLED_CHAR_ERROR;
err.location = location;
std::ostringstream s;
s << (int)ch;
s << static_cast<int>(ch);
err.msg = "The code contains unhandled character(s) (character code=" + s.str() + "). Neither unicode nor extended ascii is supported.";
outputList->push_back(err);
}
Expand Down
4 changes: 2 additions & 2 deletions simplecpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ namespace simplecpp {
}

void flags() {
name = (std::isalpha((unsigned char)string[0]) || string[0] == '_' || string[0] == '$')
name = (std::isalpha(static_cast<unsigned char>(string[0])) || string[0] == '_' || string[0] == '$')
&& (std::memchr(string.c_str(), '\'', string.size()) == nullptr);
comment = string.size() > 1U && string[0] == '/' && (string[1] == '/' || string[1] == '*');
number = std::isdigit((unsigned char)string[0]) || (string.size() > 1U && string[0] == '-' && std::isdigit((unsigned char)string[1]));
number = std::isdigit(static_cast<unsigned char>(string[0])) || (string.size() > 1U && string[0] == '-' && std::isdigit(static_cast<unsigned char>(string[1])));
op = (string.size() == 1U) ? string[0] : '\0';
}

Expand Down
16 changes: 8 additions & 8 deletions test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ static int numberOfFailedAssertions = 0;
static std::string pprint(const std::string &in)
{
std::string ret;
for (int i = 0; i < (int)in.size(); ++i) {
for (std::string::size_type i = 0; i < in.size(); ++i) {
if (in[i] == '\n')
ret += "\\n";
ret += in[i];
Expand Down Expand Up @@ -223,10 +223,10 @@ static void characterLiteral()
ASSERT_EQUALS('\u0012', simplecpp::characterLiteralToLL("'\\u0012'"));
ASSERT_EQUALS('\U00000012', simplecpp::characterLiteralToLL("'\\U00000012'"));

ASSERT_EQUALS(((unsigned int)(unsigned char)'b' << 8) | (unsigned char)'c', simplecpp::characterLiteralToLL("'bc'"));
ASSERT_EQUALS(((unsigned int)(unsigned char)'\x23' << 8) | (unsigned char)'\x45', simplecpp::characterLiteralToLL("'\\x23\\x45'"));
ASSERT_EQUALS(((unsigned int)(unsigned char)'\11' << 8) | (unsigned char)'\222', simplecpp::characterLiteralToLL("'\\11\\222'"));
ASSERT_EQUALS(((unsigned int)(unsigned char)'\a' << 8) | (unsigned char)'\b', simplecpp::characterLiteralToLL("'\\a\\b'"));
ASSERT_EQUALS((static_cast<unsigned int>(static_cast<unsigned char>('b')) << 8) | static_cast<unsigned char>('c'), simplecpp::characterLiteralToLL("'bc'"));
ASSERT_EQUALS((static_cast<unsigned int>(static_cast<unsigned char>('\x23')) << 8) | static_cast<unsigned char>('\x45'), simplecpp::characterLiteralToLL("'\\x23\\x45'"));
ASSERT_EQUALS((static_cast<unsigned int>(static_cast<unsigned char>('\11')) << 8) | static_cast<unsigned char>('\222'), simplecpp::characterLiteralToLL("'\\11\\222'"));
ASSERT_EQUALS((static_cast<unsigned int>(static_cast<unsigned char>('\a')) << 8) | static_cast<unsigned char>('\b'), simplecpp::characterLiteralToLL("'\\a\\b'"));
if (sizeof(int) <= 4)
ASSERT_EQUALS(-1, simplecpp::characterLiteralToLL("'\\xff\\xff\\xff\\xff'"));
else
Expand All @@ -253,9 +253,9 @@ static void characterLiteral()

#ifdef __GNUC__
// BEGIN Implementation-specific results
ASSERT_EQUALS((int)('AB'), simplecpp::characterLiteralToLL("'AB'"));
ASSERT_EQUALS((int)('ABC'), simplecpp::characterLiteralToLL("'ABC'"));
ASSERT_EQUALS((int)('ABCD'), simplecpp::characterLiteralToLL("'ABCD'"));
ASSERT_EQUALS(static_cast<int>('AB'), simplecpp::characterLiteralToLL("'AB'"));
ASSERT_EQUALS(static_cast<int>('ABC'), simplecpp::characterLiteralToLL("'ABC'"));
ASSERT_EQUALS(static_cast<int>('ABCD'), simplecpp::characterLiteralToLL("'ABCD'"));
ASSERT_EQUALS('\134t', simplecpp::characterLiteralToLL("'\\134t'")); // cppcheck ticket #7452
// END Implementation-specific results
#endif
Expand Down