Skip to content

Add compiler warning helper macros. #828

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

Open
wants to merge 24 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
6234b5a
Add CompilerWarnings.hpp which provides some helpful macros to make s…
mkoscumb Mar 9, 2021
aac511c
Add a use case for disabling a compiler warning using the macros.
mkoscumb Mar 9, 2021
9433762
Merge branch 'master' into mkoscumb-Add-Compiler-Warning-Macros
mkoscumb Apr 14, 2021
47b4447
I missed one last merge, whoops.
mkoscumb Apr 14, 2021
cd4584e
This warning supression wasn't actually doing anything, so I've remov…
mkoscumb Apr 14, 2021
8d7c708
Remove unused warning supression.
mkoscumb Apr 14, 2021
90f1ff2
Remove warning supression, simply cast result of CoCreateGuid to void…
mkoscumb Apr 14, 2021
4a880be
Remove deprecated method warning supression.
mkoscumb Apr 14, 2021
ed4d50d
Replace all #pragma warning supressions in NetworkDetector.cpp with t…
mkoscumb Apr 14, 2021
06b6630
Remove unnecessary #pragma warning in WindowsDesktopSystemInformation…
mkoscumb Apr 14, 2021
6f479a9
Remove unnecessary warning suppression.
mkoscumb Apr 14, 2021
2ac6f52
Replace warning pragmas in DebugLogger.hpp with macros from CompilerW…
mkoscumb Apr 14, 2021
503d40f
Add copyright blob to CompilerWarnings.hpp
mkoscumb Apr 14, 2021
46f64eb
Move warning suppressions in tests to macros. Remove unnecessary warn…
mkoscumb Apr 15, 2021
4de6d1d
Replace #else bodies with #error, to fail quickly on unsupported comp…
mkoscumb Apr 16, 2021
c17a01d
lib/include/mat/CompilerWarnings.hpp:42:48: 'OVERRIDEN' is a misspell…
mkoscumb Apr 16, 2021
4ef02f7
Replace __GCC__ with __GNUC__, which is the actual macro that GCC set…
mkoscumb Apr 16, 2021
372672b
Replace misspelling with MAT_DISABLE_WARNING_CONST_PARAMETER_NOT_OVER…
mkoscumb Apr 16, 2021
5482b4c
Merge branch 'master' into mkoscumb-Add-Compiler-Warning-Macros
mkoscumb Apr 20, 2021
ff2dff2
Merge branch 'master' into mkoscumb-Add-Compiler-Warning-Macros
mkoscumb Apr 26, 2021
0ae3118
Merge branch 'master' into mkoscumb-Add-Compiler-Warning-Macros
mkoscumb Apr 26, 2021
86e2270
Merge branch 'master' into mkoscumb-Add-Compiler-Warning-Macros
mkoscumb Apr 26, 2021
5d4c2f3
Merge branch 'master' into mkoscumb-Add-Compiler-Warning-Macros
mkoscumb Apr 29, 2021
7aef0d2
Merge branch 'main' into mkoscumb-Add-Compiler-Warning-Macros
mkoscumb Jun 28, 2023
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 Solutions/Clienttelemetry/Clienttelemetry.vcxitems
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,4 @@
<ItemGroup>
<None Include="$(MSBuildThisFileDirectory)..\..\lib\include\public\Version.hpp.template" />
</ItemGroup>
</Project>
</Project>
392 changes: 196 additions & 196 deletions Solutions/Clienttelemetry/Clienttelemetry.vcxitems.filters

Large diffs are not rendered by default.

4 changes: 0 additions & 4 deletions lib/api/LogManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
//
#ifdef _MSC_VER
// evntprov.h(838) : warning C4459 : declaration of 'Version' hides global declaration
#pragma warning(disable : 4459)
#endif
#include "LogManagerImpl.hpp"
#include "mat/config.h"

Expand Down
3 changes: 0 additions & 3 deletions lib/http/HttpClient_WinInet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
#include "mat/config.h"

#ifdef HAVE_MAT_DEFAULT_HTTP_CLIENT
#pragma warning(push)
#pragma warning(disable:4189) /* Turn off Level 4: local variable is initialized but not referenced. dwError unused in Release without printing it. */
#include "HttpClient_WinInet.hpp"
#include "utils/StringUtils.hpp"

Expand Down Expand Up @@ -563,6 +561,5 @@ bool HttpClient_WinInet::IsMsRootCheckRequired()
}

} MAT_NS_END
#pragma warning(pop)
#endif // HAVE_MAT_DEFAULT_HTTP_CLIENT
// clang-format on
63 changes: 63 additions & 0 deletions lib/include/mat/CompilerWarnings.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//
// Copyright (c) 2015-2020 Microsoft Corporation and Contributors.
// SPDX-License-Identifier: Apache-2.0
//
#ifndef MAT_COMPILERWARNINGS_HPP
#define MAT_COMPILERWARNINGS_HPP

