Skip to content

Commit

Permalink
Make constexpr constants in headers inline constexpr (#29279)
Browse files Browse the repository at this point in the history
* Make constexpr constants in headers inline constexpr

This makes the linker deduplicate these constants, instead of emitting one
definition per compilation unit. E.g. chip::System::Clock::kZero currently
exists >10 times in the chip-tool binary.

* Restyle
  • Loading branch information
ksperling-apple authored and pull[bot] committed Oct 5, 2023
1 parent 224d380 commit ad79e4d
Show file tree
Hide file tree
Showing 95 changed files with 354 additions and 354 deletions.
4 changes: 2 additions & 2 deletions examples/chip-tool/commands/clusters/ComplexArgument.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@

#include "JsonParser.h"

constexpr uint8_t kMaxLabelLength = UINT8_MAX;
constexpr const char kNullString[] = "null";
inline constexpr uint8_t kMaxLabelLength = UINT8_MAX;
inline constexpr const char kNullString[] = "null";

class ComplexArgumentParser
{
Expand Down
6 changes: 3 additions & 3 deletions examples/chip-tool/commands/clusters/WriteAttributeCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
#include "DataModelLogger.h"
#include "ModelCommand.h"

constexpr const char * kWriteCommandKey = "write";
constexpr const char * kWriteByIdCommandKey = "write-by-id";
constexpr const char * kForceWriteCommandKey = "force-write";
inline constexpr const char * kWriteCommandKey = "write";
inline constexpr const char * kWriteByIdCommandKey = "write-by-id";
inline constexpr const char * kForceWriteCommandKey = "force-write";

enum class WriteCommandType
{
Expand Down
8 changes: 4 additions & 4 deletions examples/chip-tool/commands/common/CHIPCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@

#pragma once

constexpr const char kIdentityAlpha[] = "alpha";
constexpr const char kIdentityBeta[] = "beta";
constexpr const char kIdentityGamma[] = "gamma";
inline constexpr const char kIdentityAlpha[] = "alpha";
inline constexpr const char kIdentityBeta[] = "beta";
inline constexpr const char kIdentityGamma[] = "gamma";
// The null fabric commissioner is a commissioner that isn't on a fabric.
// This is a legal configuration in which the commissioner delegates
// operational communication and invocation of the commssioning complete
Expand All @@ -46,7 +46,7 @@ constexpr const char kIdentityGamma[] = "gamma";
// commissioner portion of such an architecture. The null-fabric-commissioner
// can carry a commissioning flow up until the point of operational channel
// (CASE) communcation.
constexpr const char kIdentityNull[] = "null-fabric-commissioner";
inline constexpr const char kIdentityNull[] = "null-fabric-commissioner";

class CHIPCommand : public Command
{
Expand Down
16 changes: 8 additions & 8 deletions examples/chip-tool/commands/common/CustomStringPrefix.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@

#include <lib/support/CodeUtils.h>

static constexpr char kJsonStringPrefix[] = "json:";
constexpr size_t kJsonStringPrefixLen = ArraySize(kJsonStringPrefix) - 1; // Don't count the null
static constexpr char kJsonStringPrefix[] = "json:";
inline constexpr size_t kJsonStringPrefixLen = ArraySize(kJsonStringPrefix) - 1; // Don't count the null

static constexpr char kBase64StringPrefix[] = "base64:";
constexpr size_t kBase64StringPrefixLen = ArraySize(kBase64StringPrefix) - 1; // Don't count the null
static constexpr char kBase64StringPrefix[] = "base64:";
inline constexpr size_t kBase64StringPrefixLen = ArraySize(kBase64StringPrefix) - 1; // Don't count the null

static constexpr char kHexStringPrefix[] = "hex:";
constexpr size_t kHexStringPrefixLen = ArraySize(kHexStringPrefix) - 1; // Don't count the null
static constexpr char kHexStringPrefix[] = "hex:";
inline constexpr size_t kHexStringPrefixLen = ArraySize(kHexStringPrefix) - 1; // Don't count the null

static constexpr char kStrStringPrefix[] = "str:";
constexpr size_t kStrStringPrefixLen = ArraySize(kStrStringPrefix) - 1; // Don't count the null
static constexpr char kStrStringPrefix[] = "str:";
inline constexpr size_t kStrStringPrefixLen = ArraySize(kStrStringPrefix) - 1; // Don't count the null

inline bool IsJsonString(const char * str)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@

#pragma once

constexpr const char kIdentityAlpha[] = "alpha";
constexpr const char kIdentityBeta[] = "beta";
constexpr const char kIdentityGamma[] = "gamma";
inline constexpr const char kIdentityAlpha[] = "alpha";
inline constexpr const char kIdentityBeta[] = "beta";
inline constexpr const char kIdentityGamma[] = "gamma";

class CHIPCommandBridge : public Command {
public:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const char basePath[] = "./src/app/tests/suites/commands/delay/scripts/";
const char * getScriptsFolder() { return basePath; }
} // namespace

constexpr const char * kDefaultKey = "default";
inline constexpr const char * kDefaultKey = "default";

@interface TestDeviceControllerDelegate : NSObject <MTRDeviceControllerDelegate>
@property TestCommandBridge * commandBridge;
Expand All @@ -58,7 +58,7 @@ constexpr const char * kDefaultKey = "default";

NS_ASSUME_NONNULL_END

constexpr uint16_t kTimeoutInSeconds = 90;
inline constexpr uint16_t kTimeoutInSeconds = 90;

class TestCommandBridge : public CHIPCommandBridge,
public ValueChecker,
Expand Down
6 changes: 3 additions & 3 deletions examples/placeholder/linux/include/TestCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
#include <app-common/zap-generated/ids/Clusters.h>
#include <app-common/zap-generated/ids/Commands.h>

constexpr const char kIdentityAlpha[] = "";
constexpr const char kIdentityBeta[] = "";
constexpr const char kIdentityGamma[] = "";
inline constexpr const char kIdentityAlpha[] = "";
inline constexpr const char kIdentityBeta[] = "";
inline constexpr const char kIdentityGamma[] = "";

class TestCommand : public TestRunner,
public PICSChecker,
Expand Down
10 changes: 5 additions & 5 deletions examples/platform/telink/common/include/AppTaskCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ using namespace ::chip::Credentials;
using namespace ::chip::DeviceLayer;

namespace {
constexpr EndpointId kExampleEndpointId = 1;
constexpr uint8_t kDefaultMinLevel = 0;
constexpr uint8_t kDefaultMaxLevel = 254;
constexpr uint8_t kButtonPushEvent = 1;
constexpr uint8_t kButtonReleaseEvent = 0;
inline constexpr EndpointId kExampleEndpointId = 1;
inline constexpr uint8_t kDefaultMinLevel = 0;
inline constexpr uint8_t kDefaultMaxLevel = 254;
inline constexpr uint8_t kButtonPushEvent = 1;
inline constexpr uint8_t kButtonReleaseEvent = 0;
} // namespace

class AppTaskCommon
Expand Down
6 changes: 3 additions & 3 deletions examples/shell/shell_common/include/Globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
#include <transport/raw/UDP.h>

#if INET_CONFIG_ENABLE_TCP_ENDPOINT
constexpr size_t kMaxTcpActiveConnectionCount = 4;
constexpr size_t kMaxTcpPendingPackets = 4;
inline constexpr size_t kMaxTcpActiveConnectionCount = 4;
inline constexpr size_t kMaxTcpPendingPackets = 4;
#endif
constexpr chip::System::Clock::Timeout kResponseTimeOut = chip::System::Clock::Seconds16(1);
inline constexpr chip::System::Clock::Timeout kResponseTimeOut = chip::System::Clock::Seconds16(1);

extern chip::FabricTable gFabricTable;
extern chip::secure_channel::MessageCounterManager gMessageCounterManager;
Expand Down
2 changes: 1 addition & 1 deletion examples/tv-casting-app/linux/CastingUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include "TargetEndpointInfo.h"
#include "TargetVideoPlayerInfo.h"

constexpr uint32_t kCommissionerDiscoveryTimeoutInMs = 5 * 1000;
inline constexpr uint32_t kCommissionerDiscoveryTimeoutInMs = 5 * 1000;

CHIP_ERROR ProcessClusterCommand(int argc, char ** argv);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
#include <controller/CHIPCommissionableNodeController.h>
#include <functional>

constexpr chip::System::Clock::Seconds16 kCommissioningWindowTimeout = chip::System::Clock::Seconds16(3 * 60);
inline constexpr chip::System::Clock::Seconds16 kCommissioningWindowTimeout = chip::System::Clock::Seconds16(3 * 60);

/**
* @brief Represents a TV Casting server that can get the casting app commissioned
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include "TargetEndpointInfo.h"
#include "TargetVideoPlayerInfo.h"

constexpr size_t kMaxCachedVideoPlayers = 32;
inline constexpr size_t kMaxCachedVideoPlayers = 32;

class PersistenceManager : public chip::FabricTable::Delegate
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

#include <platform/CHIPDeviceLayer.h>

constexpr size_t kMaxNumberOfClustersPerEndpoint = 10;
inline constexpr size_t kMaxNumberOfClustersPerEndpoint = 10;

class TargetEndpointInfo
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

#include <app-common/zap-generated/cluster-objects.h>

constexpr size_t kMaxNumberOfEndpoints = 5;
inline constexpr size_t kMaxNumberOfEndpoints = 5;

class TargetVideoPlayerInfo;
class VideoPlayerConnectionContext
Expand Down
2 changes: 1 addition & 1 deletion src/app/EventLoggingTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include <lib/core/TLV.h>
#include <system/SystemPacketBuffer.h>

constexpr size_t kNumPriorityLevel = 3;
inline constexpr size_t kNumPriorityLevel = 3;
namespace chip {
namespace app {

Expand Down
6 changes: 3 additions & 3 deletions src/app/EventManagement.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@

namespace chip {
namespace app {
constexpr const uint32_t kEventManagementProfile = 0x1;
constexpr const uint32_t kFabricIndexTag = 0x1;
constexpr size_t kMaxEventSizeReserve = 512;
inline constexpr const uint32_t kEventManagementProfile = 0x1;
inline constexpr const uint32_t kFabricIndexTag = 0x1;
inline constexpr size_t kMaxEventSizeReserve = 512;
constexpr uint16_t kRequiredEventField =
(1 << to_underlying(EventDataIB::Tag::kPriority)) | (1 << to_underlying(EventDataIB::Tag::kPath));

Expand Down
2 changes: 1 addition & 1 deletion src/app/InteractionModelRevision.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@
#define CHIP_DEVICE_INTERACTION_MODEL_REVISION 11
#endif

constexpr uint8_t kInteractionModelRevisionTag = 0xFF;
inline constexpr uint8_t kInteractionModelRevisionTag = 0xFF;
2 changes: 1 addition & 1 deletion src/app/ReadHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
#include <system/SystemPacketBuffer.h>

// https://github.com/CHIP-Specifications/connectedhomeip-spec/blob/61a9d19e6af12fdfb0872bcff26d19de6c680a1a/src/Ch02_Architecture.adoc#1122-subscribe-interaction-limits
constexpr uint16_t kSubscriptionMaxIntervalPublisherLimit = 3600; // seconds (60 minutes)
inline constexpr uint16_t kSubscriptionMaxIntervalPublisherLimit = 3600; // seconds (60 minutes)

namespace chip {
namespace app {
Expand Down
6 changes: 3 additions & 3 deletions src/app/app-platform/ContentAppPlatform.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ EmberAfStatus AppPlatformExternalAttributeReadCallback(EndpointId endpoint, Clus
EmberAfStatus AppPlatformExternalAttributeWriteCallback(EndpointId endpoint, ClusterId clusterId,
const EmberAfAttributeMetadata * attributeMetadata, uint8_t * buffer);

constexpr EndpointId kTargetBindingClusterEndpointId = 0;
constexpr EndpointId kLocalVideoPlayerEndpointId = 1;
constexpr EndpointId kLocalSpeakerEndpointId = 2;
inline constexpr EndpointId kTargetBindingClusterEndpointId = 0;
inline constexpr EndpointId kLocalVideoPlayerEndpointId = 1;
inline constexpr EndpointId kLocalSpeakerEndpointId = 2;

class DLL_EXPORT ContentAppFactory
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ namespace app {
namespace Clusters {
namespace OperationalState {

constexpr size_t kOperationalStateLabelMaxSize = 64u;
constexpr size_t kOperationalErrorLabelMaxSize = 64u;
constexpr size_t kOperationalErrorDetailsMaxSize = 64u;
constexpr size_t kOperationalPhaseNameMaxSize = 64u;
inline constexpr size_t kOperationalStateLabelMaxSize = 64u;
inline constexpr size_t kOperationalErrorLabelMaxSize = 64u;
inline constexpr size_t kOperationalErrorDetailsMaxSize = 64u;
inline constexpr size_t kOperationalPhaseNameMaxSize = 64u;

/**
* A class which represents the operational state of an Operational State cluster derivation instance.
Expand Down
6 changes: 3 additions & 3 deletions src/app/clusters/scenes-server/SceneTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ typedef uint16_t SceneIndex;
typedef uint32_t TransitionTimeMs;
typedef uint32_t SceneTransitionTime;

constexpr GroupId kGlobalGroupSceneId = 0x0000;
constexpr SceneIndex kUndefinedSceneIndex = 0xffff;
constexpr SceneId kUndefinedSceneId = 0xff;
inline constexpr GroupId kGlobalGroupSceneId = 0x0000;
inline constexpr SceneIndex kUndefinedSceneIndex = 0xffff;
inline constexpr SceneId kUndefinedSceneId = 0xff;

static constexpr size_t kIteratorsMax = CHIP_CONFIG_MAX_SCENES_CONCURRENT_ITERATORS;
static constexpr size_t kSceneNameMaxLength = CHIP_CONFIG_SCENES_CLUSTER_MAXIMUM_NAME_LENGTH;
Expand Down
4 changes: 2 additions & 2 deletions src/app/data-model/Encode.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ template <typename Enum, Enum value>
using VoidType = void;

template <typename, typename = void>
constexpr bool HasUnknownValue = false;
inline constexpr bool HasUnknownValue = false;

template <typename T>
constexpr bool HasUnknownValue<T, VoidType<T, T::kUnknownEnumValue>> = true;
inline constexpr bool HasUnknownValue<T, VoidType<T, T::kUnknownEnumValue>> = true;
} // namespace detail

/*
Expand Down
2 changes: 1 addition & 1 deletion src/app/data-model/Nullable.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace DataModel {
/**
* NullNullable is an alias for NullOptional, for better readability.
*/
constexpr auto NullNullable = NullOptional;
inline constexpr auto NullNullable = NullOptional;

/*
* Dedicated type for nullable things, to differentiate them from optional
Expand Down
2 changes: 1 addition & 1 deletion src/app/icd/IcdMonitoringTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

namespace chip {

constexpr size_t kIcdMonitoringBufferSize = 40;
inline constexpr size_t kIcdMonitoringBufferSize = 40;

struct IcdMonitoringEntry : public PersistentData<kIcdMonitoringBufferSize>
{
Expand Down
2 changes: 1 addition & 1 deletion src/app/server/Server.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@

namespace chip {

constexpr size_t kMaxBlePendingPackets = 1;
inline constexpr size_t kMaxBlePendingPackets = 1;

//
// NOTE: Please do not alter the order of template specialization here as the logic
Expand Down
24 changes: 12 additions & 12 deletions src/app/tests/integration/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,18 @@ extern chip::SessionHolder gSession;
extern chip::TestPersistentStorageDelegate gStorage;
extern chip::Crypto::DefaultSessionKeystore gSessionKeystore;

constexpr chip::NodeId kTestNodeId = 0x1ULL;
constexpr chip::NodeId kTestNodeId1 = 0x2ULL;
constexpr chip::ClusterId kTestClusterId = 6;
constexpr chip::CommandId kTestCommandId = 40;
constexpr chip::EndpointId kTestEndpointId = 1;
constexpr chip::GroupId kTestGroupId = 0;
constexpr chip::FieldId kTestFieldId1 = 1;
constexpr chip::FieldId kTestFieldId2 = 2;
constexpr uint8_t kTestFieldValue1 = 1;
constexpr uint8_t kTestFieldValue2 = 2;
constexpr chip::EventId kTestChangeEvent1 = 1;
constexpr chip::EventId kTestChangeEvent2 = 2;
inline constexpr chip::NodeId kTestNodeId = 0x1ULL;
inline constexpr chip::NodeId kTestNodeId1 = 0x2ULL;
inline constexpr chip::ClusterId kTestClusterId = 6;
inline constexpr chip::CommandId kTestCommandId = 40;
inline constexpr chip::EndpointId kTestEndpointId = 1;
inline constexpr chip::GroupId kTestGroupId = 0;
inline constexpr chip::FieldId kTestFieldId1 = 1;
inline constexpr chip::FieldId kTestFieldId2 = 2;
inline constexpr uint8_t kTestFieldValue1 = 1;
inline constexpr uint8_t kTestFieldValue2 = 2;
inline constexpr chip::EventId kTestChangeEvent1 = 1;
inline constexpr chip::EventId kTestChangeEvent2 = 2;
void InitializeChip(void);
void ShutdownChip(void);
void TLVPrettyPrinter(const char * aFormat, ...);
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#include <lib/support/CodeUtils.h>
#include <lib/support/UnitTestUtils.h>

constexpr uint8_t kMaxAllowedPaths = 64;
inline constexpr uint8_t kMaxAllowedPaths = 64;

namespace chip {
namespace test_utils {
Expand Down
8 changes: 4 additions & 4 deletions src/app/util/mock/Constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@

namespace chip {
namespace Test {
constexpr EndpointId kMockEndpoint1 = 0xFFFE;
constexpr EndpointId kMockEndpoint2 = 0xFFFD;
constexpr EndpointId kMockEndpoint3 = 0xFFFC;
constexpr EndpointId kMockEndpointMin = 0xFFF1;
inline constexpr EndpointId kMockEndpoint1 = 0xFFFE;
inline constexpr EndpointId kMockEndpoint2 = 0xFFFD;
inline constexpr EndpointId kMockEndpoint3 = 0xFFFC;
inline constexpr EndpointId kMockEndpointMin = 0xFFF1;

constexpr AttributeId MockAttributeId(const uint16_t & id)
{
Expand Down
10 changes: 5 additions & 5 deletions src/app/util/privilege-storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@

#include <lib/core/DataModelTypes.h>

constexpr int kMatterAccessPrivilegeView = 0;
constexpr int kMatterAccessPrivilegeOperate = 1;
constexpr int kMatterAccessPrivilegeManage = 2;
constexpr int kMatterAccessPrivilegeAdminister = 3;
constexpr int kMatterAccessPrivilegeMaxValue = kMatterAccessPrivilegeAdminister;
inline constexpr int kMatterAccessPrivilegeView = 0;
inline constexpr int kMatterAccessPrivilegeOperate = 1;
inline constexpr int kMatterAccessPrivilegeManage = 2;
inline constexpr int kMatterAccessPrivilegeAdminister = 3;
inline constexpr int kMatterAccessPrivilegeMaxValue = kMatterAccessPrivilegeAdminister;

int MatterGetAccessPrivilegeForReadAttribute(chip::ClusterId cluster, chip::AttributeId attribute);
int MatterGetAccessPrivilegeForWriteAttribute(chip::ClusterId cluster, chip::AttributeId attribute);
Expand Down
Loading

0 comments on commit ad79e4d

Please sign in to comment.