Skip to content

Commit ab2f819

Browse files
author
David Roberts
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);

0 commit comments

Comments
 (0)