Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/tools/Checks.cpp
blob: a348eff42d4fc2e81a843d082991f4eee1732f5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// Copyright (c) 2026 Progmasoft.
// SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-2.0

#include <algorithm>
#include <set>
#include <string>
#include <string_view>
#include <vector>

#include "tools/Amalgamation.hpp"
#include "tools/Checks.hpp"

namespace Progmasoft::Catch3::Tools
{
    namespace
    {
        namespace fs = std::filesystem;

        constexpr std::string_view kCatch2License = R"(
//              Copyright Catch2 Authors
// Distributed under the Boost Software License, Version 1.0.
//   (See accompanying file LICENSE.txt or copy at
//        https://www.boost.org/LICENSE_1_0.txt)

// SPDX-License-Identifier: BSL-1.0
)";

        constexpr std::string_view kProgmasoftLicense
            = R"(// Copyright (c) 2026 Progmasoft.
// SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-2.0
)";

        [[nodiscard]] bool
        IsInternalFolder(std::string_view name)
        {
            return name == "Detail" || name == "Internal";
        }

        class ConvenienceHeaders final
        {
        public:
            ConvenienceHeaders(const fs::path &repository, Problems &found)
                : root(repository)
                , problems(found)
            {
                for (const auto &file : UpstreamFiles(repository, found))
                {
                    if (EndsWith(file, ".hpp"))
                    {
                        headers.insert(file.substr(4));
                    }
                }
            }

            void
            Walk(const std::string &folder)
            {
                Verify(folder);
                for (const auto &name : Subfolders(folder))
                {
                    if (!IsInternalFolder(name))
                    {
                        Walk(folder + "/" + name);
                    }
                }
            }

        private:
            [[nodiscard]] std::vector<std::string>
            HeadersIn(const std::string &folder) const
            {
                const auto prefix = folder + "/";
                std::vector<std::string> found;
                for (const auto &header : headers)
                {
                    if (StartsWith(header, prefix)
                        && header.find('/', prefix.size()) == std::string::npos)
                    {
                        found.push_back(header);
                    }
                }
                return found;
            }

            [[nodiscard]] std::set<std::string>
            Subfolders(const std::string &folder) const
            {
                const auto prefix = folder + "/";
                std::set<std::string> names;
                for (const auto &header : headers)
                {
                    const auto slash = header.find('/', prefix.size());
                    if (StartsWith(header, prefix)
                        && slash != std::string::npos)
                    {
                        names.insert(header.substr(prefix.size(),
                                                   slash - prefix.size()));
                    }
                }
                return names;
            }

            [[nodiscard]] std::vector<std::string>
            Wanted(const std::string &folder) const
            {
                auto wanted = HeadersIn(folder);
                for (const auto &name : Subfolders(folder))
                {
                    const auto subfolder = folder + "/" + name;
                    if (IsInternalFolder(name))
                    {
                        const auto inner = HeadersIn(subfolder);
                        wanted.insert(wanted.end(), inner.begin(), inner.end());
                    }
                    else
                    {
                        wanted.push_back(subfolder + "/All.hpp");
                    }
                }
                std::sort(wanted.begin(), wanted.end());
                return wanted;
            }

            void
            Verify(const std::string &folder)
            {
                const auto name = folder + "/All.hpp";
                if (headers.count(name) == 0)
                {
                    problems.push_back("missing convenience header: " + name);
                    return;
                }
                std::vector<std::string> includes;
                const auto text = ReadOrReport(root / "src" / name, problems);
                for (const auto &line : SplitLines(text))
                {
                    const auto include = ParseInclude(line);
                    if (StartsWith(line, "#include") && include
                        && include->Angled
                        && StartsWith(include->Name, "Progmasoft/Catch3/")
                        && EndsWith(include->Name, ".hpp"))
                    {
                        includes.push_back(include->Name);
                    }
                }
                if (!std::is_sorted(includes.begin(), includes.end()))
                {
                    problems.push_back(name + ": includes are not sorted");
                }
                std::set<std::string> seen;
                for (const auto &include : includes)
                {
                    if (!seen.insert(include).second)
                    {
                        problems.push_back(name + ": duplicated include "
                                           + include);
                    }
                }
                std::set<std::string> wanted;
                for (auto &include : Wanted(folder))
                {
                    if (include != name)
                    {
                        wanted.insert(std::move(include));
                    }
                }
                for (const auto &include : wanted)
                {
                    if (seen.count(include) == 0
                        && include
                               != "Progmasoft/Catch3/Internal/"
                                  "WindowsHProxy.hpp")
                    {
                        problems.push_back(name + ": missing include "
                                           + include);
                    }
                }
                for (const auto &include : seen)
                {
                    if (wanted.count(include) == 0)
                    {
                        problems.push_back(name + ": superfluous include "
                                           + include);
                    }
                }
            }

            fs::path root;
            Problems &problems;
            std::set<std::string> headers;
        };
    } // namespace

    void
    CheckLicenses(const fs::path &root, Problems &problems)
    {
        const auto upstream = UpstreamFiles(root, problems);
        for (const char *directory :
             { "src/Progmasoft", "tests", "examples", "fuzzing", "tools" })
        {
            for (const auto &file :
                 FilesBelow(root, root / directory, { ".cpp", ".hpp" }, true))
            {
                const bool progmasoft
                    = StartsWith(file, "tools/")
                      || (file.find("Progmasoft/") != std::string::npos
                          && upstream.count(file) == 0);
                const auto expected
                    = progmasoft ? kProgmasoftLicense : kCatch2License;
                if (!StartsWith(ReadOrReport(root / file, problems), expected))
                {
                    problems.push_back(file + " does not have its license");
                }
            }
        }
    }

    void
    CheckConvenienceHeaders(const fs::path &root, Problems &problems)
    {
        ConvenienceHeaders{ root, problems }.Walk("Progmasoft/Catch3");
    }

    void
    CheckPragmaOnce(const fs::path &root, Problems &problems)
    {
        for (const auto &file :
             FilesBelow(root, root / "src/Progmasoft", { ".hpp" }, true))
        {
            const auto text = ReadOrReport(root / file, problems);
            if (!StartsWith(text, "#pragma once\n")
                && text.find("\n#pragma once\n") == std::string::npos)
            {
                problems.push_back(file + " has no #pragma once");
            }
        }
    }

    void
    CheckDuplicateFilenames(const fs::path &root, Problems &problems)
    {
        std::set<std::string> names;
        for (const auto &file :
             FilesBelow(root, root / "src/Progmasoft", { ".cpp" }, true))
        {
            const auto name = fs::path{ file }.filename().string();
            if (!names.insert(name).second)
            {
                problems.push_back(file
                                   + " shares its name with another source");
            }
        }
    }
} // namespace Progmasoft::Catch3::Tools