#if defined(_MSC_VER)

#define MAT_PUSH_WARNINGS __pragma(warning(push))
#define MAT_POP_WARNINGS __pragma(warning(pop))
#define MAT_DISABLE_WARNING(warningNumber) __pragma(warning(disable: warningNumber))

#elif defined(__clang__) || defined(__GNUC__)

#define MAT_STRINGIFY_PRAGMA(args) _Pragma(#args)

#ifdef __clang__
#define MAT_STRINGIFY_PRAGMA_WITH_COMPILER(args) MAT_STRINGIFY_PRAGMA(clang args)
#else
#define MAT_STRINGIFY_PRAGMA_WITH_COMPILER(args) MAT_STRINGIFY_PRAGMA(GCC args)
#endif

#define MAT_PUSH_WARNINGS MAT_STRINGIFY_PRAGMA_WITH_COMPILER(diagnostic push)
#define MAT_POP_WARNINGS MAT_STRINGIFY_PRAGMA_WITH_COMPILER(diagnostic pop)
#define MAT_DISABLE_WARNING(warningName) MAT_STRINGIFY_PRAGMA_WITH_COMPILER(diagnostic ignored #warningName)

#else

#error Unsupported compiler toolchain.

#endif

/*

Define specific Disable warning macros here. Keep macros ordered alphabetically.

*/

#if defined(_MSC_VER)

#define MAT_DISABLE_WARNING_CONST_PARAMETER_NOT_OVERRIDDEN MAT_DISABLE_WARNING(4373)
#define MAT_DISABLE_WARNING_DECIMAL_TERMINATES_OCTAL_ESCAPE_SEQUCENCE MAT_DISABLE_WARNING(4125)
#define MAT_DISABLE_WARNING_DEPRECATED_METHOD_CALL MAT_DISABLE_WARNING(4996)
#define MAT_DISABLE_WARNING_EXCEPTION_EXECUTE_HANDLER MAT_DISABLE_WARNING(6320)
#define MAT_DISABLE_WARNING_EXPRESSION_IS_ALWAYS_FALSE MAT_DISABLE_WARNING(4296)
#define MAT_DISABLE_WARNING_INCONSISTENT_MISSING_OVERRIDE

#elif defined(__clang__) || defined(__GNUC__)

#define MAT_DISABLE_WARNING_CONST_PARAMETER_NOT_OVERRIDDEN
#define MAT_DISABLE_WARNING_DECIMAL_TERMINATES_OCTAL_ESCAPE_SEQUCENCE
#define MAT_DISABLE_WARNING_DEPRECATED_METHOD_CALL MAT_DISABLE_WARNING(-Wdeprecated-declarations)
#define MAT_DISABLE_WARNING_EXPRESSION_IS_ALWAYS_FALSE MAT_DISABLE_WARNING(-Wtype-limits)
#define MAT_DISABLE_WARNING_INCONSISTENT_MISSING_OVERRIDE MAT_DISABLE_WARNING(-Winconsistent-missing-override)

#else

#error Unsupported compiler toolchain.

#endif

#endif // MAT_COMPILERWARNINGS_HPP
21 changes: 4 additions & 17 deletions lib/offline/OfflineStorage_SQLite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "ILogManager.hpp"
#include "SQLiteWrapper.hpp"
#include "utils/StringUtils.hpp"
#include "mat/CompilerWarnings.hpp"
#include <algorithm>
#include <numeric>
#include <set>
Expand Down Expand Up @@ -765,16 +766,8 @@ namespace MAT_NS_BEGIN {
if (!stmt.select() || !stmt.getRow(m_pageSize)) { return false; }
}

#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable:4296) // expression always false.
#elif defined( __clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wtype-limits" // error: comparison of unsigned expression < 0 is always false [-Werror=type-limits]
#elif defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wtype-limits" // error: comparison of unsigned expression < 0 is always false [-Werror=type-limits]
#endif
MAT_PUSH_WARNINGS
MAT_DISABLE_WARNING_EXPRESSION_IS_ALWAYS_FALSE // error: comparison of unsigned expression < 0 is always false [-Werror=type-limits]

