Skip to content

Commit

Permalink
Fix assert overload, not supported by msvc
Browse files Browse the repository at this point in the history
warning C4002: too many arguments for function-like macro invocation 'ENFORCE_1'
  • Loading branch information
TheOneRing committed Jul 15, 2020
1 parent 266cece commit cbcaeb9
Show file tree
Hide file tree
Showing 41 changed files with 175 additions and 188 deletions.
20 changes: 4 additions & 16 deletions src/common/asserts.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,15 @@
#define OC_ASSERT_MSG qCritical
#endif

// For overloading macros by argument count
// See stackoverflow.com/questions/16683146/can-macros-be-overloaded-by-number-of-arguments
#define OC_ASSERT_CAT(A, B) A##B
#define OC_ASSERT_SELECT(NAME, NUM) OC_ASSERT_CAT(NAME##_, NUM)
#define OC_ASSERT_GET_COUNT(_1, _2, _3, COUNT, ...) COUNT
#define OC_ASSERT_VA_SIZE(...) OC_ASSERT_GET_COUNT(__VA_ARGS__, 3, 2, 1, 0)

#define OC_ASSERT_OVERLOAD(NAME, ...) OC_ASSERT_SELECT(NAME, OC_ASSERT_VA_SIZE(__VA_ARGS__)) \
(__VA_ARGS__)

