Skip to content

Commit ab2f819

Browse files
committed
[ML] Correct the way internal linkage of constants is achieved (elastic#102)
1. There's no need for constants declared in unnamed namespaces to be declared static - they already have internal linkage. 2. Constants of type char* pointing to literals should be declared const char* const - declaring them const char* does not result in internal linkage because it allows them to be changed to point at something completely different!
1 parent 7071a64 commit ab2f819

24 files changed

+58
-63
lines changed

include/core/COsFileFuncs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class CORE_EXPORT COsFileFuncs : private CNonInstantiatable {
7070
static const int EXECUTABLE;
7171

7272
//! The name of the magic file that discards everything written to it
73-
static const char* NULL_FILENAME;
73+
static const char* const NULL_FILENAME;
7474

7575
public:
7676
//! Signed size type (to be used instead of ssize_t)

include/core/CProcess.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ namespace core {
4040
class CORE_EXPORT CProcess : private CNonCopyable {
4141
public:
4242
//! These messages need to be 100% standard across all services
43-
static const char* STARTING_MSG;
44-
static const char* STARTED_MSG;
45-
static const char* STOPPING_MSG;
46-
static const char* STOPPED_MSG;
43+
static const char* const STARTING_MSG;
44+
static const char* const STARTED_MSG;
45+
static const char* const STOPPING_MSG;
46+
static const char* const STOPPED_MSG;
4747

4848
public:
4949
//! Prototype of the mlMain() function

include/core/CWordDictionary.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ class CORE_EXPORT CWordDictionary : private CNonCopyable {
149149

150150
private:
151151
//! Name of the file to load that contains the dictionary words.
152-
static const char* DICTIONARY_FILE;
152+
static const char* const DICTIONARY_FILE;
153153

154154
//! The constructor loads a file, and hence may take a while. This
155155
//! mutex prevents the singleton object being constructed simultaneously

include/core/CXmlParser.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class CORE_EXPORT CXmlParser : public CXmlParserIntf {
5959
static const std::string ATTRIBUTE_EQUALS;
6060
static const size_t DEFAULT_INDENT_SPACES;
6161
static const size_t MAX_INDENT_SPACES;
62-
static const char* INDENT_SPACE_STR;
62+
static const char* const INDENT_SPACE_STR;
6363

6464
public:
6565
using TStrVec = std::vector<std::string>;

include/core/CoreTypes.h

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,12 @@ using TTime = time_t;
1717

1818
//! The standard line ending for the platform - DON'T make this std::string as
1919
//! that would cause many strings to be constructed (since the variable is
20-
//! static const at the namespace level, so is internal to each file this
21-
//! header is included in)
20+
//! const at the namespace level, so is internal to each file this header is
21+
//! included in)
2222
#ifdef Windows
23-
static const char* LINE_ENDING = "\r\n";
23+
const char* const LINE_ENDING = "\r\n";
2424
#else
25-
#ifdef __GNUC__
26-
// Tell g++ that it's reasonable that this variable isn't used
27-
__attribute__((unused)) static const char* LINE_ENDING = "\n";
28-
#else
29-
static const char* LINE_ENDING = "\n";
30-
#endif
25+
const char* const LINE_ENDING = "\n";
3126
#endif
3227
}
3328
}

include/model/ModelTypes.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@ enum EMetricCategory {
701701
};
702702

703703
//! Must correspond to the number of enumeration values of EMetricCategory
704-
static const size_t NUM_METRIC_CATEGORIES = 9;
704+
const size_t NUM_METRIC_CATEGORIES = 9;
705705

706706
//! Get the metric feature data corresponding to \p feature
707707
//! if there is one.

lib/api/unittest/CIoManagerTest.cc

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,19 @@ const uint32_t PAUSE_TIME_MS = 10;
2525
const size_t MAX_ATTEMPTS = 100;
2626
const size_t TEST_SIZE = 10000;
2727
const char TEST_CHAR = 'a';
28-
const char* GOOD_INPUT_FILE_NAME = "testfiles/good_input_file";
29-
const char* GOOD_OUTPUT_FILE_NAME = "testfiles/good_output_file";
28+
const char* const GOOD_INPUT_FILE_NAME = "testfiles/good_input_file";
29+
const char* const GOOD_OUTPUT_FILE_NAME = "testfiles/good_output_file";
3030
#ifdef Windows
31-
const char* GOOD_INPUT_PIPE_NAME = "\\\\.\\pipe\\good_input_pipe";
32-
const char* GOOD_OUTPUT_PIPE_NAME = "\\\\.\\pipe\\good_output_pipe";
31+
const char* const GOOD_INPUT_PIPE_NAME = "\\\\.\\pipe\\good_input_pipe";
32+
const char* const GOOD_OUTPUT_PIPE_NAME = "\\\\.\\pipe\\good_output_pipe";
3333
#else
34-
const char* GOOD_INPUT_PIPE_NAME = "testfiles/good_input_pipe";
35-
const char* GOOD_OUTPUT_PIPE_NAME = "testfiles/good_output_pipe";
34+
const char* const GOOD_INPUT_PIPE_NAME = "testfiles/good_input_pipe";
35+
const char* const GOOD_OUTPUT_PIPE_NAME = "testfiles/good_output_pipe";
3636
#endif
37-
const char* BAD_INPUT_FILE_NAME = "can't_create_a_file_here/bad_input_file";
38-
const char* BAD_OUTPUT_FILE_NAME = "can't_create_a_file_here/bad_output_file";
39-
const char* BAD_INPUT_PIPE_NAME = "can't_create_a_pipe_here/bad_input_pipe";
40-
const char* BAD_OUTPUT_PIPE_NAME = "can't_create_a_pipe_here/bad_output_pipe";
37+
const char* const BAD_INPUT_FILE_NAME = "can't_create_a_file_here/bad_input_file";
38+
const char* const BAD_OUTPUT_FILE_NAME = "can't_create_a_file_here/bad_output_file";
39+
const char* const BAD_INPUT_PIPE_NAME = "can't_create_a_pipe_here/bad_input_pipe";
40+
const char* const BAD_OUTPUT_PIPE_NAME = "can't_create_a_pipe_here/bad_output_pipe";
4141

4242
class CThreadDataWriter : public ml::core::CThread {
4343
public:

lib/core/CMutex_Windows.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
namespace {
99
// 4000 is a value that Microsoft uses in some of their code, so it's
1010
// hopefully a reasonably sensible setting
11-
static const DWORD SPIN_COUNT(4000);
11+
const DWORD SPIN_COUNT(4000);
1212
}
1313

1414
namespace ml {

lib/core/COsFileFuncs.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const int COsFileFuncs::READABLE(R_OK);
2727
const int COsFileFuncs::WRITABLE(W_OK);
2828
const int COsFileFuncs::EXECUTABLE(X_OK);
2929

30-
const char* COsFileFuncs::NULL_FILENAME("/dev/null");
30+
const char* const COsFileFuncs::NULL_FILENAME("/dev/null");
3131

3232
int COsFileFuncs::open(const char* path, int oflag) {
3333
return ::open(path, oflag);

lib/core/COsFileFuncs_Windows.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const int COsFileFuncs::WRITABLE(2);
5151
// For Windows, consider "executable" the same as "readable" for the time being
5252
const int COsFileFuncs::EXECUTABLE(4);
5353

54-
const char* COsFileFuncs::NULL_FILENAME("nul");
54+
const char* const COsFileFuncs::NULL_FILENAME("nul");
5555

5656
int COsFileFuncs::open(const char* path, int oflag) {
5757
return COsFileFuncs::open(path, oflag, 0);

lib/core/CProcess.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
namespace ml {
1414
namespace core {
1515

16-
const char* CProcess::STARTING_MSG("Process Starting.");
17-
const char* CProcess::STARTED_MSG("Process Started.");
18-
const char* CProcess::STOPPING_MSG("Process Shutting Down.");
19-
const char* CProcess::STOPPED_MSG("Process Exiting.");
16+
const char* const CProcess::STARTING_MSG("Process Starting.");
17+
const char* const CProcess::STARTED_MSG("Process Started.");
18+
const char* const CProcess::STOPPING_MSG("Process Shutting Down.");
19+
const char* const CProcess::STOPPED_MSG("Process Exiting.");
2020

2121
CProcess::CProcess()
2222
: m_IsService(false), m_Initialised(false), m_Running(false),

lib/core/CProcess_Windows.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ const DWORD PPID(findParentProcessId());
6262
namespace ml {
6363
namespace core {
6464

65-
const char* CProcess::STARTING_MSG("Process Starting.");
66-
const char* CProcess::STARTED_MSG("Process Started.");
67-
const char* CProcess::STOPPING_MSG("Process Shutting Down.");
68-
const char* CProcess::STOPPED_MSG("Process Exiting.");
65+
const char* const CProcess::STARTING_MSG("Process Starting.");
66+
const char* const CProcess::STARTED_MSG("Process Started.");
67+
const char* const CProcess::STOPPING_MSG("Process Shutting Down.");
68+
const char* const CProcess::STOPPED_MSG("Process Exiting.");
6969

7070
CProcess::CProcess()
7171
: m_IsService(false), m_Initialised(false), m_Running(false),

lib/core/CResourceLocator.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include <stdlib.h>
1212

1313
namespace {
14-
const char* CPP_SRC_HOME("CPP_SRC_HOME");
14+
const char* const CPP_SRC_HOME("CPP_SRC_HOME");
1515
}
1616

1717
namespace ml {

lib/core/CStatistics.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ namespace {
2525

2626
using TGenericLineWriter = core::CRapidJsonLineWriter<rapidjson::OStreamWrapper>;
2727

28-
static const std::string NAME_TYPE("name");
29-
static const std::string DESCRIPTION_TYPE("description");
30-
static const std::string STAT_TYPE("value");
28+
const std::string NAME_TYPE("name");
29+
const std::string DESCRIPTION_TYPE("description");
30+
const std::string STAT_TYPE("value");
3131

3232
//! Persistence tags
33-
static const std::string KEY_TAG("a");
34-
static const std::string VALUE_TAG("b");
33+
const std::string KEY_TAG("a");
34+
const std::string VALUE_TAG("b");
3535

3636
//! Helper function to add a string/int pair to JSON writer
3737
void addStringInt(TGenericLineWriter& writer,

lib/core/CUname_Windows.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ bool queryKernelVersion(uint16_t& major, uint16_t& minor, uint16_t& build) {
2525
// then distinguish client/server versions of Windows using
2626
// VerifyVersionInfo().
2727

28-
static const char* KERNEL32_DLL("kernel32.dll");
28+
static const char* const KERNEL32_DLL("kernel32.dll");
2929

3030
DWORD handle(0);
3131
DWORD size(GetFileVersionInfoSize(KERNEL32_DLL, &handle));

lib/core/CWindowsError_Windows.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include <cmath>
1212

1313
namespace {
14-
static const size_t BUFFER_SIZE(1024);
14+
const size_t BUFFER_SIZE(1024);
1515

1616
// This is a workaround for a bug in the Visual Studio 2013 C runtime library.
1717
// See http://connect.microsoft.com/VisualStudio/feedback/details/811093 for

lib/core/CWordDictionary.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ CWordDictionary::EPartOfSpeech partOfSpeechFromCode(char partOfSpeechCode) {
6565
}
6666
}
6767

68-
const char* CWordDictionary::DICTIONARY_FILE("ml-en.dict");
68+
const char* const CWordDictionary::DICTIONARY_FILE("ml-en.dict");
6969

7070
CFastMutex CWordDictionary::ms_LoadMutex;
7171
volatile CWordDictionary* CWordDictionary::ms_Instance(nullptr);

lib/core/CXmlParser.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const std::string CXmlParser::ATTRIBUTE_EQUALS("=");
3030
const size_t CXmlParser::DEFAULT_INDENT_SPACES(4);
3131
const size_t CXmlParser::MAX_INDENT_SPACES(10);
3232
// The number of spaces in this constant MUST match the maximum above
33-
const char* CXmlParser::INDENT_SPACE_STR(" ");
33+
const char* const CXmlParser::INDENT_SPACE_STR(" ");
3434

3535
CXmlParser::CXmlParser()
3636
: m_Doc(nullptr), m_XPathContext(nullptr), m_NavigatedNode(nullptr) {

lib/core/unittest/CDualThreadStreamBufTest.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ void CDualThreadStreamBufTest::testPutback() {
191191

192192
buf.signalEndOfFile();
193193

194-
static const char* PUTBACK_CHARS("put this back");
194+
static const char* const PUTBACK_CHARS("put this back");
195195
std::istream strm(&buf);
196196
char c('\0');
197197
CPPUNIT_ASSERT(strm.get(c).good());

lib/core/unittest/CLoggerTest.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222

2323
namespace {
2424
#ifdef Windows
25-
const char* TEST_PIPE_NAME = "\\\\.\\pipe\\testpipe";
25+
const char* const TEST_PIPE_NAME = "\\\\.\\pipe\\testpipe";
2626
#else
27-
const char* TEST_PIPE_NAME = "testfiles/testpipe";
27+
const char* const TEST_PIPE_NAME = "testfiles/testpipe";
2828
#endif
2929
}
3030

lib/core/unittest/CNamedPipeFactoryTest.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ const size_t MAX_ATTEMPTS = 100;
2626
const size_t TEST_SIZE = 10000;
2727
const char TEST_CHAR = 'a';
2828
#ifdef Windows
29-
const char* TEST_PIPE_NAME = "\\\\.\\pipe\\testpipe";
29+
const char* const TEST_PIPE_NAME = "\\\\.\\pipe\\testpipe";
3030
#else
31-
const char* TEST_PIPE_NAME = "testfiles/testpipe";
31+
const char* const TEST_PIPE_NAME = "testfiles/testpipe";
3232
#endif
3333

3434
class CThreadDataWriter : public ml::core::CThread {
@@ -279,7 +279,7 @@ void CNamedPipeFactoryTest::testErrorIfSymlink() {
279279
// the file system
280280
LOG_DEBUG(<< "symlink test not relevant to Windows");
281281
#else
282-
static const char* TEST_SYMLINK_NAME = "test_symlink";
282+
static const char* const TEST_SYMLINK_NAME = "test_symlink";
283283

284284
// Remove any files left behind by a previous failed test, but don't check
285285
// the return codes as these calls will usually fail

lib/maths/CSampling.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ void sampleQuantiles(const DISTRIBUTION& distribution, std::size_t n, TDoubleVec
393393
result.push_back(expectation(distribution, a, b));
394394
}
395395

396-
static const std::string RNG_TAG("a");
396+
const std::string RNG_TAG("a");
397397
}
398398

399399
bool CSampling::staticsAcceptRestoreTraverser(core::CStateRestoreTraverser& traverser) {

lib/maths/CXMeansOnline1d.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -588,9 +588,9 @@ const std::string DECAY_RATE_TAG("j");
588588
const std::string HISTORY_LENGTH_TAG("k");
589589

590590
// CXMeansOnline1d::CCluster
591-
static const std::string INDEX_TAG("a");
592-
static const std::string STRUCTURE_TAG("b");
593-
static const std::string PRIOR_TAG("c");
591+
const std::string INDEX_TAG("a");
592+
const std::string STRUCTURE_TAG("b");
593+
const std::string PRIOR_TAG("c");
594594

595595
const std::string EMPTY_STRING;
596596
}

lib/model/CForecastModelPersist.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ namespace ml {
2222
namespace model {
2323

2424
namespace {
25-
static const std::string FORECAST_MODEL_PERSIST_TAG("forecast_persist");
26-
static const std::string FEATURE_TAG("feature");
27-
static const std::string DATA_TYPE_TAG("datatype");
28-
static const std::string MODEL_TAG("model");
29-
static const std::string BY_FIELD_VALUE_TAG("by_field_value");
25+
const std::string FORECAST_MODEL_PERSIST_TAG("forecast_persist");
26+
const std::string FEATURE_TAG("feature");
27+
const std::string DATA_TYPE_TAG("datatype");
28+
const std::string MODEL_TAG("model");
29+
const std::string BY_FIELD_VALUE_TAG("by_field_value");
3030
}
3131

3232
CForecastModelPersist::CPersist::CPersist(const std::string& temporaryPath)

0 commit comments

Comments
 (0)