Skip to content

Commit 2f4f85b

Browse files
Krzmbrzljimidle
authored andcommitted
Cpp: Remove code duplication (antlr#3995)
* Cpp: Remove code duplication The same function was defined inside multiple different source files (in an anonymous namespace). Not only is this generally bad practice from a maintenance point of view, but it also break unity builds (which can speed up compilation times A LOT). The fix simply consists in introducing a new Utils file that contains the single implementation and every source file that uses this function simply has to be linked against that new file (and include the corresponding header). Signed-off-by: Robert Adam <[email protected]> * CI: Enable unity builds for cpp targets This should reduce the build time for those steps significantly Note: Unity builds are enabled only for cmake-based CI builds Signed-off-by: Robert Adam <[email protected]> * CI: Perform unity and non-unity cpp builds Signed-off-by: Robert Adam <[email protected]> --------- Signed-off-by: Robert Adam <[email protected]> Signed-off-by: Jim.Idle <[email protected]>
1 parent 60a8a6d commit 2f4f85b

File tree

8 files changed

+27
-28
lines changed

8 files changed

+27
-28
lines changed

.github/workflows/hosted.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ jobs:
2626
windows-2022
2727
]
2828
compiler: [ clang, gcc ]
29+
unity_build: [ ON, OFF ]
2930
exclude:
3031
- os: windows-2022
3132
compiler: gcc
@@ -95,7 +96,7 @@ jobs:
9596
9697
cd runtime/Cpp
9798
98-
cmake -G Ninja -DCMAKE_BUILD_TYPE=Debug -DANTLR_BUILD_CPP_TESTS=OFF -S . -B out/Debug
99+
cmake -G Ninja -DCMAKE_BUILD_TYPE=Debug -DANTLR_BUILD_CPP_TESTS=OFF -DCMAKE_UNITY_BUILD=${{ matrix.unity_build }} -DCMAKE_UNITY_BUILD_BATCH_SIZE=20 -S . -B out/Debug
99100
if %errorlevel% neq 0 exit /b %errorlevel%
100101
101102
cmake --build out/Debug -j %NUMBER_OF_PROCESSORS%
@@ -130,7 +131,7 @@ jobs:
130131
131132
cd runtime/Cpp
132133
133-
cmake -G Ninja -DCMAKE_BUILD_TYPE=Debug -DANTLR_BUILD_CPP_TESTS=OFF -S . -B out/Debug
134+
cmake -G Ninja -DCMAKE_BUILD_TYPE=Debug -DANTLR_BUILD_CPP_TESTS=OFF -DCMAKE_UNITY_BUILD=${{ matrix.unity_build }} -DCMAKE_UNITY_BUILD_BATCH_SIZE=20 -S . -B out/Debug
134135
cmake --build out/Debug --parallel
135136
136137
cmake -G Ninja -DCMAKE_BUILD_TYPE=Release -DANTLR_BUILD_CPP_TESTS=OFF -S . -B out/Release

runtime/Cpp/runtime/antlr4cpp-vs2019.vcxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,4 +649,4 @@
649649
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
650650
<ImportGroup Label="ExtensionTargets">
651651
</ImportGroup>
652-
</Project>
652+
</Project>

runtime/Cpp/runtime/antlr4cpp-vs2022.vcxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,4 +649,4 @@
649649
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
650650
<ImportGroup Label="ExtensionTargets">
651651
</ImportGroup>
652-
</Project>
652+
</Project>

runtime/Cpp/runtime/src/atn/ArrayPredictionContext.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <cstring>
99

1010
#include "atn/SingletonPredictionContext.h"
11+
#include "atn/HashUtils.h"
1112
#include "misc/MurmurHash.h"
1213
#include "support/Casts.h"
1314

@@ -17,10 +18,6 @@ using namespace antlrcpp;
1718

1819
namespace {
1920

20-
bool cachedHashCodeEqual(size_t lhs, size_t rhs) {
21-
return lhs == rhs || lhs == 0 || rhs == 0;
22-
}
23-
2421
bool predictionContextEqual(const Ref<const PredictionContext> &lhs, const Ref<const PredictionContext> &rhs) {
2522
// parent PredictionContext pointers can be null during full context mode and
2623
// the ctxs are in an ArrayPredictionContext. If both are null, return true
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/* Copyright (c) 2022 The ANTLR Project. All rights reserved.
2+
* Use of this file is governed by the BSD 3-clause license that
3+
* can be found in the LICENSE.txt file in the project root.
4+
*/
5+
6+
#pragma once
7+
8+
#include <cstddef>
9+
10+
namespace antlr4 {
11+
namespace atn {
12+
13+
inline bool cachedHashCodeEqual(size_t lhs, size_t rhs) {
14+
return lhs == rhs || lhs == 0 || rhs == 0;
15+
}
16+
17+
} // namespace atn
18+
} // namespace antlr4

runtime/Cpp/runtime/src/atn/LexerActionExecutor.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include "misc/MurmurHash.h"
77
#include "atn/LexerIndexedCustomAction.h"
8+
#include "atn/HashUtils.h"
89
#include "support/CPPUtils.h"
910
#include "support/Arrays.h"
1011
#include "support/Casts.h"
@@ -18,10 +19,6 @@ using namespace antlrcpp;
1819

1920
namespace {
2021

21-
bool cachedHashCodeEqual(size_t lhs, size_t rhs) {
22-
return lhs == rhs || lhs == 0 || rhs == 0;
23-
}
24-
2522
bool lexerActionEqual(const Ref<const LexerAction> &lhs, const Ref<const LexerAction> &rhs) {
2623
return *lhs == *rhs;
2724
}

runtime/Cpp/runtime/src/atn/LexerIndexedCustomAction.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* can be found in the LICENSE.txt file in the project root.
44
*/
55

6+
#include "atn/HashUtils.h"
67
#include "misc/MurmurHash.h"
78
#include "Lexer.h"
89
#include "support/CPPUtils.h"
@@ -15,14 +16,6 @@ using namespace antlr4::atn;
1516
using namespace antlr4::misc;
1617
using namespace antlrcpp;
1718

18-
namespace {
19-
20-
bool cachedHashCodeEqual(size_t lhs, size_t rhs) {
21-
return lhs == rhs || lhs == 0 || rhs == 0;
22-
}
23-
24-
}
25-
2619
LexerIndexedCustomAction::LexerIndexedCustomAction(int offset, Ref<const LexerAction> action)
2720
: LexerAction(LexerActionType::INDEXED_CUSTOM, true), _action(std::move(action)), _offset(offset) {}
2821

runtime/Cpp/runtime/src/atn/SingletonPredictionContext.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,11 @@
77

88
#include "support/Casts.h"
99
#include "misc/MurmurHash.h"
10+
#include "atn/HashUtils.h"
1011

1112
using namespace antlr4::atn;
1213
using namespace antlrcpp;
1314

14-
namespace {
15-
16-
bool cachedHashCodeEqual(size_t lhs, size_t rhs) {
17-
return lhs == rhs || lhs == 0 || rhs == 0;
18-
}
19-
20-
}
21-
2215
SingletonPredictionContext::SingletonPredictionContext(Ref<const PredictionContext> parent, size_t returnState)
2316
: PredictionContext(PredictionContextType::SINGLETON), parent(std::move(parent)), returnState(returnState) {
2417
assert(returnState != ATNState::INVALID_STATE_NUMBER);

0 commit comments

Comments
 (0)