// Default assert: If the condition is false in debug builds, terminate.
//
// Prints a message on failure, even in release builds.
#define ASSERT(...) OC_ASSERT_OVERLOAD(ASSERT, __VA_ARGS__)
#define ASSERT_1(cond) \
#define OC_ASSERT(cond) \
if (!(cond)) { \
OC_ASSERT_MSG("ASSERT: \"%s\" in file %s, line %d", #cond, __FILE__, __LINE__); \
} else { \
}
#define ASSERT_2(cond, message) \
#define OC_ASSERT_X(cond, message) \
if (!(cond)) { \
OC_ASSERT_MSG("ASSERT: \"%s\" in file %s, line %d with message: %s", #cond, __FILE__, __LINE__, message); \
} else { \
Expand All @@ -37,13 +26,12 @@
// Enforce condition to be true, even in release builds.
//
// Prints 'message' and aborts execution if 'cond' is false.
#define ENFORCE(...) OC_ASSERT_OVERLOAD(ENFORCE, __VA_ARGS__)
#define ENFORCE_1(cond) \
#define OC_ENFORCE(cond) \
if (!(cond)) { \
qFatal("ENFORCE: \"%s\" in file %s, line %d", #cond, __FILE__, __LINE__); \
} else { \
}
#define ENFORCE_2(cond, message) \
#define OC_ENFORCE_X(cond, message) \
if (!(cond)) { \
qFatal("ENFORCE: \"%s\" in file %s, line %d with message: %s", #cond, __FILE__, __LINE__, message); \
} else { \
Expand Down
4 changes: 2 additions & 2 deletions src/common/checksums.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,9 @@ void ComputeChecksum::start(const QString &filePath)

void ComputeChecksum::start(std::unique_ptr<QIODevice> device)
{
ENFORCE(device);
OC_ENFORCE(device);
qCInfo(lcChecksums) << "Computing" << checksumType() << "checksum of device" << device.get() << "in a thread";
ASSERT(!device->parent());
OC_ASSERT(!device->parent());

startImpl(std::move(device));
}
Expand Down
10 changes: 5 additions & 5 deletions src/common/ownsql.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,9 @@ int SqlQuery::prepare(const QByteArray &sql, bool allow_failure)
if (_errId != SQLITE_OK) {
_error = QString::fromUtf8(sqlite3_errmsg(_db));
qCWarning(lcSql) << "Sqlite prepare statement error:" << _error << "in" << _sql;
ENFORCE(allow_failure, "SQLITE Prepare error");
OC_ENFORCE_X(allow_failure, "SQLITE Prepare error");
} else {
ASSERT(_stmt);
OC_ASSERT(_stmt);
_sqldb->_queries.insert(this);
}
}
Expand Down Expand Up @@ -371,7 +371,7 @@ void SqlQuery::bindValue(int pos, const QVariant &value)

int res = -1;
if (!_stmt) {
ASSERT(false);
OC_ASSERT(false);
return;
}

Expand Down Expand Up @@ -429,7 +429,7 @@ void SqlQuery::bindValue(int pos, const QVariant &value)
if (res != SQLITE_OK) {
qCWarning(lcSql) << "ERROR binding SQL value:" << value << "error:" << res;
}
ASSERT(res == SQLITE_OK);
OC_ASSERT(res == SQLITE_OK);
}

bool SqlQuery::nullValue(int index)
Expand Down Expand Up @@ -499,7 +499,7 @@ void SqlQuery::reset_and_clear_bindings()

bool SqlQuery::initOrReset(const QByteArray &sql, OCC::SqlDatabase &db)
{
ENFORCE(!_sqldb || &db == _sqldb);
OC_ENFORCE(!_sqldb || &db == _sqldb);
_sqldb = &db;
_db = db.sqliteDb();
if (_stmt) {
Expand Down
10 changes: 5 additions & 5 deletions src/common/result.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,29 +101,29 @@ class Result

const T &operator*() const &
{
ASSERT(!_isError);
OC_ASSERT(!_isError);
return _result;
}
T operator*() &&
{
ASSERT(!_isError);
OC_ASSERT(!_isError);
return std::move(_result);
}

const T *operator->() const
{
ASSERT(!_isError);
OC_ASSERT(!_isError);
return &_result;
}

const Error &error() const &
{
ASSERT(_isError);
OC_ASSERT(_isError);
return _error;
}
Error error() &&
{
ASSERT(_isError);
OC_ASSERT(_isError);
return std::move(_error);
}
};
Expand Down
72 changes: 36 additions & 36 deletions src/common/syncjournaldb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ bool SyncJournalDb::sqlFail(const QString &log, const SqlQuery &query)
commitTransaction();
qCWarning(lcDb) << "SQL Error" << log << query.error();
_db.close();
ASSERT(false);
OC_ASSERT(false);
return false;
}

Expand Down Expand Up @@ -1752,7 +1752,7 @@ void SyncJournalDb::setPollInfo(const SyncJournalDb::PollInfo &info)
QStringList SyncJournalDb::getSelectiveSyncList(SyncJournalDb::SelectiveSyncListType type, bool *ok)
{
QStringList result;
ASSERT(ok);
OC_ASSERT(ok);

QMutexLocker locker(&_mutex);
if (!checkConnect()) {
Expand Down Expand Up @@ -1991,17 +1991,17 @@ void SyncJournalDb::setConflictRecord(const ConflictRecord &record)
return;

auto &query = _setConflictRecordQuery;
ASSERT(query.initOrReset(QByteArrayLiteral(
"INSERT OR REPLACE INTO conflicts "
"(path, baseFileId, baseModtime, baseEtag, basePath) "
"VALUES (?1, ?2, ?3, ?4, ?5);"),
OC_ASSERT(query.initOrReset(QByteArrayLiteral(
"INSERT OR REPLACE INTO conflicts "
"(path, baseFileId, baseModtime, baseEtag, basePath) "
"VALUES (?1, ?2, ?3, ?4, ?5);"),
_db));
query.bindValue(1, record.path);
query.bindValue(2, record.baseFileId);
query.bindValue(3, record.baseModtime);
query.bindValue(4, record.baseEtag);
query.bindValue(5, record.initialBasePath);
ASSERT(query.exec());
OC_ASSERT(query.exec());
}

ConflictRecord SyncJournalDb::conflictRecord(const QByteArray &path)
Expand All @@ -2012,9 +2012,9 @@ ConflictRecord SyncJournalDb::conflictRecord(const QByteArray &path)
if (!checkConnect())
return entry;
auto &query = _getConflictRecordQuery;
ASSERT(query.initOrReset(QByteArrayLiteral("SELECT baseFileId, baseModtime, baseEtag, basePath FROM conflicts WHERE path=?1;"), _db));
OC_ASSERT(query.initOrReset(QByteArrayLiteral("SELECT baseFileId, baseModtime, baseEtag, basePath FROM conflicts WHERE path=?1;"), _db));
query.bindValue(1, path);
ASSERT(query.exec());
OC_ASSERT(query.exec());
if (!query.next().hasData)
return entry;

Expand All @@ -2032,9 +2032,9 @@ void SyncJournalDb::deleteConflictRecord(const QByteArray &path)
if (!checkConnect())
return;

ASSERT(_deleteConflictRecordQuery.initOrReset("DELETE FROM conflicts WHERE path=?1;", _db));
OC_ASSERT(_deleteConflictRecordQuery.initOrReset("DELETE FROM conflicts WHERE path=?1;", _db));
_deleteConflictRecordQuery.bindValue(1, path);
ASSERT(_deleteConflictRecordQuery.exec());
OC_ASSERT(_deleteConflictRecordQuery.exec());
}

QByteArrayList SyncJournalDb::conflictRecordPaths()
Expand All @@ -2045,7 +2045,7 @@ QByteArrayList SyncJournalDb::conflictRecordPaths()

SqlQuery query(_db);
query.prepare("SELECT path FROM conflicts");
ASSERT(query.exec());
OC_ASSERT(query.exec());

QByteArrayList paths;
while (query.next().hasData)
Expand Down Expand Up @@ -2107,8 +2107,8 @@ Optional<PinState> SyncJournalDb::PinStateInterface::rawForPath(const QByteArray
return {};

auto &query = _db->_getRawPinStateQuery;
ASSERT(query.initOrReset(QByteArrayLiteral(
"SELECT pinState FROM flags WHERE path == ?1;"),
OC_ASSERT(query.initOrReset(QByteArrayLiteral(
"SELECT pinState FROM flags WHERE path == ?1;"),
_db->_db));
query.bindValue(1, path);
query.exec();
Expand All @@ -2130,13 +2130,13 @@ Optional<PinState> SyncJournalDb::PinStateInterface::effectiveForPath(const QByt
return {};

auto &query = _db->_getEffectivePinStateQuery;
ASSERT(query.initOrReset(QByteArrayLiteral(
"SELECT pinState FROM flags WHERE"
// explicitly allow "" to represent the root path
// (it'd be great if paths started with a / and "/" could be the root)
" (" IS_PREFIX_PATH_OR_EQUAL("path", "?1") " OR path == '')"
" AND pinState is not null AND pinState != 0"
" ORDER BY length(path) DESC LIMIT 1;"),
OC_ASSERT(query.initOrReset(QByteArrayLiteral(
"SELECT pinState FROM flags WHERE"
// explicitly allow "" to represent the root path
// (it'd be great if paths started with a / and "/" could be the root)
" (" IS_PREFIX_PATH_OR_EQUAL("path", "?1") " OR path == '')"
" AND pinState is not null AND pinState != 0"
" ORDER BY length(path) DESC LIMIT 1;"),
_db->_db));
query.bindValue(1, path);
query.exec();
Expand Down Expand Up @@ -2165,10 +2165,10 @@ Optional<PinState> SyncJournalDb::PinStateInterface::effectiveForPathRecursive(c

// Find all the non-inherited pin states below the item
auto &query = _db->_getSubPinsQuery;
ASSERT(query.initOrReset(QByteArrayLiteral(
"SELECT DISTINCT pinState FROM flags WHERE"
" (" IS_PREFIX_PATH_OF("?1", "path") " OR ?1 == '')"
" AND pinState is not null and pinState != 0;"),
OC_ASSERT(query.initOrReset(QByteArrayLiteral(
"SELECT DISTINCT pinState FROM flags WHERE"
" (" IS_PREFIX_PATH_OF("?1", "path") " OR ?1 == '')"
" AND pinState is not null and pinState != 0;"),
_db->_db));
query.bindValue(1, path);
query.exec();
Expand All @@ -2195,13 +2195,13 @@ void SyncJournalDb::PinStateInterface::setForPath(const QByteArray &path, PinSta
return;

auto &query = _db->_setPinStateQuery;
ASSERT(query.initOrReset(QByteArrayLiteral(
// If we had sqlite >=3.24.0 everywhere this could be an upsert,
// making further flags columns easy
//"INSERT INTO flags(path, pinState) VALUES(?1, ?2)"
//" ON CONFLICT(path) DO UPDATE SET pinState=?2;"),
// Simple version that doesn't work nicely with multiple columns:
"INSERT OR REPLACE INTO flags(path, pinState) VALUES(?1, ?2);"),
OC_ASSERT(query.initOrReset(QByteArrayLiteral(
// If we had sqlite >=3.24.0 everywhere this could be an upsert,
// making further flags columns easy
//"INSERT INTO flags(path, pinState) VALUES(?1, ?2)"
//" ON CONFLICT(path) DO UPDATE SET pinState=?2;"),
// Simple version that doesn't work nicely with multiple columns:
"INSERT OR REPLACE INTO flags(path, pinState) VALUES(?1, ?2);"),
_db->_db));
query.bindValue(1, path);
query.bindValue(2, static_cast<int>(state));
Expand All @@ -2215,10 +2215,10 @@ void SyncJournalDb::PinStateInterface::wipeForPathAndBelow(const QByteArray &pat
return;

auto &query = _db->_wipePinStateQuery;
ASSERT(query.initOrReset(QByteArrayLiteral(
"DELETE FROM flags WHERE "
// Allow "" to delete everything
" (" IS_PREFIX_PATH_OR_EQUAL("?1", "path") " OR ?1 == '');"),
OC_ASSERT(query.initOrReset(QByteArrayLiteral(
"DELETE FROM flags WHERE "
// Allow "" to delete everything
" (" IS_PREFIX_PATH_OR_EQUAL("?1", "path") " OR ?1 == '');"),
_db->_db));
query.bindValue(1, path);
query.exec();
Expand Down
26 changes: 13 additions & 13 deletions src/common/utility_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,13 @@ QVariant Utility::registryGetKeyValue(HKEY hRootKey, const QString &subKey, cons

REGSAM sam = KEY_READ | KEY_WOW64_64KEY;
LONG result = RegOpenKeyEx(hRootKey, reinterpret_cast<LPCWSTR>(subKey.utf16()), 0, sam, &hKey);
ASSERT(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND);
OC_ASSERT(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND);
if (result != ERROR_SUCCESS)
return value;

DWORD type = 0, sizeInBytes = 0;
result = RegQueryValueEx(hKey, reinterpret_cast<LPCWSTR>(valueName.utf16()), 0, &type, nullptr, &sizeInBytes);
ASSERT(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND);
OC_ASSERT(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND);
if (result == ERROR_SUCCESS) {
switch (type) {
case REG_DWORD:
Expand Down Expand Up @@ -145,7 +145,7 @@ QVariant Utility::registryGetKeyValue(HKEY hRootKey, const QString &subKey, cons
Q_UNREACHABLE();
}
}
ASSERT(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND);
OC_ASSERT(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND);

RegCloseKey(hKey);
return value;
Expand All @@ -160,7 +160,7 @@ bool Utility::registrySetKeyValue(HKEY hRootKey, const QString &subKey, const QS
// FIXME: Not doing so at the moment means that explorer will show the cloud provider, but 32bit processes' open dialogs (like the ownCloud client itself) won't show it.
REGSAM sam = KEY_WRITE | KEY_WOW64_64KEY;
LONG result = RegCreateKeyEx(hRootKey, reinterpret_cast<LPCWSTR>(subKey.utf16()), 0, nullptr, 0, sam, nullptr, &hKey, nullptr);
ASSERT(result == ERROR_SUCCESS);
OC_ASSERT(result == ERROR_SUCCESS);
if (result != ERROR_SUCCESS)
return false;

Expand All @@ -180,7 +180,7 @@ bool Utility::registrySetKeyValue(HKEY hRootKey, const QString &subKey, const QS
default:
Q_UNREACHABLE();
}
ASSERT(result == ERROR_SUCCESS);
OC_ASSERT(result == ERROR_SUCCESS);

RegCloseKey(hKey);
return result == ERROR_SUCCESS;
Expand All @@ -191,16 +191,16 @@ bool Utility::registryDeleteKeyTree(HKEY hRootKey, const QString &subKey)
HKEY hKey;
REGSAM sam = DELETE | KEY_ENUMERATE_SUB_KEYS | KEY_QUERY_VALUE | KEY_SET_VALUE | KEY_WOW64_64KEY;
LONG result = RegOpenKeyEx(hRootKey, reinterpret_cast<LPCWSTR>(subKey.utf16()), 0, sam, &hKey);
ASSERT(result == ERROR_SUCCESS);
OC_ASSERT(result == ERROR_SUCCESS);
if (result != ERROR_SUCCESS)
return false;

result = RegDeleteTree(hKey, nullptr);
RegCloseKey(hKey);
ASSERT(result == ERROR_SUCCESS);
OC_ASSERT(result == ERROR_SUCCESS);

result |= RegDeleteKeyEx(hRootKey, reinterpret_cast<LPCWSTR>(subKey.utf16()), sam, 0);
ASSERT(result == ERROR_SUCCESS);
OC_ASSERT(result == ERROR_SUCCESS);

return result == ERROR_SUCCESS;
}
Expand All @@ -210,12 +210,12 @@ bool Utility::registryDeleteKeyValue(HKEY hRootKey, const QString &subKey, const
HKEY hKey;
REGSAM sam = KEY_WRITE | KEY_WOW64_64KEY;
LONG result = RegOpenKeyEx(hRootKey, reinterpret_cast<LPCWSTR>(subKey.utf16()), 0, sam, &hKey);
ASSERT(result == ERROR_SUCCESS);
OC_ASSERT(result == ERROR_SUCCESS);
if (result != ERROR_SUCCESS)
return false;

result = RegDeleteValue(hKey, reinterpret_cast<LPCWSTR>(valueName.utf16()));
ASSERT(result == ERROR_SUCCESS);
OC_ASSERT(result == ERROR_SUCCESS);

RegCloseKey(hKey);
return result == ERROR_SUCCESS;
Expand All @@ -226,14 +226,14 @@ bool Utility::registryWalkSubKeys(HKEY hRootKey, const QString &subKey, const st
HKEY hKey;
REGSAM sam = KEY_READ | KEY_WOW64_64KEY;
LONG result = RegOpenKeyEx(hRootKey, reinterpret_cast<LPCWSTR>(subKey.utf16()), 0, sam, &hKey);
ASSERT(result == ERROR_SUCCESS);
OC_ASSERT(result == ERROR_SUCCESS);
if (result != ERROR_SUCCESS)
return false;

DWORD maxSubKeyNameSize;
// Get the largest keyname size once instead of relying each call on ERROR_MORE_DATA.
result = RegQueryInfoKey(hKey, nullptr, nullptr, nullptr, nullptr, &maxSubKeyNameSize, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
ASSERT(result == ERROR_SUCCESS);
OC_ASSERT(result == ERROR_SUCCESS);
if (result != ERROR_SUCCESS) {
RegCloseKey(hKey);
return false;
Expand All @@ -250,7 +250,7 @@ bool Utility::registryWalkSubKeys(HKEY hRootKey, const QString &subKey, const st
DWORD subKeyNameSize = subKeyName.size();
retCode = RegEnumKeyEx(hKey, i, reinterpret_cast<LPWSTR>(subKeyName.data()), &subKeyNameSize, nullptr, nullptr, nullptr, nullptr);

ASSERT(result == ERROR_SUCCESS || retCode == ERROR_NO_MORE_ITEMS);
OC_ASSERT(result == ERROR_SUCCESS || retCode == ERROR_NO_MORE_ITEMS);
if (retCode == ERROR_SUCCESS) {
// subKeyNameSize excludes the trailing \0
subKeyName.resize(subKeyNameSize);
Expand Down
Loading

0 comments on commit cbcaeb9

Please sign in to comment.