#define PREPARE_SQL(var_, stmt_) \
if ((var_ = m_db->prepare(stmt_)) < 0) { return false; }
Expand Down Expand Up @@ -863,13 +856,7 @@ namespace MAT_NS_BEGIN {

#undef PREPARE_SQL

#if defined(_MSC_VER)
#pragma warning(pop)
#elif defined(__clang__)
#pragma clang diagnostic pop
#elif defined(__GNUC__)
#pragma GCC diagnostic pop
#endif
MAT_POP_WARNINGS

ResizeDb();
return true;
Expand Down
18 changes: 1 addition & 17 deletions lib/pal/PAL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,6 @@ namespace PAL_NS_BEGIN {
#define _CRT_SECURE_NO_WARNINGS
#endif

#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable:4996)
#endif
void log(LogLevel level, char const* component, char const* fmt, ...)
{
#if defined(ANDROID) && !defined(ANDROID_SUPPRESS_LOGCAT)
Expand Down Expand Up @@ -287,9 +283,6 @@ namespace PAL_NS_BEGIN {
(void)(fmt);
#endif /* of #ifdef HAVE_MAT_LOGGING */
}
#ifdef _MSC_VER
#pragma warning(pop)
#endif

} // namespace detail

Expand All @@ -304,17 +297,11 @@ namespace PAL_NS_BEGIN {
return m_taskDispatcher;
}

#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable:6031)
#endif
std::string PlatformAbstractionLayer::generateUuidString() const
{
#ifdef _WIN32
GUID uuid = { 0, 0, 0, { 0, 0, 0, 0, 0, 0, 0, 0 } };
auto hr = CoCreateGuid(&uuid);
/* CoCreateGuid` will possiblity never fail, so ignoring the result */
UNREFERENCED_PARAMETER(hr);
(void) CoCreateGuid(&uuid);
return MAT::to_string(uuid);
#elif defined(__APPLE__)
auto uuid {CFUUIDCreate(kCFAllocatorDefault)};
Expand Down Expand Up @@ -367,9 +354,6 @@ namespace PAL_NS_BEGIN {
return buf;
#endif
}
#ifdef _MSC_VER
#pragma warning(pop)
#endif

int64_t PlatformAbstractionLayer::getUtcSystemTimeMs() const
{
Expand Down
16 changes: 7 additions & 9 deletions lib/pal/desktop/NetworkDetector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// SPDX-License-Identifier: Apache-2.0
//
#include "ctmacros.hpp"
#include "mat/CompilerWarnings.hpp"
#include "mat/config.h"
#ifdef HAVE_MAT_NETDETECT

Expand Down Expand Up @@ -81,8 +82,6 @@ namespace MAT_NS_BEGIN
/// This function can be called on any Windows release and it provides a SEH handler.
/// </summary>
/// <returns></returns>
#pragma warning(push)
#pragma warning(disable: 6320)
int NetworkDetector::GetCurrentNetworkCost()
{
#if 0
Expand All @@ -106,11 +105,14 @@ namespace MAT_NS_BEGIN
// Exception thrown at XXX (KernelBase.dll) in YYY : The binding handle is invalid.
// If there is a handler for this exception, the program may be safely continued.
//*******************************************************************************************************************************
MAT_PUSH_WARNINGS
MAT_DISABLE_WARNING_EXCEPTION_EXECUTE_HANDLER
__except (EXCEPTION_EXECUTE_HANDLER)
{
LOG_ERROR("Unable to obtain network state!");
m_currentNetworkCost = NetworkCost_Unknown;
}
MAT_POP_WARNINGS

// Notify the app about current network cost change
DebugEvent evt;
Expand All @@ -121,7 +123,6 @@ namespace MAT_NS_BEGIN

return m_currentNetworkCost;
}
#pragma warning(pop)

/// <summary>
/// Get current network connectivity state
Expand Down Expand Up @@ -438,19 +439,17 @@ namespace MAT_NS_BEGIN
/// <summary>
/// Register for COM events and block-wait in RegisterAndListen
/// </summary>
#pragma warning( push )
#pragma warning(disable:28159)
#pragma warning(disable:4996)
#pragma warning(disable:6320)
// We must use GetVersionEx to retain backwards compat with Win 7 SP1
void NetworkDetector::run()
{
// Check Windows version and if below Windows 8, then avoid running Network cost detection logic
OSVERSIONINFO osvi;
BOOL bIsWindows8orLater;
ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
MAT_PUSH_WARNINGS
MAT_DISABLE_WARNING_DEPRECATED_METHOD_CALL // We must use GetVersionEx to retain backwards compat with Win 7 SP1
GetVersionEx(&osvi);
MAT_POP_WARNINGS
bIsWindows8orLater = ((osvi.dwMajorVersion >= 6) && (osvi.dwMinorVersion >= 2)) || (osvi.dwMajorVersion > 6);
// Applications not manifested for Windows 8.1 or Windows 10 will return the Windows 8 OS version value (6.2)
if (!bIsWindows8orLater)
Expand Down Expand Up @@ -504,7 +503,6 @@ namespace MAT_NS_BEGIN
}

}
#pragma warning( pop )

/// <summary>
/// Start network monitoring thread
Expand Down
2 changes: 0 additions & 2 deletions lib/pal/desktop/WindowsDesktopSystemInformationImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#endif

#pragma warning( disable : 4996 )

// Windows 7.1 SDK module:
#pragma comment (lib, "Version.Lib")

Expand Down
7 changes: 4 additions & 3 deletions lib/tracing/api/DebugLogger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//
#ifndef DEBUGLOGGER_HPP
#define DEBUGLOGGER_HPP
#include "mat/CompilerWarnings.hpp"
/// <summary>
/// C++11 implementation of a cross-platform debug logging facility.
/// </summary>
Expand Down Expand Up @@ -602,16 +603,16 @@ class DebugLogger
auto now = std::chrono::system_clock::now();
int64_t millis = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count();
auto in_time_t = std::chrono::system_clock::to_time_t(now);
#pragma warning(push)
#pragma warning(disable: 4996)
MAT_PUSH_WARNINGS
MAT_DISABLE_WARNING_DEPRECATED_METHOD_CALL
// FIXME: this has been the case for all v1 builds and v2 Linux, but we should improve this because
// the static structure returned by localtime function may change its contents if another thread
// invokes localtime. Generally that is not an issue, as time won't change that fast, but it may
// potentially result an invalid log event timestamp.
//
// warning C4996: 'localtime': This function or variable may be unsafe. Consider using localtime_s instead
ss << std::put_time(localtime(&in_time_t), "%Y-%m-%d %X");
#pragma warning(pop)
MAT_POP_WARNINGS
ss << "." << std::setfill('0') << std::setw(3) << (unsigned)(millis % 1000);
ss << "|";
}
Expand Down
33 changes: 12 additions & 21 deletions tests/common/MockIEcsClient.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,15 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
//

#pragma once
#ifndef MOCKIECSCLIENT_HPP
#define MOCKIECSCLIENT_HPP
#include <ecsClientInterface.hpp>
#include "mat/CompilerWarnings.hpp"

namespace testing {

#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Winconsistent-missing-override" // GMock MOCK_METHOD* macros don't use override.
#endif

#ifdef _MSC_VER
// Avoid noise caused by ECS using const modifier on value-type arguments like int, bool and double.
// The modifiers are lost inside Google Mock, the resulting signature differs and compiler complains.
#pragma warning(push)
#pragma warning(disable:4373) // C4373: previous versions of the compiler did not override when parameters only differed by const/volatile qualifiers
#endif
MAT_PUSH_WARNINGS
MAT_DISABLE_WARNING_INCONSISTENT_MISSING_OVERRIDE // GMock MOCK_METHOD* macros don't use override.

class MockIEcsConfig : public ecsclient::IEcsConfig,
public virtual auf::Object
Expand All @@ -27,6 +19,10 @@ class MockIEcsConfig : public ecsclient::IEcsConfig,
MockIEcsConfig();
virtual ~MockIEcsConfig();

// Avoid noise caused by ECS using const modifier on value-type arguments like int, bool and double.
// The modifiers are lost inside Google Mock, the resulting signature differs and compiler complains.
MAT_PUSH_WARNINGS
MAT_DISABLE_WARNING_CONST_PARAMETER_NOT_OVERRIDDEN // C4373: previous versions of the compiler did not override when parameters only differed by const/volatile qualifiers
MOCK_CONST_METHOD3(GetSetting, std::string(std::string const & t, std::string const &, std::string const &));
MOCK_CONST_METHOD3(GetSettingAsString, std::string(std::string const &, std::string const &, std::string const &));
MOCK_CONST_METHOD3(GetSettingAsInt, int(std::string const &, std::string const &, int const defaultValue));
Expand All @@ -38,13 +34,9 @@ class MockIEcsConfig : public ecsclient::IEcsConfig,
MOCK_CONST_METHOD2(GetKeys, std::vector<std::string>(std::string const &, std::string const &));
MOCK_CONST_METHOD0(GetAgents, std::vector<std::string>());
MOCK_CONST_METHOD0(GetETag, std::string());
MAT_POP_WARNINGS
};

#ifdef _MSC_VER
#pragma warning(pop)
#endif


class MockIEcsClient : public ecsclient::IEcsClient {
public:
MockIEcsClient();
Expand All @@ -67,9 +59,8 @@ class MockIEcsClient : public ecsclient::IEcsClient {
MOCK_METHOD0(ResumeSendingRequests, void());
};

#if defined(__clang__)
#pragma clang diagnostic pop
#endif
MAT_POP_WARNINGS

} // namespace testing

#endif // #ifndef MOCKIECSCLIENT_HPP
Loading