Skip to content

Commit

Permalink
Resolve clang-tidy bugprone warnings
Browse files Browse the repository at this point in the history
Most of these issues are pretty innocuous, but I did find a couple
places that constructors/destructors were relying on dynamic dispatch.
  • Loading branch information
thetic committed Nov 19, 2022
1 parent 65846fb commit 44ca541
Show file tree
Hide file tree
Showing 16 changed files with 44 additions and 38 deletions.
4 changes: 2 additions & 2 deletions examples/AllTests/MockDocumentationTest.cpp
Expand Up @@ -125,8 +125,8 @@ TEST(MockDocumentation, ObjectParameters)
TEST(MockDocumentation, returnValue)
{
mock().expectOneCall("function").andReturnValue(10);
int value = mock().actualCall("function").returnValue().getIntValue();
value = mock().returnValue().getIntValue();
mock().actualCall("function").returnValue().getIntValue();
int value = mock().returnValue().getIntValue();
LONGS_EQUAL(10, value);
}

Expand Down
2 changes: 1 addition & 1 deletion include/CppUTest/UtestMacros.h
Expand Up @@ -312,7 +312,7 @@
BITS_LOCATION(expected, actual, mask, text, __FILE__, __LINE__)

#define BITS_LOCATION(expected, actual, mask, text, file, line)\
do { UtestShell::getCurrent()->assertBitsEqual(expected, actual, mask, sizeof(actual), text, file, line); } while(0)
do { UtestShell::getCurrent()->assertBitsEqual(expected, actual, mask, sizeof(actual), text, file, line); } while(0) // NOLINT

#define ENUMS_EQUAL_INT(expected, actual)\
ENUMS_EQUAL_TYPE(int, expected, actual)
Expand Down
2 changes: 1 addition & 1 deletion include/CppUTestExt/MockCheckedActualCall.h
Expand Up @@ -150,7 +150,7 @@ class MockCheckedActualCall : public MockActualCall
MockOutputParametersListNode* outputParameterExpectations_;

virtual void addOutputParameter(const SimpleString& name, const SimpleString& type, void* ptr);
virtual void cleanUpOutputParameterList();
void cleanUpOutputParameterList();
};

