Jlm
Loading...
Searching...
No Matches
JlcCommandLineParserTests.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2018 Nico Reißmann <nico.reissmann@gmail.com>
3 * See COPYING for terms of redistribution.
4 */
5
6#include <gtest/gtest.h>
7
9
10#include <cstring>
11
13ParseCommandLineArguments(const std::vector<std::string> & commandLineArguments)
14{
15 std::vector<const char *> cStrings;
16 for (const auto & commandLineArgument : commandLineArguments)
17 {
18 cStrings.push_back(commandLineArgument.c_str());
19 }
20
21 static jlm::tooling::JlcCommandLineParser commandLineParser;
22 return commandLineParser.ParseCommandLineArguments(
23 static_cast<int>(cStrings.size()),
24 cStrings.data());
25}
26
27TEST(JlcCommandLineParserTests, Test1)
28{
29 // Arrange
30 std::vector<std::string> commandLineArguments({ "jlc", "-c", "-o", "foo.o", "foo.c" });
31
32 // Act
33 auto & commandLineOptions = ParseCommandLineArguments(commandLineArguments);
34
35 // Assert
36 EXPECT_EQ(commandLineOptions.Compilations_.size(), 1u);
37 auto & compilation = commandLineOptions.Compilations_[0];
38
39 EXPECT_EQ(compilation.RequiresLinking(), false);
40 EXPECT_EQ(compilation.OutputFile(), "foo.o");
41}
42
43TEST(JlcCommandLineParserTests, Test2)
44{
45 // Arrange
46 std::vector<std::string> commandLineArguments({ "jlc", "-o", "foobar", "/tmp/f1.o" });
47
48 // Act
49 auto & commandLineOptions = ParseCommandLineArguments(commandLineArguments);
50
51 // Assert
52 EXPECT_EQ(commandLineOptions.Compilations_.size(), 1u);
53 EXPECT_EQ(commandLineOptions.OutputFile_, "foobar");
54
55 auto & compilation = commandLineOptions.Compilations_[0];
56 EXPECT_FALSE(compilation.RequiresParsing());
57 EXPECT_FALSE(compilation.RequiresOptimization());
58 EXPECT_FALSE(compilation.RequiresAssembly());
59 EXPECT_TRUE(compilation.RequiresLinking());
60}
61
62TEST(JlcCommandLineParserTests, Test3)
63{
64 using namespace jlm::tooling;
65
66 // Arrange
67 std::vector<std::string> commandLineArguments({ "jlc", "-O", "foobar.c" });
68
69 // Act
70 auto & commandLineOptions = ParseCommandLineArguments(commandLineArguments);
71
72 // Assert
73 EXPECT_EQ(commandLineOptions.OptimizationLevel_, JlcCommandLineOptions::OptimizationLevel::O0);
74}
75
76TEST(JlcCommandLineParserTests, Test4)
77{
78 // Arrange
79 std::vector<std::string> commandLineArguments({ "jlc", "foobar.c", "-c" });
80
81 // Act
82 auto & commandLineOptions = ParseCommandLineArguments(commandLineArguments);
83
84 // Assert
85 EXPECT_EQ(commandLineOptions.Compilations_.size(), 1u);
86
87 auto & compilation = commandLineOptions.Compilations_[0];
88 EXPECT_FALSE(compilation.RequiresLinking());
89 EXPECT_EQ(compilation.OutputFile(), "foobar.o");
90}
91
92TEST(JlcCommandLineParserTests, TestJlmOptOptimizations)
93{
94 using namespace jlm::tooling;
95
96 // Arrange
97 std::vector<std::string> commandLineArguments(
98 { "jlc", "foobar.c", "-JCommonNodeElimination", "-JDeadNodeElimination" });
99
100 // Act
101 auto & commandLineOptions = ParseCommandLineArguments(commandLineArguments);
102
103 // Assert
104 EXPECT_EQ(commandLineOptions.JlmOptOptimizations_.size(), 2u);
105 EXPECT_EQ(
106 commandLineOptions.JlmOptOptimizations_[0],
107 JlmOptCommandLineOptions::OptimizationId::CommonNodeElimination);
108 EXPECT_EQ(
109 commandLineOptions.JlmOptOptimizations_[1],
110 JlmOptCommandLineOptions::OptimizationId::DeadNodeElimination);
111}
112
113TEST(JlcCommandLineParserTests, TestFalseJlmOptOptimization)
114{
115 using namespace jlm::tooling;
116
117 // Arrange
118 std::vector<std::string> commandLineArguments({ "jlc", "-JFoobar", "foobar.c" });
119
120 // Act & Assert
121 EXPECT_THROW(ParseCommandLineArguments(commandLineArguments), CommandLineParser::Exception);
122}
123
124TEST(JlcCommandLineParserTests, TestJlmOptPassStatistics)
125{
126 using namespace jlm::tooling;
127
128 // Arrange
129 std::vector<std::string> commandLineArguments({ "jlc",
130 "--JlmOptPassStatistics=print-aggregation-time",
131 "--JlmOptPassStatistics=print-andersen-analysis",
132 "foobar.c" });
133
134 jlm::util::HashSet expectedStatistics(
136
137 // Act
138 auto & commandLineOptions = ParseCommandLineArguments(commandLineArguments);
139
140 // Assert
141 EXPECT_EQ(commandLineOptions.JlmOptPassStatistics_, expectedStatistics);
142}
TEST(JlcCommandLineParserTests, Test1)
static const jlm::tooling::JlcCommandLineOptions & ParseCommandLineArguments(const std::vector< std::string > &commandLineArguments)
const JlcCommandLineOptions & ParseCommandLineArguments(int argc, const char *const *argv) override