Skip to content

Commit

Permalink
refactor(internal): Remove & forbid use of rtti (#6262)
Browse files Browse the repository at this point in the history
* Remove Runtime Type Information from code and compilation

Removes the usage of rtti in Test.cpp (by removing the need to dynamic_cast).
Adds the no-rtti flag to the g++-compiler flags to exclude rtti completely.

* Disable rtti also in cpb and XCode compilation settings.
  • Loading branch information
petervdmeer authored Sep 24, 2021
1 parent 09168f1 commit 2381805
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 6 deletions.
2 changes: 2 additions & 0 deletions EndlessSky.cbp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@
<Add option="-Wall" />
<Add option="-std=c++11" />
<Add option="-Werror" />
<Add option="-Wold-style-cast" />
<Add option="-fno-rtti" />
<Add directory="C:/dev64/include" />
</Compiler>
<Linker>
Expand Down
4 changes: 2 additions & 2 deletions EndlessSky.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1179,7 +1179,7 @@
EXECUTABLE_PREFIX = "";
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_ENABLE_CPP_EXCEPTIONS = YES;
GCC_ENABLE_CPP_RTTI = YES;
GCC_ENABLE_CPP_RTTI = NO;
GCC_SYMBOLS_PRIVATE_EXTERN = YES;
GCC_WARN_64_TO_32_BIT_CONVERSION = NO;
MACOSX_DEPLOYMENT_TARGET = 10.9;
Expand All @@ -1204,7 +1204,7 @@
EXECUTABLE_PREFIX = "";
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_ENABLE_CPP_EXCEPTIONS = YES;
GCC_ENABLE_CPP_RTTI = YES;
GCC_ENABLE_CPP_RTTI = NO;
GCC_SYMBOLS_PRIVATE_EXTERN = YES;
GCC_WARN_64_TO_32_BIT_CONVERSION = NO;
MACOSX_DEPLOYMENT_TARGET = 10.9;
Expand Down
2 changes: 2 additions & 0 deletions EndlessSkyLib.cbp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
<Add option="-Wall" />
<Add option="-std=c++11" />
<Add option="-Werror" />
<Add option="-Wold-style-cast" />
<Add option="-fno-rtti" />
<Add directory="C:/dev64/include" />
</Compiler>
<Unit filename="source/AI.cpp" />
Expand Down
3 changes: 3 additions & 0 deletions EndlessSkyTests.cbp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
<Compiler>
<Add option="-Wall" />
<Add option="-std=c++11" />
<Add option="-Werror" />
<Add option="-Wold-style-cast" />
<Add option="-fno-rtti" />
<Add directory="tests/include" />
<Add directory="C:/dev64/include" />
</Compiler>
Expand Down
2 changes: 1 addition & 1 deletion SConstruct
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Help(opts.GenerateHelpText(env))
# $ CXXFLAGS=-msse3 scons
# $ CXXFLAGS=-march=native scons
# or modify the `flags` variable:
flags = ["-std=c++11", "-Wall", "-Werror", "-Wold-style-cast"]
flags = ["-std=c++11", "-Wall", "-Werror", "-Wold-style-cast", "-fno-rtti"]
if env["mode"] != "debug":
flags += ["-O3", "-flto"]
env.Append(LINKFLAGS = ["-O3", "-flto"])
Expand Down
2 changes: 1 addition & 1 deletion source/MainPanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class MainPanel : public Panel {
void OnCallback();

// Send a command to the engine (on behalf of the player).
void GiveCommand(const Command &command);
virtual void GiveCommand(const Command &command) override;

// The main panel allows fast-forward.
virtual bool AllowFastForward() const override;
Expand Down
7 changes: 7 additions & 0 deletions source/Panel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ bool Panel::ZoneClick(const Point &point)



// Queue a command for the Panel to process. Typically only used for MainPanel.
void Panel::GiveCommand(const Command &command)
{
}



// Panels will by default not allow fast-forward. The ones that do allow
// it will override this (virtual) function and return true.
bool Panel::AllowFastForward() const
Expand Down
3 changes: 3 additions & 0 deletions source/Panel.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ class Panel {
// so, apply that zone's action and return true.
bool ZoneClick(const Point &point);

// Queue a command for the Panel to process. Typically only used for MainPanel.
virtual void GiveCommand(const Command &command);

// Is fast-forward allowed to be on when this panel is on top of the GUI stack?
virtual bool AllowFastForward() const;

Expand Down
4 changes: 2 additions & 2 deletions source/Test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -463,9 +463,9 @@ void Test::Step(Context &context, UI &menuPanels, UI &gamePanels, PlayerInfo &pl

// Both get as well as the cast can result in a nullpointer. In both cases we
// will fail the test, since we expect the MainPanel to be here.
auto mainPanel = dynamic_cast<MainPanel *>(gamePanels.Root().get());
auto mainPanel = gamePanels.Root().get();
if(!mainPanel)
Fail(context, player, "root gamepanel of wrong type when sending command");
Fail(context, player, "root gamepanel not found when sending command");

mainPanel->GiveCommand(stepToRun.command);
}
Expand Down

0 comments on commit 2381805

Please sign in to comment.