class MockActualCallTrace : public MockActualCall
Expand Down
3 changes: 1 addition & 2 deletions src/CppUTest/CommandLineArguments.cpp
Expand Up @@ -71,8 +71,7 @@ bool CommandLineArguments::parse(TestPlugin* plugin)
else if (argument == "-ll") listTestLocations_ = true;
else if (argument == "-ri") runIgnored_ = true;
else if (argument == "-f") crashOnFail_ = true;
else if (argument == "-e") rethrowExceptions_ = false;
else if (argument == "-ci") rethrowExceptions_ = false;
else if ((argument == "-e") || (argument == "-ci")) rethrowExceptions_ = false;
else if (argument.startsWith("-r")) setRepeatCount(ac_, av_, i);
else if (argument.startsWith("-g")) addGroupFilter(ac_, av_, i);
else if (argument.startsWith("-t")) correctParameters = addGroupDotNameFilter(ac_, av_, i, "-t", false, false);
Expand Down
2 changes: 1 addition & 1 deletion src/CppUTest/MemoryLeakWarningPlugin.cpp
Expand Up @@ -131,7 +131,7 @@ void cpputest_free_location_with_leak_detection(void* buffer, const char* file,
#undef new

#if CPPUTEST_HAVE_EXCEPTIONS
#define UT_THROW_BAD_ALLOC_WHEN_NULL(memory) if (memory == NULLPTR) throw CPPUTEST_BAD_ALLOC()
#define UT_THROW_BAD_ALLOC_WHEN_NULL(memory) if ((memory) == NULLPTR) throw CPPUTEST_BAD_ALLOC()
#else
#define UT_THROW_BAD_ALLOC_WHEN_NULL(memory)
#endif
Expand Down
26 changes: 12 additions & 14 deletions src/CppUTest/SimpleString.cpp
Expand Up @@ -62,7 +62,7 @@ GlobalSimpleStringMemoryAccountant::~GlobalSimpleStringMemoryAccountant()

void GlobalSimpleStringMemoryAccountant::restoreAllocator()
{
if (SimpleString::getStringAllocator() == allocator_)
if (allocator_ && (SimpleString::getStringAllocator() == allocator_))
SimpleString::setStringAllocator(allocator_->originalAllocator());
}

Expand Down Expand Up @@ -1031,19 +1031,17 @@ SimpleString StringFromMaskedBits(unsigned long value, unsigned long mask, size_

SimpleString StringFromOrdinalNumber(unsigned int number)
{
unsigned int onesDigit = number % 10;

const char* suffix;
if (number >= 11 && number <= 13) {
suffix = "th";
} else if (3 == onesDigit) {
suffix = "rd";
} else if (2 == onesDigit) {
suffix = "nd";
} else if (1 == onesDigit) {
suffix = "st";
} else {
suffix = "th";
const char* suffix = "th";

if ((number < 11) || (number > 13)) {
unsigned int const onesDigit = number % 10;
if (3 == onesDigit) {
suffix = "rd";
} else if (2 == onesDigit) {
suffix = "nd";
} else if (1 == onesDigit) {
suffix = "st";
}
}

return StringFromFormat("%u%s", number, suffix);
Expand Down
8 changes: 4 additions & 4 deletions src/CppUTest/TestMemoryAllocator.cpp
Expand Up @@ -418,7 +418,7 @@ MemoryAccountantAllocationNode* MemoryAccountant::createNewAccountantAllocationN

void MemoryAccountant::destroyAccountantAllocationNode(MemoryAccountantAllocationNode* node) const
{
allocator_->free_memory((char*) node, sizeof(node), __FILE__, __LINE__);
allocator_->free_memory((char*) node, sizeof(node), __FILE__, __LINE__); // NOLINT
}

MemoryAccountant::MemoryAccountant()
Expand Down Expand Up @@ -479,9 +479,9 @@ MemoryAccountantAllocationNode* MemoryAccountant::findNodeOfSize(size_t size) co
{
if (useCacheSizes_) {
for (MemoryAccountantAllocationNode* node = head_; node; node = node->next_) {
if (size > node->size_ && node->next_ == NULLPTR)
return node;
else if (size <= node->size_ && !(node->next_->size_ != 0 && node->next_->size_ <= size))
if (((size > node->size_) && (node->next_ == NULLPTR))
|| ((size <= node->size_) &&
!((node->next_->size_ != 0) && (node->next_->size_ <= size))))
return node;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/CppUTestExt/CodeMemoryReportFormatter.cpp
Expand Up @@ -34,7 +34,7 @@
#define MAX_VARIABLE_NAME_LINE_PART 10
#define MAX_VARIABLE_NAME_FILE_PART 53
#define MAX_VARIABLE_NAME_SEPERATOR_PART 1
#define MAX_VARIABLE_NAME_LENGTH MAX_VARIABLE_NAME_FILE_PART + MAX_VARIABLE_NAME_SEPERATOR_PART + MAX_VARIABLE_NAME_LINE_PART
#define MAX_VARIABLE_NAME_LENGTH (MAX_VARIABLE_NAME_FILE_PART + MAX_VARIABLE_NAME_SEPERATOR_PART + MAX_VARIABLE_NAME_LINE_PART)

struct CodeReportingAllocationNode
{
Expand Down
13 changes: 11 additions & 2 deletions src/CppUTestExt/MockSupport.cpp
Expand Up @@ -44,9 +44,18 @@ MockSupport& mock(const SimpleString& mockName, MockFailureReporter* failureRepo
}

MockSupport::MockSupport(const SimpleString& mockName)
: actualCallOrder_(0), expectedCallOrder_(0), strictOrdering_(false), standardReporter_(&defaultReporter_), ignoreOtherCalls_(false), enabled_(true), lastActualFunctionCall_(NULLPTR), mockName_(mockName), tracing_(false)
:
actualCallOrder_(0),
expectedCallOrder_(0),
strictOrdering_(false),
activeReporter_(NULLPTR),
standardReporter_(&defaultReporter_),
ignoreOtherCalls_(false),
enabled_(true),
lastActualFunctionCall_(NULLPTR),
mockName_(mockName),
tracing_(false)
{
setActiveReporter(NULLPTR);
}

MockSupport::~MockSupport()
Expand Down
2 changes: 1 addition & 1 deletion tests/CppUTest/SimpleStringCacheTest.cpp
Expand Up @@ -188,7 +188,7 @@ TEST(SimpleStringInternalCache, clearCacheWillRemoveAllCachedMemoryButNotAllUsed
char* mem = cache.alloc(10);
cache.dealloc(mem, 10);

mem = cache.alloc(60);
cache.alloc(60);

cache.clearCache();

Expand Down
2 changes: 1 addition & 1 deletion tests/CppUTest/TestFailureNaNTest.cpp
Expand Up @@ -54,7 +54,7 @@ TEST_GROUP(TestFailureNanAndInf)
delete test;
}
};
#define FAILURE_EQUAL(a, b) STRCMP_EQUAL_LOCATION(a, b.getMessage().asCharString(), "", __FILE__, __LINE__)
#define FAILURE_EQUAL(a, b) STRCMP_EQUAL_LOCATION(a, (b).getMessage().asCharString(), "", __FILE__, __LINE__)

TEST(TestFailureNanAndInf, DoublesEqualExpectedIsNaN)
{
Expand Down
2 changes: 1 addition & 1 deletion tests/CppUTest/TestFailureTest.cpp
Expand Up @@ -47,7 +47,7 @@ TEST_GROUP(TestFailure)
delete test;
}
};
#define FAILURE_EQUAL(a, b) STRCMP_EQUAL_LOCATION(a, b.getMessage().asCharString(), "", __FILE__, __LINE__)
#define FAILURE_EQUAL(a, b) STRCMP_EQUAL_LOCATION(a, (b).getMessage().asCharString(), "", __FILE__, __LINE__)

TEST(TestFailure, CreateFailure)
{
Expand Down
2 changes: 1 addition & 1 deletion tests/CppUTest/TestMemoryAllocatorTest.cpp
Expand Up @@ -127,7 +127,7 @@ TEST(TestMemoryAllocatorTest, NullUnknownNames)

#if (! CPPUTEST_SANITIZE_ADDRESS)

#define MAX_SIZE_FOR_ALLOC ((size_t) -1 > (unsigned short)-1) ? (size_t) -97 : (size_t) -1
#define MAX_SIZE_FOR_ALLOC ((size_t) -1 > (unsigned short)-1) ? (size_t)(-97) : (size_t)(-1)

static void failTryingToAllocateTooMuchMemory(void)
{
Expand Down
8 changes: 4 additions & 4 deletions tests/CppUTest/TestUTestMacro.cpp
Expand Up @@ -615,16 +615,16 @@ TEST(UnitTestMacros, FailureWithLONGS_EQUALS)

static void failingTestMethodWithLONGS_EQUALWithSymbolicParameters_()
{
#define _MONDAY 1
int day_of_the_week = _MONDAY+1;
LONGS_EQUAL(_MONDAY, day_of_the_week);
#define MONDAY 1
int day_of_the_week = MONDAY+1;
LONGS_EQUAL(MONDAY, day_of_the_week);
TestTestingFixture::lineExecutedAfterCheck(); // LCOV_EXCL_LINE
} // LCOV_EXCL_LINE

TEST(UnitTestMacros, FailureWithLONGS_EQUALShowsSymbolicParameters)
{
fixture.runTestWithMethod(failingTestMethodWithLONGS_EQUALWithSymbolicParameters_);
CHECK_TEST_FAILS_PROPER_WITH_TEXT("LONGS_EQUAL(_MONDAY, day_of_the_week) failed");
CHECK_TEST_FAILS_PROPER_WITH_TEXT("LONGS_EQUAL(MONDAY, day_of_the_week) failed");
CHECK_TEST_FAILS_PROPER_WITH_TEXT("expected <1 (0x1)>");
CHECK_TEST_FAILS_PROPER_WITH_TEXT("but was <2 (0x2)>");
CHECK_FALSE(fixture.getOutput().contains("Message: "));
Expand Down
2 changes: 1 addition & 1 deletion tests/CppUTest/UtestPlatformTest.cpp
Expand Up @@ -126,7 +126,7 @@ TEST(UTestPlatformsTest_PlatformSpecificRunTestInASeperateProcess, FailureInSepa

static int accessViolationTestFunction_()
{
return *(volatile int*) NULLPTR;
return *(volatile int*) NULLPTR; // NOLINT
}

TEST(UTestPlatformsTest_PlatformSpecificRunTestInASeperateProcess, AccessViolationInSeparateProcessWorks)
Expand Down
2 changes: 1 addition & 1 deletion tests/CppUTestExt/OrderedTestTest.cpp
Expand Up @@ -82,7 +82,7 @@ TEST_GROUP(TestOrderedTest)

TEST(TestOrderedTest, TestInstallerSetsFields)
{
OrderedTestInstaller(orderedTest, "testgroup", "testname", "this.cpp", 10, 5);
OrderedTestInstaller installer(orderedTest, "testgroup", "testname", "this.cpp", 10, 5);
STRCMP_EQUAL("testgroup", orderedTest.getGroup().asCharString());
STRCMP_EQUAL("testname", orderedTest.getName().asCharString());
STRCMP_EQUAL("this.cpp", orderedTest.getFile().asCharString());
Expand Down

0 comments on commit 44ca541

Please sign in to comment.